vector.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*!
  2. \file vector.c
  3. \brief Vector subroutines
  4. (C) 2008-2013 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. \author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <grass/vector.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/glocale.h>
  15. #include "local_proto.h"
  16. static int load_vectors(const struct Option *, const struct Option *,
  17. const struct Option *, const struct Option *, int, nv_data *);
  18. static void error_handler_vector(void *);
  19. static void error_handler_db(void *);
  20. /*!
  21. \brief Load vector maps (lines)
  22. \param params module parameters
  23. \param data nviz data
  24. \return number of loaded vectors
  25. */
  26. int load_vlines(const struct GParams *params, nv_data * data)
  27. {
  28. return load_vectors(params->elev_map, params->elev_const,
  29. params->vlines, params->vline_pos,
  30. MAP_OBJ_VECT, data);
  31. }
  32. /*!
  33. \brief Load vector maps (points)
  34. \param params module parameters
  35. \param data nviz data
  36. \return number of loaded vectors
  37. */
  38. int load_vpoints(const struct GParams *params, nv_data * data)
  39. {
  40. return load_vectors(params->elev_map, params->elev_const,
  41. params->vpoints, params->vpoint_pos,
  42. MAP_OBJ_SITE, data);
  43. }
  44. int load_vectors(const struct Option *elev_map,
  45. const struct Option *elev_const, const struct Option *vect,
  46. const struct Option *position,
  47. int map_obj_type, nv_data * data)
  48. {
  49. int i, id;
  50. int nvects;
  51. const char *mapset;
  52. double x, y, z;
  53. if ((!elev_map->answer || elev_const->answer) && GS_num_surfs() == 0) { /* load base surface if no loaded */
  54. int *surf_list, nsurf;
  55. Nviz_new_map_obj(MAP_OBJ_SURF, NULL, 0.0, data);
  56. surf_list = GS_get_surf_list(&nsurf);
  57. GS_set_att_const(surf_list[0], ATT_TRANSP, 255);
  58. }
  59. nvects = 0;
  60. for (i = 0; vect->answers[i]; i++) {
  61. mapset = G_find_vector2(vect->answers[i], "");
  62. if (mapset == NULL) {
  63. G_fatal_error(_("Vector map <%s> not found"), vect->answers[i]);
  64. }
  65. id = Nviz_new_map_obj(map_obj_type,
  66. G_fully_qualified_name(vect->answers[i], mapset),
  67. 0.0, data);
  68. /* set position */
  69. x = atof(position->answers[i*3+0]);
  70. y = atof(position->answers[i*3+1]);
  71. z = atof(position->answers[i*3+2]);
  72. if (map_obj_type == MAP_OBJ_VECT)
  73. GV_set_trans(id, x, y, z);
  74. else
  75. GP_set_trans(id, x, y, z);
  76. nvects++;
  77. }
  78. return nvects;
  79. }
  80. /*!
  81. \brief Set vector lines mode
  82. \param params parameters
  83. \return 1 on success
  84. \return 0 on failure
  85. */
  86. int vlines_set_attrb(const struct GParams *params)
  87. {
  88. int i, layer, color, width, flat, height;
  89. int *vect_list, nvects;
  90. int have_colors;
  91. char *color_column, *width_column;
  92. struct Colors colors;
  93. vect_list = GV_get_vect_list(&nvects);
  94. for (i = 0; i < nvects; i++) {
  95. check_map(params, i, TRUE, &layer, NULL);
  96. color = Nviz_color_from_str(params->vline_color->answers[i]);
  97. color_column = params->vline_color_column->answers ?
  98. params->vline_color_column->answers[i] : NULL;
  99. width = atoi(params->vline_width->answers[i]);
  100. width_column = params->vline_width_column->answers ?
  101. params->vline_width_column->answers[i] : NULL;
  102. if (strcmp(params->vline_mode->answers[i], "flat") == 0)
  103. flat = 1;
  104. else
  105. flat = 0;
  106. /* style (mode -- use memory by default) */
  107. if (GV_set_style(vect_list[i], TRUE, color, width, flat) < 0)
  108. return 0;
  109. /* check for vector color table */
  110. have_colors = Vect_read_colors(params->vlines->answers[i], "",
  111. &colors);
  112. if (have_colors || color_column || width_column)
  113. if (GV_set_style_thematic(vect_list[i], layer, color_column,
  114. width_column, have_colors ? &colors : NULL) < 0)
  115. return 0;
  116. /* height */
  117. height = atoi(params->vline_height->answers[i]);
  118. if (height > 0)
  119. GV_set_trans(vect_list[i], 0.0, 0.0, height);
  120. }
  121. return 1;
  122. }
  123. /*!
  124. \brief Set vector points style
  125. \param params parameters
  126. \return 1 on success
  127. \return 0 on failure
  128. */
  129. int vpoints_set_attrb(const struct GParams *params)
  130. {
  131. int i, layer, have_colors, with_z;
  132. int *site_list, nsites;
  133. int marker, color, width;
  134. float size;
  135. char *marker_str, *color_column, *size_column, *width_column, *marker_column;
  136. struct Colors colors;
  137. site_list = GP_get_site_list(&nsites);
  138. for (i = 0; i < nsites; i++) {
  139. check_map(params, i, FALSE, &layer, &with_z);
  140. color = Nviz_color_from_str(params->vpoint_color->answers[i]);
  141. color_column = params->vpoint_color_column->answers ?
  142. params->vpoint_color_column->answers[i] : NULL;
  143. size = atof(params->vpoint_size->answers[i]);
  144. size_column = params->vpoint_size_column->answers ?
  145. params->vpoint_size_column->answers[i] : NULL;
  146. width = atoi(params->vpoint_width->answers[i]);
  147. width_column = params->vpoint_width_column->answers ?
  148. params->vpoint_width_column->answers[i] : NULL;
  149. marker_str = params->vpoint_marker->answers[i];
  150. marker_column = params->vpoint_marker_column->answers ?
  151. params->vpoint_marker_column->answers[i] : NULL;
  152. marker = GP_str_to_marker(marker_str);
  153. if (with_z) {
  154. if (strcmp(params->vpoint_mode->answers[i], "surface") == 0)
  155. GP_set_zmode(site_list[i], FALSE);
  156. else
  157. GP_set_zmode(site_list[i], TRUE);
  158. }
  159. if (GP_set_style(site_list[i], color, width, size, marker) < 0)
  160. return 0;
  161. /* check for vector color table */
  162. have_colors = Vect_read_colors(params->vpoints->answers[i], "",
  163. &colors);
  164. if (have_colors || color_column || width_column ||
  165. size_column || marker_column) {
  166. if (GP_set_style_thematic(site_list[i], layer, color_column,
  167. width_column, size_column, marker_column,
  168. have_colors ? &colors : NULL) < 0)
  169. return 0;
  170. }
  171. }
  172. return 1;
  173. }
  174. /*!
  175. \brief Check vector map
  176. \param params parameters
  177. \param index answers array index
  178. \param vlines TRUE for lines otherwise points
  179. \param[out] field number
  180. \param[out] WITH_Z for 3D maps
  181. \return 0 on success otherwise 1
  182. */
  183. int check_map(const struct GParams *params, int index, int vlines,
  184. int *field, int *with_z)
  185. {
  186. int type;
  187. struct Map_info Map;
  188. const char *map, *layer, *color, *size, *width, *marker;
  189. struct field_info *Fi;
  190. dbDriver *driver;
  191. dbColumn *column;
  192. Fi = NULL;
  193. driver = NULL;
  194. if (vlines) {
  195. map = params->vlines->answers[index];
  196. layer = params->vline_layer->answers[index];
  197. color = params->vline_color_column->answers ?
  198. params->vline_color_column->answers[index] : NULL;
  199. size = NULL;
  200. width = params->vline_width_column->answers ?
  201. params->vline_width_column->answers[index] : NULL;
  202. marker = NULL;
  203. }
  204. else {
  205. map = params->vpoints->answers[index];
  206. layer = params->vpoint_layer->answers[index];
  207. color = params->vpoint_color_column->answers ?
  208. params->vpoint_color_column->answers[index] : NULL;
  209. size = params->vpoint_size_column->answers ?
  210. params->vpoint_size_column->answers[index] : NULL;
  211. width = params->vpoint_width_column->answers ?
  212. params->vpoint_width_column->answers[index] : NULL;
  213. marker = params->vpoint_marker_column->answers ?
  214. params->vpoint_marker_column->answers[index] : NULL;
  215. }
  216. if (!map)
  217. return 1;
  218. if (1 > Vect_open_old(&Map, map, ""))
  219. G_fatal_error(_("Unable to open vector map <%s>"), map);
  220. G_add_error_handler(error_handler_vector, &Map);
  221. if (with_z)
  222. *with_z = Vect_is_3d(&Map);
  223. *field = -1;
  224. Fi = Vect_get_field2(&Map, layer);
  225. if (Fi) {
  226. *field = Fi->number;
  227. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  228. if (!driver)
  229. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  230. Fi->database, Fi->driver);
  231. G_add_error_handler(error_handler_db, driver);
  232. if (color) {
  233. db_get_column(driver, Fi->table, color, &column);
  234. if (!column)
  235. G_fatal_error(_("Column <%s> in table <%s> not found"),
  236. color, Fi->table);
  237. if (db_column_Ctype(driver, Fi->table, color) != DB_C_TYPE_STRING)
  238. G_fatal_error(_("Data type of color column must be character"));
  239. }
  240. if (size) {
  241. db_get_column(driver, Fi->table, size, &column);
  242. if (!column)
  243. G_fatal_error(_("Column <%s> in table <%s> not found"),
  244. size, Fi->table);
  245. type = db_column_Ctype(driver, Fi->table, size);
  246. if (type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE)
  247. G_fatal_error(_("Data type of size column must be numeric"));
  248. }
  249. if (width) {
  250. db_get_column(driver, Fi->table, width, &column);
  251. if (!column)
  252. G_fatal_error(_("Column <%s> in table <%s> not found"),
  253. width, Fi->table);
  254. type = db_column_Ctype(driver, Fi->table, width);
  255. if (type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE)
  256. G_fatal_error(_("Data type of width column must be numeric"));
  257. }
  258. if (marker) {
  259. db_get_column(driver, Fi->table, marker, &column);
  260. if (!column)
  261. G_fatal_error(_("Column <%s> in table <%s> not found"),
  262. marker, Fi->table);
  263. type = db_column_Ctype(driver, Fi->table, marker);
  264. if (db_column_Ctype(driver, Fi->table, marker) != DB_C_TYPE_STRING)
  265. G_fatal_error(_("Data type of marker column must be character"));
  266. }
  267. G_remove_error_handler(error_handler_db, driver);
  268. db_close_database_shutdown_driver(driver);
  269. }
  270. G_remove_error_handler(error_handler_vector, &Map);
  271. Vect_close(&Map);
  272. return 0;
  273. }
  274. void error_handler_vector(void *p)
  275. {
  276. struct Map_info *Map;
  277. Map = (struct Map_info *)p;
  278. Vect_close(Map);
  279. }
  280. void error_handler_db(void *p)
  281. {
  282. dbDriver *driver;
  283. driver = (dbDriver *)p;
  284. if (driver)
  285. db_close_database_shutdown_driver(driver);
  286. }