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