gp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*!
  2. \file lib/ogsf/gp.c
  3. \brief OGSF library - loading and manipulating point sets (lower 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 (January 1994)
  8. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  9. */
  10. #include <stdlib.h>
  11. #include <grass/gis.h>
  12. #include <grass/ogsf.h>
  13. #define FIRST_SITE_ID 21720
  14. static geosite *Site_top = NULL;
  15. /*!
  16. \brief Get geosite struct
  17. \param id point set id
  18. \return pointer to geosite struct
  19. \return NULL on failure
  20. */
  21. geosite *gp_get_site(int id)
  22. {
  23. geosite *gp;
  24. G_debug(5, "gp_get_site(%d)", id);
  25. for (gp = Site_top; gp; gp = gp->next) {
  26. if (gp->gsite_id == id) {
  27. return gp;
  28. }
  29. }
  30. return NULL;
  31. }
  32. /*!
  33. \brief Get previous geosite struct from list
  34. \param id point set id
  35. \return pointer to geosite struct
  36. \return NULL on failure
  37. */
  38. geosite *gp_get_prev_site(int id)
  39. {
  40. geosite *pp;
  41. G_debug(5, "gp_get_prev_site(%d)", id);
  42. for (pp = Site_top; pp; pp = pp->next) {
  43. if (pp->gsite_id == id - 1) {
  44. return (pp);
  45. }
  46. }
  47. return NULL;
  48. }
  49. /*!
  50. \brief Get number of loaded point sets
  51. \return number of point sets
  52. */
  53. int gp_num_sites(void)
  54. {
  55. geosite *gp;
  56. int i;
  57. for (i = 0, gp = Site_top; gp; gp = gp->next, i++) ;
  58. G_debug(5, "gp_num_sites(): n=%d", i);
  59. return i;
  60. }
  61. /*!
  62. \brief Get last point set
  63. \return pointer to geosite struct
  64. \return NULL if no point set is available
  65. */
  66. geosite *gp_get_last_site(void)
  67. {
  68. geosite *lp;
  69. G_debug(5, "gp_get_last_site");
  70. if (!Site_top) {
  71. return NULL;
  72. }
  73. for (lp = Site_top; lp->next; lp = lp->next) ;
  74. G_debug(5, " last site id: %d", lp->gsite_id);
  75. return lp;
  76. }
  77. /*!
  78. \brief Create new geosite instance and add it to list
  79. \return pointer to geosite struct
  80. \return NULL on error
  81. */
  82. geosite *gp_get_new_site(void)
  83. {
  84. geosite *np, *lp;
  85. np = (geosite *) G_malloc(sizeof(geosite)); /* G_fatal_error */
  86. if (!np) {
  87. return NULL;
  88. }
  89. G_zero(np, sizeof(geosite));
  90. lp = gp_get_last_site();
  91. if (lp) {
  92. lp->next = np;
  93. np->gsite_id = lp->gsite_id + 1;
  94. }
  95. else {
  96. Site_top = np;
  97. np->gsite_id = FIRST_SITE_ID;
  98. }
  99. np->style = (gvstyle *) G_malloc(sizeof(gvstyle));
  100. if (!np->style)
  101. return NULL;
  102. G_zero(np->style, sizeof (gvstyle));
  103. np->hstyle = (gvstyle *) G_malloc(sizeof(gvstyle));
  104. if (!np->hstyle)
  105. return NULL;
  106. G_zero(np->hstyle, sizeof (gvstyle));
  107. G_debug(5, "gp_get_new_site id=%d", np->gsite_id);
  108. return np;
  109. }
  110. /*!
  111. \brief Update drape surfaces
  112. Call after surface is deleted
  113. */
  114. void gp_update_drapesurfs(void)
  115. {
  116. geosite *gp;
  117. int i, j;
  118. for (gp = Site_top; gp; gp = gp->next) {
  119. if (gp->n_surfs) {
  120. for (i = 0; i < gp->n_surfs; i++) {
  121. if (gp->drape_surf_id[i]) {
  122. if (NULL == gs_get_surf(gp->drape_surf_id[i])) {
  123. for (j = i; j < gp->n_surfs - 1; j++) {
  124. gp->drape_surf_id[j] = gp->drape_surf_id[j + 1];
  125. }
  126. gp->n_surfs = gp->n_surfs - 1;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. return;
  133. }
  134. /*!
  135. \brief Set default value for geosite struct
  136. \param gp pointer to geosite struct
  137. \return 1 on success
  138. \return -1 on failure
  139. */
  140. int gp_set_defaults(geosite * gp)
  141. {
  142. float dim;
  143. if (!gp) {
  144. return -1;
  145. }
  146. G_debug(5, "gp_set_defaults() id=%d", gp->gsite_id);
  147. GS_get_longdim(&dim);
  148. gp->style->color = 0xF0F0F0;
  149. gp->style->size = dim / 100.;
  150. gp->style->width = 1;
  151. gp->style->symbol = ST_X;
  152. gp->hstyle->color = 0xFF0000;
  153. gp->hstyle->size = dim / 150.;
  154. gp->hstyle->symbol = ST_X;
  155. gp->tstyle = NULL;
  156. return 1;
  157. }
  158. /*!
  159. \brief Initialize geosite struct
  160. \todo Currently does nothing
  161. \param gp pointer to geosite struct
  162. \return -1 on failure
  163. \return 0 on success
  164. */
  165. int gp_init_site(geosite * gp)
  166. {
  167. G_debug(5, "gp_init_site");
  168. if (!gp) {
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. /*!
  174. \brief Delete point set and remove from list
  175. \param id point set id
  176. */
  177. void gp_delete_site(int id)
  178. {
  179. geosite *fp;
  180. G_debug(5, "gp_delete_site");
  181. fp = gp_get_site(id);
  182. if (fp) {
  183. gp_free_site(fp);
  184. }
  185. return;
  186. }
  187. /*!
  188. \brief Free allocated geosite struct
  189. \param fp pointer to geosite struct
  190. \return 1 on success
  191. \return -1 on failure
  192. */
  193. int gp_free_site(geosite * fp)
  194. {
  195. geosite *gp;
  196. int found = 0;
  197. G_debug(5, "gp_free_site(id=%d)", fp->gsite_id);
  198. if (Site_top) {
  199. if (fp == Site_top) {
  200. if (Site_top->next) {
  201. /* can't free top if last */
  202. found = 1;
  203. Site_top = fp->next;
  204. }
  205. else {
  206. gp_free_sitemem(fp);
  207. G_free(fp);
  208. Site_top = NULL;
  209. }
  210. }
  211. else {
  212. for (gp = Site_top; gp && !found; gp = gp->next) {
  213. /* can't free top */
  214. if (gp->next) {
  215. if (gp->next == fp) {
  216. found = 1;
  217. gp->next = fp->next;
  218. }
  219. }
  220. }
  221. }
  222. if (found) {
  223. gp_free_sitemem(fp);
  224. G_free(fp);
  225. fp = NULL;
  226. }
  227. return (1);
  228. }
  229. return -1;
  230. }
  231. /*!
  232. \brief Free geosite (lower level)
  233. \param fp pointer to geosite struct
  234. */
  235. void gp_free_sitemem(geosite * fp)
  236. {
  237. geopoint *gpt, *tmp;
  238. G_free((void *)fp->filename);
  239. fp->filename = NULL;
  240. if (fp->style) {
  241. G_free(fp->style);
  242. }
  243. if (fp->hstyle) {
  244. G_free(fp->hstyle);
  245. }
  246. if (fp->points) {
  247. for (gpt = fp->points; gpt;) {
  248. G_free(gpt->cats);
  249. if(gpt->style) {
  250. G_free(gpt->style);
  251. }
  252. tmp = gpt;
  253. gpt = gpt->next;
  254. G_free(tmp);
  255. }
  256. fp->n_sites = 0;
  257. fp->points = NULL;
  258. }
  259. if (fp->tstyle) {
  260. G_free(fp->tstyle->color_column);
  261. G_free(fp->tstyle->symbol_column);
  262. G_free(fp->tstyle->size_column);
  263. G_free(fp->tstyle->width_column);
  264. }
  265. return;
  266. }
  267. /*!
  268. \brief Set drape surfaces
  269. \param gp pointer to geosite struct
  270. \param hsurf list of surfaces (id)
  271. \param nsurf number of surfaces
  272. */
  273. void gp_set_drapesurfs(geosite * gp, int hsurfs[], int nsurfs)
  274. {
  275. int i;
  276. for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
  277. gp->drape_surf_id[i] = hsurfs[i];
  278. }
  279. return;
  280. }