gp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/gstypes.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. G_debug(5, "gp_get_new_site");
  86. np = (geosite *) G_malloc(sizeof(geosite)); /* G_fatal_error */
  87. if (!np) {
  88. return NULL;
  89. }
  90. G_zero(np, sizeof(geosite));
  91. lp = gp_get_last_site();
  92. if (lp) {
  93. lp->next = np;
  94. np->gsite_id = lp->gsite_id + 1;
  95. }
  96. else {
  97. Site_top = np;
  98. np->gsite_id = FIRST_SITE_ID;
  99. }
  100. np->style = (gvstyle *) G_malloc(sizeof(gvstyle));
  101. if (!np->style)
  102. return NULL;
  103. G_zero(np->style, sizeof (gvstyle));
  104. np->hstyle = (gvstyle *) G_malloc(sizeof(gvstyle));
  105. if (!np->hstyle)
  106. return NULL;
  107. G_zero(np->hstyle, sizeof (gvstyle));
  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. G_debug(5, "gp_set_defaults");
  144. if (!gp) {
  145. return -1;
  146. }
  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. return 1;
  156. }
  157. /*!
  158. \brief Initialize geosite struct
  159. \todo Currently does nothing
  160. \param gp pointer to geosite struct
  161. \return -1 on failure
  162. \return 0 on success
  163. */
  164. int gp_init_site(geosite * gp)
  165. {
  166. G_debug(5, "gp_init_site");
  167. if (!gp) {
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. /*!
  173. \brief Delete point set and remove from list
  174. \param id point set id
  175. */
  176. void gp_delete_site(int id)
  177. {
  178. geosite *fp;
  179. G_debug(5, "gp_delete_site");
  180. fp = gp_get_site(id);
  181. if (fp) {
  182. gp_free_site(fp);
  183. }
  184. return;
  185. }
  186. /*!
  187. \brief Free allocated geosite struct
  188. \param fp pointer to geosite struct
  189. \return 1 on success
  190. \return -1 on failure
  191. */
  192. int gp_free_site(geosite * fp)
  193. {
  194. geosite *gp;
  195. int found = 0;
  196. G_debug(5, "gp_free_site(id=%d)", fp->gsite_id);
  197. if (Site_top) {
  198. if (fp == Site_top) {
  199. if (Site_top->next) {
  200. /* can't free top if last */
  201. found = 1;
  202. Site_top = fp->next;
  203. }
  204. else {
  205. gp_free_sitemem(fp);
  206. G_free(fp);
  207. Site_top = NULL;
  208. }
  209. }
  210. else {
  211. for (gp = Site_top; gp && !found; gp = gp->next) {
  212. /* can't free top */
  213. if (gp->next) {
  214. if (gp->next == fp) {
  215. found = 1;
  216. gp->next = fp->next;
  217. }
  218. }
  219. }
  220. }
  221. if (found) {
  222. gp_free_sitemem(fp);
  223. G_free(fp);
  224. fp = NULL;
  225. }
  226. return (1);
  227. }
  228. return -1;
  229. }
  230. /*!
  231. \brief Free geosite (lower level)
  232. \param fp pointer to geosite struct
  233. */
  234. void gp_free_sitemem(geosite * fp)
  235. {
  236. geopoint *gpt, *tmp;
  237. G_free((void *)fp->filename);
  238. fp->filename = NULL;
  239. if (fp->style) {
  240. G_free(fp->style);
  241. }
  242. if (fp->hstyle) {
  243. G_free(fp->hstyle);
  244. }
  245. if (fp->points) {
  246. for (gpt = fp->points; gpt;) {
  247. G_free(gpt->cats);
  248. if(gpt->style) {
  249. G_free(gpt->style);
  250. }
  251. tmp = gpt;
  252. gpt = gpt->next;
  253. G_free(tmp);
  254. }
  255. fp->n_sites = 0;
  256. fp->points = NULL;
  257. }
  258. if(fp->tstyle) {
  259. G_free(fp->tstyle->color_column);
  260. G_free(fp->tstyle->symbol_column);
  261. G_free(fp->tstyle->size_column);
  262. G_free(fp->tstyle->width_column);
  263. }
  264. return;
  265. }
  266. /*!
  267. \brief Set drape surfaces
  268. \param gp pointer to geosite struct
  269. \param hsurf list of surfaces (id)
  270. \param nsurf number of surfaces
  271. */
  272. void gp_set_drapesurfs(geosite * gp, int hsurfs[], int nsurfs)
  273. {
  274. int i;
  275. for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
  276. gp->drape_surf_id[i] = hsurfs[i];
  277. }
  278. return;
  279. }