gp3.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*!
  2. \file lib/ogsf/gp3.c
  3. \brief OGSF library - loading point sets (lower level functions)
  4. GRASS OpenGL gsurf OGSF Library
  5. (C) 1999-2008, 2011 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Bill Brown USACERL, GMSL/University of Illinois (January 1994)
  9. \author Updated by Martin Landa <landa.martin gmail.com>
  10. (doxygenized in May 2008, thematic mapping in June 2011)
  11. */
  12. #include <stdlib.h>
  13. #include <grass/gis.h>
  14. #include <grass/colors.h>
  15. #include <grass/raster.h>
  16. #include <grass/vector.h>
  17. #include <grass/dbmi.h>
  18. #include <grass/glocale.h>
  19. #include <grass/ogsf.h>
  20. /*!
  21. \brief Load to points to memory
  22. The other alternative may be to load to a tmp file.
  23. \param name name of vector map to be loaded
  24. \param[out] nsites number of loaded points
  25. \param[out] has_z 2D or 3D points data loaded?
  26. \return pointer to geopoint struct (array)
  27. \return NULL on failure
  28. */
  29. geopoint *Gp_load_sites(const char *name, 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. int ndim;
  38. const char *mapset;
  39. np = 0;
  40. eof = 0;
  41. mapset = G_find_vector2(name, "");
  42. if (!mapset) {
  43. G_warning(_("Vector map <%s> not found"), name);
  44. return NULL;
  45. }
  46. Vect_set_open_level(1);
  47. if (Vect_open_old(&map, name, "") == -1) {
  48. G_fatal_error(_("Unable to open vector map <%s>"),
  49. G_fully_qualified_name(name, mapset));
  50. }
  51. Points = Vect_new_line_struct();
  52. Cats = Vect_new_cats_struct();
  53. top = gpt = (geopoint *) G_malloc(sizeof(geopoint));
  54. G_zero(gpt, sizeof(geopoint));
  55. if (!top) {
  56. return NULL;
  57. }
  58. G_get_set_window(&wind);
  59. Vect_set_constraint_region(&map, wind.north, wind.south, wind.east,
  60. wind.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
  61. /* get ndim */
  62. *has_z = 0;
  63. ndim = 2;
  64. if (Vect_is_3d(&map)) {
  65. *has_z = 1;
  66. ndim = 3;
  67. }
  68. while (eof == 0) {
  69. ltype = Vect_read_next_line(&map, Points, Cats);
  70. switch (ltype) {
  71. case -1:
  72. {
  73. G_warning(_("Unable to read vector map <%s>"),
  74. G_fully_qualified_name(name, mapset));
  75. return NULL;
  76. }
  77. case -2: /* EOF */
  78. {
  79. eof = 1;
  80. continue;
  81. }
  82. }
  83. if ((ltype & GV_POINTS)) {
  84. np++;
  85. gpt->p3[X] = Points->x[0];
  86. gpt->p3[Y] = Points->y[0];
  87. if (ndim > 2) {
  88. gpt->dims = 3;
  89. gpt->p3[Z] = Points->z[0];
  90. }
  91. else {
  92. gpt->dims = 2;
  93. }
  94. /* Store category info for thematic display */
  95. if (Cats->n_cats > 0) {
  96. gpt->cats = Cats;
  97. Cats = Vect_new_cats_struct();
  98. }
  99. else {
  100. Vect_reset_cats(Cats);
  101. }
  102. /* initialize style */
  103. gpt->highlighted = 0;
  104. G_debug(5, "loading vector point %d x=%f y=%f ncats=%d",
  105. np, Points->x[0], Points->y[0], Cats->n_cats);
  106. gpt->next = (geopoint *) G_malloc(sizeof(geopoint)); /* G_fatal_error */
  107. G_zero(gpt->next, sizeof(geopoint));
  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(name, mapset));
  123. return (NULL);
  124. }
  125. else {
  126. G_message(_("Vector map <%s> loaded (%d points)"),
  127. G_fully_qualified_name(name, mapset), np);
  128. }
  129. *nsites = np;
  130. return top;
  131. }
  132. /*!
  133. \brief Load styles for geopoints based on thematic mapping
  134. \param gp pointer to geosite structure
  135. \param colors pointer to Colors structure or NULL
  136. \return number of points defined by thematic mapping
  137. \return -1 on error
  138. */
  139. int Gp_load_sites_thematic(geosite *gp, struct Colors *colors)
  140. {
  141. geopoint *gpt;
  142. struct Map_info Map;
  143. struct field_info *Fi;
  144. int nvals, cat, npts, nskipped;
  145. int red, blu, grn;
  146. const char *str;
  147. const char *mapset;
  148. dbDriver *driver;
  149. dbValue value;
  150. if(!gp || !gp->tstyle || !gp->filename)
  151. return -1;
  152. mapset = G_find_vector2(gp->filename, "");
  153. if (!mapset) {
  154. G_fatal_error(_("Vector map <%s> not found"), gp->filename);
  155. }
  156. Vect_set_open_level(1);
  157. if (Vect_open_old(&Map, gp->filename, "") == -1) {
  158. G_fatal_error(_("Unable to open vector map <%s>"),
  159. G_fully_qualified_name(gp->filename, mapset));
  160. }
  161. Fi = Vect_get_field(&Map, gp->tstyle->layer);
  162. if (!Fi) {
  163. G_warning(_("Database connection not defined for layer %d"),
  164. gp->tstyle->layer);
  165. }
  166. else {
  167. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  168. if (!driver)
  169. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  170. Fi->database, Fi->driver);
  171. }
  172. G_message(_("Loading thematic points layer <%s>..."),
  173. G_fully_qualified_name(gp->filename, mapset));
  174. npts = nskipped = 0;
  175. for(gpt = gp->points; gpt; gpt = gpt->next) {
  176. gpt->style = (gvstyle *) G_malloc(sizeof(gvstyle));
  177. G_zero(gpt->style, sizeof(gvstyle));
  178. /* use default style */
  179. gpt->style->color = gp->style->color;
  180. gpt->style->symbol = gp->style->symbol;
  181. gpt->style->size = gp->style->size;
  182. gpt->style->width = gp->style->width;
  183. cat = -1;
  184. if (gpt->cats)
  185. Vect_cat_get(gpt->cats, gp->tstyle->layer, &cat);
  186. if (cat < 0) {
  187. nskipped++;
  188. continue;
  189. }
  190. /* color */
  191. if (colors) {
  192. if (!Rast_get_c_color((const CELL *) &cat, &red, &grn, &blu, colors)) {
  193. G_warning(_("No color rule defined for category %d"), cat);
  194. gpt->style->color = gp->style->color;
  195. }
  196. gpt->style->color = (red & RED_MASK) + ((int)((grn) << 8) & GRN_MASK) +
  197. ((int)((blu) << 16) & BLU_MASK);
  198. }
  199. if (gp->tstyle->color_column) {
  200. nvals = db_select_value(driver, Fi->table, Fi->key, cat, gp->tstyle->color_column, &value);
  201. if (nvals < 1)
  202. continue;
  203. str = db_get_value_string(&value);
  204. if (!str)
  205. continue;
  206. if (G_str_to_color(str, &red, &grn, &blu) != 1) {
  207. G_warning(_("Invalid color definition (%s)"),
  208. str);
  209. gpt->style->color = gp->style->color;
  210. }
  211. else {
  212. gpt->style->color = (red & RED_MASK) + ((int)((grn) << 8) & GRN_MASK) +
  213. ((int)((blu) << 16) & BLU_MASK);
  214. }
  215. }
  216. /* size */
  217. if (gp->tstyle->size_column) {
  218. nvals = db_select_value(driver, Fi->table, Fi->key, cat, gp->tstyle->size_column, &value);
  219. if (nvals < 1)
  220. continue;
  221. gpt->style->size = db_get_value_int(&value);
  222. }
  223. /* width */
  224. if (gp->tstyle->width_column) {
  225. nvals = db_select_value(driver, Fi->table, Fi->key, cat, gp->tstyle->width_column, &value);
  226. if (nvals < 1)
  227. continue;
  228. gpt->style->width = db_get_value_int(&value);
  229. }
  230. /* symbol/marker */
  231. if (gp->tstyle->symbol_column) {
  232. nvals = db_select_value(driver, Fi->table, Fi->key, cat, gp->tstyle->symbol_column, &value);
  233. if (nvals < 1)
  234. continue;
  235. str = db_get_value_string(&value);
  236. gpt->style->symbol = GP_str_to_marker(str);
  237. }
  238. npts++;
  239. }
  240. if (nskipped > 0)
  241. G_warning(_("%d points without category. "
  242. "Unable to determine color rules for features without category."),
  243. nskipped);
  244. return npts;
  245. }