GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-835afb4352
gp.c
Go to the documentation of this file.
1 /*!
2  \file lib/ogsf/gp.c
3 
4  \brief OGSF library - loading and manipulating point sets (lower level
5  functions)
6 
7  (C) 1999-2008, 2011 by the GRASS Development Team
8 
9  This program is free software under the GNU General Public License
10  (>=v2). Read the file COPYING that comes with GRASS for details.
11 
12  \author Bill Brown USACERL, GMSL/University of Illinois (January 1994)
13  \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
14  */
15 
16 #include <stdlib.h>
17 
18 #include <grass/gis.h>
19 #include <grass/ogsf.h>
20 
21 #define FIRST_SITE_ID 21720
22 
23 static geosite *Site_top = NULL;
24 
25 /*!
26  \brief Get geosite struct
27 
28  \param id point set id
29 
30  \return pointer to geosite struct
31  \return NULL on failure
32  */
34 {
35  geosite *gp;
36 
37  G_debug(5, "gp_get_site(%d)", id);
38 
39  for (gp = Site_top; gp; gp = gp->next) {
40  if (gp->gsite_id == id) {
41  return gp;
42  }
43  }
44 
45  return NULL;
46 }
47 
48 /*!
49  \brief Get previous geosite struct from list
50 
51  \param id point set id
52 
53  \return pointer to geosite struct
54  \return NULL on failure
55  */
57 {
58  geosite *pp;
59 
60  G_debug(5, "gp_get_prev_site(%d)", id);
61 
62  for (pp = Site_top; pp; pp = pp->next) {
63  if (pp->gsite_id == id - 1) {
64  return (pp);
65  }
66  }
67 
68  return NULL;
69 }
70 
71 /*!
72  \brief Get number of loaded point sets
73 
74  \return number of point sets
75  */
76 int gp_num_sites(void)
77 {
78  geosite *gp;
79  int i;
80 
81  for (i = 0, gp = Site_top; gp; gp = gp->next, i++)
82  ;
83 
84  G_debug(5, "gp_num_sites(): n=%d", i);
85 
86  return i;
87 }
88 
89 /*!
90  \brief Get last point set
91 
92  \return pointer to geosite struct
93  \return NULL if no point set is available
94  */
96 {
97  geosite *lp;
98 
99  G_debug(5, "gp_get_last_site");
100 
101  if (!Site_top) {
102  return NULL;
103  }
104 
105  for (lp = Site_top; lp->next; lp = lp->next)
106  ;
107 
108  G_debug(5, " last site id: %d", lp->gsite_id);
109 
110  return lp;
111 }
112 
113 /*!
114  \brief Create new geosite instance and add it to list
115 
116  \return pointer to geosite struct
117  \return NULL on error
118  */
120 {
121  geosite *np, *lp;
122 
123  np = (geosite *)G_malloc(sizeof(geosite)); /* G_fatal_error */
124  if (!np) {
125  return NULL;
126  }
127  G_zero(np, sizeof(geosite));
128 
129  lp = gp_get_last_site();
130  if (lp) {
131  lp->next = np;
132  np->gsite_id = lp->gsite_id + 1;
133  }
134  else {
135  Site_top = np;
136  np->gsite_id = FIRST_SITE_ID;
137  }
138  np->style = (gvstyle *)G_malloc(sizeof(gvstyle));
139  if (!np->style)
140  return NULL;
141  G_zero(np->style, sizeof(gvstyle));
142  np->hstyle = (gvstyle *)G_malloc(sizeof(gvstyle));
143  if (!np->hstyle)
144  return NULL;
145  G_zero(np->hstyle, sizeof(gvstyle));
146 
147  G_debug(5, "gp_get_new_site id=%d", np->gsite_id);
148 
149  return np;
150 }
151 
152 /*!
153  \brief Update drape surfaces
154 
155  Call after surface is deleted
156  */
158 {
159  geosite *gp;
160  int i, j;
161 
162  for (gp = Site_top; gp; gp = gp->next) {
163  if (gp->n_surfs) {
164  for (i = 0; i < gp->n_surfs; i++) {
165  if (gp->drape_surf_id[i]) {
166  if (NULL == gs_get_surf(gp->drape_surf_id[i])) {
167  for (j = i; j < gp->n_surfs - 1; j++) {
168  gp->drape_surf_id[j] = gp->drape_surf_id[j + 1];
169  }
170 
171  gp->n_surfs = gp->n_surfs - 1;
172  }
173  }
174  }
175  }
176  }
177 
178  return;
179 }
180 
181 /*!
182  \brief Set default value for geosite struct
183 
184  \param gp pointer to geosite struct
185 
186  \return 1 on success
187  \return -1 on failure
188  */
190 {
191  float dim;
192 
193  if (!gp) {
194  return -1;
195  }
196  G_debug(5, "gp_set_defaults() id=%d", gp->gsite_id);
197 
198  GS_get_longdim(&dim);
199 
200  gp->style->color = 0xF0F0F0;
201  gp->style->size = dim / 100.;
202  gp->style->width = 1;
203  gp->style->symbol = ST_X;
204  gp->hstyle->color = 0xFF0000;
205  gp->hstyle->size = dim / 150.;
206  gp->hstyle->symbol = ST_X;
207  gp->tstyle = NULL;
208 
209  return 1;
210 }
211 
212 /*!
213  \brief Initialize geosite struct
214 
215  \todo Currently does nothing
216 
217  \param gp pointer to geosite struct
218 
219  \return -1 on failure
220  \return 0 on success
221  */
223 {
224  G_debug(5, "gp_init_site");
225 
226  if (!gp) {
227  return -1;
228  }
229 
230  return 0;
231 }
232 
233 /*!
234  \brief Delete point set and remove from list
235 
236  \param id point set id
237  */
238 void gp_delete_site(int id)
239 {
240  geosite *fp;
241 
242  G_debug(5, "gp_delete_site");
243 
244  fp = gp_get_site(id);
245 
246  if (fp) {
247  gp_free_site(fp);
248  }
249 
250  return;
251 }
252 
253 /*!
254  \brief Free allocated geosite struct
255 
256  \param fp pointer to geosite struct
257 
258  \return 1 on success
259  \return -1 on failure
260  */
262 {
263  geosite *gp;
264  int found = 0;
265 
266  G_debug(5, "gp_free_site(id=%d)", fp->gsite_id);
267 
268  if (Site_top) {
269  if (fp == Site_top) {
270  if (Site_top->next) {
271  /* can't free top if last */
272  found = 1;
273  Site_top = fp->next;
274  }
275  else {
276  gp_free_sitemem(fp);
277  G_free(fp);
278  Site_top = NULL;
279  }
280  }
281  else {
282  for (gp = Site_top; gp && !found; gp = gp->next) {
283  /* can't free top */
284  if (gp->next) {
285  if (gp->next == fp) {
286  found = 1;
287  gp->next = fp->next;
288  }
289  }
290  }
291  }
292 
293  if (found) {
294  gp_free_sitemem(fp);
295  G_free(fp);
296  fp = NULL;
297  }
298 
299  return (1);
300  }
301 
302  return -1;
303 }
304 
305 /*!
306  \brief Free geosite (lower level)
307 
308  \param fp pointer to geosite struct
309  */
311 {
312  geopoint *gpt, *tmp;
313 
314  G_free((void *)fp->filename);
315  fp->filename = NULL;
316  if (fp->style) {
317  G_free(fp->style);
318  }
319  if (fp->hstyle) {
320  G_free(fp->hstyle);
321  }
322  if (fp->points) {
323  for (gpt = fp->points; gpt;) {
324  G_free(gpt->cats);
325  if (gpt->style) {
326  G_free(gpt->style);
327  }
328 
329  tmp = gpt;
330  gpt = gpt->next;
331  G_free(tmp);
332  }
333 
334  fp->n_sites = 0;
335  fp->points = NULL;
336  }
337 
338  if (fp->tstyle) {
339  G_free(fp->tstyle->color_column);
341  G_free(fp->tstyle->size_column);
342  G_free(fp->tstyle->width_column);
343  }
344 
345  return;
346 }
347 
348 /*!
349  \brief Set drape surfaces
350 
351  \param gp pointer to geosite struct
352  \param hsurf list of surfaces (id)
353  \param nsurf number of surfaces
354  */
355 void gp_set_drapesurfs(geosite *gp, int hsurfs[], int nsurfs)
356 {
357  int i;
358 
359  for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
360  gp->drape_surf_id[i] = hsurfs[i];
361  }
362 
363  return;
364 }
#define NULL
Definition: ccmath.h:32
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_malloc(n)
Definition: defs/gis.h:94
int G_debug(int, const char *,...) __attribute__((format(printf
int GS_get_longdim(float *)
Get largest dimension.
Definition: gs2.c:140
geosurf * gs_get_surf(int)
Get geosurf struct.
Definition: gs.c:63
geosite * gp_get_new_site(void)
Create new geosite instance and add it to list.
Definition: gp.c:119
geosite * gp_get_prev_site(int id)
Get previous geosite struct from list.
Definition: gp.c:56
geosite * gp_get_last_site(void)
Get last point set.
Definition: gp.c:95
void gp_delete_site(int id)
Delete point set and remove from list.
Definition: gp.c:238
geosite * gp_get_site(int id)
Get geosite struct.
Definition: gp.c:33
int gp_free_site(geosite *fp)
Free allocated geosite struct.
Definition: gp.c:261
void gp_set_drapesurfs(geosite *gp, int hsurfs[], int nsurfs)
Set drape surfaces.
Definition: gp.c:355
void gp_free_sitemem(geosite *fp)
Free geosite (lower level)
Definition: gp.c:310
int gp_init_site(geosite *gp)
Initialize geosite struct.
Definition: gp.c:222
int gp_set_defaults(geosite *gp)
Set default value for geosite struct.
Definition: gp.c:189
#define FIRST_SITE_ID
Definition: gp.c:21
int gp_num_sites(void)
Get number of loaded point sets.
Definition: gp.c:76
void gp_update_drapesurfs(void)
Update drape surfaces.
Definition: gp.c:157
#define MAX_SURFS
Definition: ogsf.h:40
#define ST_X
Definition: ogsf.h:92
Definition: ogsf.h:349
struct g_point * next
Definition: ogsf.h:358
struct line_cats * cats
Definition: ogsf.h:353
gvstyle * style
Definition: ogsf.h:355
Definition: ogsf.h:362
char * filename
Definition: ogsf.h:369
struct g_site * next
Definition: ogsf.h:374
int drape_surf_id[MAX_SURFS]
Definition: ogsf.h:364
int gsite_id
Definition: ogsf.h:363
gvstyle * hstyle
Definition: ogsf.h:379
gvstyle_thematic * tstyle
Definition: ogsf.h:377
geopoint * points
Definition: ogsf.h:372
int n_sites
Definition: ogsf.h:365
gvstyle * style
Definition: ogsf.h:378
int n_surfs
Definition: ogsf.h:365
char * symbol_column
Definition: ogsf.h:306
char * color_column
Definition: ogsf.h:305
char * size_column
Definition: ogsf.h:307
char * width_column
Definition: ogsf.h:308
int color
Definition: ogsf.h:286
int symbol
Definition: ogsf.h:287
float size
Definition: ogsf.h:288
int width
Definition: ogsf.h:289