gv2.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*!
  2. \file lib/ogsf/gv2.c
  3. \brief OGSF library - loading and manipulating vector sets (higher level functions)
  4. (C) 1999-2008, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Bill Brown USACERL, GMSL/University of Illinois
  8. \author Updated by Martin landa <landa.martin gmail.com>
  9. (doxygenized in May 2008, thematic mapping in June 2011)
  10. */
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <grass/gis.h>
  14. #include <grass/ogsf.h>
  15. #include "gsget.h"
  16. static int Vect_ID[MAX_VECTS];
  17. static int Next_vect = 0;
  18. /*!
  19. \brief Check if vector set exists
  20. \param id vector set id
  21. \return 0 not found
  22. \return 1 found
  23. */
  24. int GV_vect_exists(int id)
  25. {
  26. int i, found = 0;
  27. G_debug(3, "GV_vect_exists");
  28. if (NULL == gv_get_vect(id)) {
  29. return (0);
  30. }
  31. for (i = 0; i < Next_vect && !found; i++) {
  32. if (Vect_ID[i] == id) {
  33. found = 1;
  34. }
  35. }
  36. return (found);
  37. }
  38. /*!
  39. \brief Register new vector set
  40. \return vector set id
  41. \return -1 on error
  42. */
  43. int GV_new_vector(void)
  44. {
  45. geovect *nv;
  46. if (Next_vect < MAX_VECTS) {
  47. nv = gv_get_new_vect();
  48. gv_set_defaults(nv);
  49. Vect_ID[Next_vect] = nv->gvect_id;
  50. ++Next_vect;
  51. G_debug(3, "GV_new_vector(): id=%d", nv->gvect_id);
  52. return nv->gvect_id;
  53. }
  54. return -1;
  55. }
  56. /*!
  57. \brief Get number of available vector sets
  58. \return number of vector sets
  59. */
  60. int GV_num_vects(void)
  61. {
  62. return (gv_num_vects());
  63. }
  64. /*!
  65. \brief Get list of vector sets
  66. Must free when no longer needed!
  67. \param numvects number of vector sets
  68. \return pointer to list of point sets
  69. \return NULL on error
  70. */
  71. int *GV_get_vect_list(int *numvects)
  72. {
  73. int i, *ret;
  74. *numvects = Next_vect;
  75. if (Next_vect) {
  76. ret = (int *)G_malloc(Next_vect * sizeof(int));
  77. if (!ret) {
  78. return (NULL);
  79. }
  80. for (i = 0; i < Next_vect; i++) {
  81. ret[i] = Vect_ID[i];
  82. }
  83. return (ret);
  84. }
  85. return (NULL);
  86. }
  87. /*!
  88. \brief Delete vector set from list
  89. \param id vector set id
  90. \return 1 on success
  91. \return -1 on error
  92. */
  93. int GV_delete_vector(int id)
  94. {
  95. int i, j, found = 0;
  96. G_debug(3, "GV_delete_vect");
  97. if (GV_vect_exists(id)) {
  98. gv_delete_vect(id);
  99. for (i = 0; i < Next_vect && !found; i++) {
  100. if (Vect_ID[i] == id) {
  101. found = 1;
  102. for (j = i; j < Next_vect; j++) {
  103. Vect_ID[j] = Vect_ID[j + 1];
  104. }
  105. }
  106. }
  107. if (found) {
  108. --Next_vect;
  109. return (1);
  110. }
  111. }
  112. return (-1);
  113. }
  114. /*!
  115. \brief Load vector set
  116. Check to see if handle already loaded, if so - free before loading
  117. new for now, always load to memory
  118. \todo Load file handle & ready for reading instead of using
  119. memory
  120. \param id vector set id
  121. \param filename filename
  122. \return -1 on error (invalid vector set id)
  123. \return 1 on success
  124. */
  125. int GV_load_vector(int id, const char *filename)
  126. {
  127. geovect *gv;
  128. if (NULL == (gv = gv_get_vect(id))) {
  129. return (-1);
  130. }
  131. if (gv->lines) {
  132. gv_free_vectmem(gv);
  133. }
  134. gv->filename = G_store(filename);
  135. if ((gv->lines = Gv_load_vect(filename, &(gv->n_lines)))) {
  136. return (1);
  137. }
  138. return (-1);
  139. }
  140. /*!
  141. \brief Get vector map name
  142. Note: char array is allocated by G_store()
  143. \param id vector set id
  144. \param filename &filename
  145. \return -1 on error (invalid vector set id)
  146. \return 1 on success
  147. */
  148. int GV_get_vectname(int id, char **filename)
  149. {
  150. geovect *gv;
  151. if (NULL == (gv = gv_get_vect(id))) {
  152. return (-1);
  153. }
  154. *filename = G_store(gv->filename);
  155. return (1);
  156. }
  157. /*!
  158. \brief Set vector style
  159. \param id vector set id
  160. \param mem non-zero for use memory
  161. \param color color value
  162. \param width line width
  163. \param use_z non-zero for 3d mode
  164. \return -1 on error (invalid vector set id)
  165. \return 1 on success
  166. */
  167. int GV_set_style(int id, int mem, int color, int width, int use_z)
  168. {
  169. geovect *gv;
  170. if (NULL == (gv = gv_get_vect(id))) {
  171. return -1;
  172. }
  173. gv->use_mem = mem;
  174. gv->use_z = use_z;
  175. gv->style->color = color;
  176. gv->style->width = width;
  177. return 1;
  178. }
  179. /*!
  180. \brief Get vector style
  181. \param id vector set id
  182. \param[out] mem non-zero for use memory
  183. \param[out] color color value
  184. \param[out] width line width
  185. \param[out] use_z non-zero for 3d mode
  186. \return -1 on error (invalid vector set id)
  187. \return 1 on success
  188. */
  189. int GV_get_style(int id, int *mem, int *color, int *width, int *use_z)
  190. {
  191. geovect *gv;
  192. if (NULL == (gv = gv_get_vect(id))) {
  193. return -1;
  194. }
  195. *mem = gv->use_mem;
  196. *color = gv->style->color;
  197. *width = gv->style->width;
  198. *use_z = gv->use_z;
  199. return 1;
  200. }
  201. /*!
  202. \brief Set vector set style for thematic mapping
  203. Updates also style for each geoline.
  204. \param id vector set id
  205. \param layer layer number for thematic mapping
  206. \param color color column name
  207. \param width width column name
  208. \param colors pointer to Colors structure or NULL
  209. \return 1 on success
  210. \return -1 on error (point set not found)
  211. */
  212. int GV_set_style_thematic(int id, int layer, const char* color, const char* width,
  213. struct Colors *color_rules)
  214. {
  215. geovect *gv;
  216. if (NULL == (gv = gv_get_vect(id))) {
  217. return -1;
  218. }
  219. if(!gv->tstyle)
  220. gv->tstyle = (gvstyle_thematic *)G_malloc(sizeof(gvstyle_thematic));
  221. G_zero(gv->tstyle, sizeof(gvstyle_thematic));
  222. gv->tstyle->active = 1;
  223. gv->tstyle->layer = layer;
  224. if (color)
  225. gv->tstyle->color_column = G_store(color);
  226. if (width)
  227. gv->tstyle->width_column = G_store(width);
  228. Gv_load_vect_thematic(gv, color_rules);
  229. return 1;
  230. }
  231. /*!
  232. \brief Make style for thematic mapping inactive
  233. \param id vector set id
  234. \return 1 on success
  235. \return -1 on error (point set not found)
  236. */
  237. int GV_unset_style_thematic(int id)
  238. {
  239. geovect *gv;
  240. G_debug(4, "GV_unset_style_thematic(): id=%d", id);
  241. if (NULL == (gv = gv_get_vect(id))) {
  242. return -1;
  243. }
  244. if (gv->tstyle) {
  245. gv->tstyle->active = 0;
  246. }
  247. return 1;
  248. }
  249. /*!
  250. \brief Set trans ?
  251. \param id vector set id
  252. \param xtrans,ytrans,ztrans x/y/z trans values
  253. */
  254. void GV_set_trans(int id, float xtrans, float ytrans, float ztrans)
  255. {
  256. geovect *gv;
  257. G_debug(3, "GV_set_trans");
  258. gv = gv_get_vect(id);
  259. if (gv) {
  260. gv->x_trans = xtrans;
  261. gv->y_trans = ytrans;
  262. gv->z_trans = ztrans;
  263. }
  264. return;
  265. }
  266. /*!
  267. \brief Get trans ?
  268. \param id vector set id
  269. \param[out] xtrans,ytrans,ztrans x/y/z trans values
  270. */
  271. int GV_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
  272. {
  273. geovect *gv;
  274. gv = gv_get_vect(id);
  275. if (gv) {
  276. *xtrans = gv->x_trans;
  277. *ytrans = gv->y_trans;
  278. *ztrans = gv->z_trans;
  279. return (1);
  280. }
  281. return (-1);
  282. }
  283. /*!
  284. \brief Select surface identified by hs to have vector identified
  285. by hv draped over it
  286. \param hv vector set id
  287. \param hs surface id
  288. \return 1 on success
  289. \return -1 on error
  290. */
  291. int GV_select_surf(int hv, int hs)
  292. {
  293. geovect *gv;
  294. if (GV_surf_is_selected(hv, hs)) {
  295. return (1);
  296. }
  297. gv = gv_get_vect(hv);
  298. if (gv && GS_surf_exists(hs)) {
  299. gv->drape_surf_id[gv->n_surfs] = hs;
  300. gv->n_surfs += 1;
  301. return (1);
  302. }
  303. return (-1);
  304. }
  305. /*!
  306. \brief Unselect surface
  307. \param hv vector set id
  308. \param hs surface id
  309. \return 1 on success
  310. \return -1 on error
  311. */
  312. int GV_unselect_surf(int hv, int hs)
  313. {
  314. geovect *gv;
  315. int i, j;
  316. if (!GV_surf_is_selected(hv, hs)) {
  317. return (1);
  318. }
  319. gv = gv_get_vect(hv);
  320. if (gv) {
  321. for (i = 0; i < gv->n_surfs; i++) {
  322. if (gv->drape_surf_id[i] == hs) {
  323. for (j = i; j < gv->n_surfs - 1; j++) {
  324. gv->drape_surf_id[j] = gv->drape_surf_id[j + 1];
  325. }
  326. gv->n_surfs -= 1;
  327. return (1);
  328. }
  329. }
  330. }
  331. return (-1);
  332. }
  333. /*!
  334. \brief Check if surface is selected
  335. \param hv vector set id
  336. \param hs surface id
  337. \return 1 selected
  338. \return 0 not selected
  339. */
  340. int GV_surf_is_selected(int hv, int hs)
  341. {
  342. int i;
  343. geovect *gv;
  344. gv = gv_get_vect(hv);
  345. if (gv) {
  346. for (i = 0; i < gv->n_surfs; i++) {
  347. if (hs == gv->drape_surf_id[i]) {
  348. return (1);
  349. }
  350. }
  351. }
  352. return (0);
  353. }
  354. /*!
  355. \brief Draw vector set
  356. \param vid vector set id
  357. */
  358. void GV_draw_vect(int vid)
  359. {
  360. geosurf *gs;
  361. geovect *gv;
  362. int i;
  363. gv = gv_get_vect(vid);
  364. if (gv) {
  365. for (i = 0; i < gv->n_surfs; i++) {
  366. gs = gs_get_surf(gv->drape_surf_id[i]);
  367. if (gs) {
  368. gvd_vect(gv, gs, 0);
  369. }
  370. }
  371. }
  372. return;
  373. }
  374. /*!
  375. \brief Draw all loaded vector sets
  376. */
  377. void GV_alldraw_vect(void)
  378. {
  379. int id;
  380. for (id = 0; id < Next_vect; id++) {
  381. GV_draw_vect(Vect_ID[id]);
  382. }
  383. return;
  384. }
  385. /*!
  386. \brief Draw vector set (fast mode)
  387. \todo Seems to be broken, nothing is drawn
  388. \param vid vector set id
  389. */
  390. void GV_draw_fastvect(int vid)
  391. {
  392. geosurf *gs;
  393. geovect *gv;
  394. int i;
  395. gv = gv_get_vect(vid);
  396. if (gv) {
  397. for (i = 0; i < gv->n_surfs; i++) {
  398. gs = gs_get_surf(gv->drape_surf_id[i]);
  399. if (gs) {
  400. gvd_vect(gv, gs, 1);
  401. }
  402. }
  403. }
  404. return;
  405. }
  406. /*!
  407. \brief Draw all loaded vector sets (fast mode)
  408. */
  409. void GV_alldraw_fastvect(void)
  410. {
  411. int id;
  412. for (id = 0; id < Next_vect; id++) {
  413. GV_draw_fastvect(Vect_ID[id]);
  414. }
  415. return;
  416. }
  417. /*!
  418. \brief Set client data
  419. \param id vector set id
  420. \param clientd pointer to client data
  421. \return 1 on success
  422. \return -1 on error
  423. */
  424. int GV_Set_ClientData(int id, void *clientd)
  425. {
  426. geovect *gv;
  427. gv = gv_get_vect(id);
  428. if (gv) {
  429. gv->clientdata = clientd;
  430. return (1);
  431. }
  432. return (-1);
  433. }
  434. /*!
  435. \brief Get client data
  436. \param id vector set id
  437. \return pointer to client data
  438. \return NULL on error
  439. */
  440. void *GV_Get_ClientData(int id)
  441. {
  442. geovect *gv;
  443. gv = gv_get_vect(id);
  444. if (gv) {
  445. return (gv->clientdata);
  446. }
  447. return (NULL);
  448. }