Gp3.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. \file Gp3.c
  3. \brief OGSF library - loading point sets (lower level functions)
  4. GRASS OpenGL gsurf OGSF Library
  5. (C) 1999-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Bill Brown USACERL, GMSL/University of Illinois (January 1994)
  11. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/raster.h>
  16. #include <grass/site.h>
  17. #include <grass/vector.h>
  18. #include <grass/glocale.h>
  19. #include <grass/gstypes.h>
  20. /*!
  21. \brief Load to points to memory
  22. The other alternative may be to load to a tmp file.
  23. \param grassname vector point map
  24. \param nsites
  25. \param has_z 2D or 3D points?
  26. \return pointer to geopoint struct
  27. \return NULL on failure
  28. */
  29. geopoint *Gp_load_sites(const char *grassname, int *nsites, int *has_z)
  30. {
  31. struct Map_info map;
  32. static struct line_pnts *Points = NULL;
  33. struct line_cats *Cats = NULL;
  34. geopoint *top, *gpt, *prev;
  35. int np, ltype, eof;
  36. struct Cell_head wind;
  37. RASTER_MAP_TYPE rtype;
  38. int ndim;
  39. const char *mapset;
  40. np = 0;
  41. eof = 0;
  42. *has_z = 0;
  43. mapset = G_find_vector2(grassname, "");
  44. if (!mapset) {
  45. G_warning(_("Vector map <%s> not found"), grassname);
  46. return NULL;
  47. }
  48. Vect_set_open_level(2);
  49. if (Vect_open_old(&map, grassname, "") == -1) {
  50. G_fatal_error(_("Unable to open vector map <%s>"),
  51. G_fully_qualified_name(grassname, mapset));
  52. }
  53. Points = Vect_new_line_struct();
  54. Cats = Vect_new_cats_struct();
  55. top = gpt = (geopoint *) G_malloc(sizeof(geopoint));
  56. if (!top) {
  57. return (NULL);
  58. }
  59. G_get_set_window(&wind);
  60. /* get ndim */
  61. ndim = 2;
  62. if (Vect_is_3d(&map)) {
  63. ndim = 3;
  64. }
  65. /* set rtype */
  66. rtype = CELL_TYPE;
  67. while (eof == 0) {
  68. ltype = Vect_read_next_line(&map, Points, Cats);
  69. switch (ltype) {
  70. case -1:
  71. {
  72. G_warning(_("Unable to read vector map <%s>"),
  73. G_fully_qualified_name(grassname, mapset));
  74. return (NULL);
  75. }
  76. case -2: /* EOF */
  77. {
  78. eof = 1;
  79. continue;
  80. }
  81. }
  82. if ((ltype & GV_POINTS)) {
  83. np++;
  84. gpt->p3[X] = Points->x[0];
  85. gpt->p3[Y] = Points->y[0];
  86. if (ndim > 2) {
  87. *has_z = 1;
  88. gpt->dims = 3;
  89. gpt->p3[Z] = Points->z[0];
  90. }
  91. else {
  92. gpt->dims = 2;
  93. *has_z = 0;
  94. }
  95. /* Store category info for thematic display */
  96. if (Cats->n_cats > 0) {
  97. gpt->cats = Cats;
  98. Cats = Vect_new_cats_struct();
  99. }
  100. else {
  101. gpt->cats = NULL;
  102. Vect_reset_cats(Cats);
  103. }
  104. gpt->highlighted = 0;
  105. G_debug(3, "loading vector point %d %f %f -- %d",
  106. np, Points->x[0], Points->y[0], Cats->n_cats);
  107. gpt->next = (geopoint *) G_malloc(sizeof(geopoint)); /* G_fatal_error */
  108. if (!gpt->next) {
  109. return (NULL);
  110. }
  111. prev = gpt;
  112. gpt = gpt->next;
  113. }
  114. }
  115. if (np > 0) {
  116. prev->next = NULL;
  117. G_free(gpt);
  118. }
  119. Vect_close(&map);
  120. if (!np) {
  121. G_warning(_("No points from vector map <%s> fall within current region"),
  122. G_fully_qualified_name(grassname, mapset));
  123. return (NULL);
  124. }
  125. else {
  126. G_message(_("Vector map <%s> loaded (%d points)"),
  127. G_fully_qualified_name(grassname, mapset), np);
  128. }
  129. *nsites = np;
  130. return (top);
  131. }