GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
gp2.c
Go to the documentation of this file.
1/*!
2 \file lib/ogsf/gp2.c
3
4 \brief OGSF library - loading and manipulating point sets (higher 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 (January 1994)
11 \author Updated by Martin landa <landa.martin gmail.com>
12 (doxygenized in May 2008, thematic mapping in June 2011)
13 */
14
15#include <stdlib.h>
16#include <string.h>
17
18#include <grass/gis.h>
19#include <grass/ogsf.h>
20#include <grass/glocale.h>
21
22#include "gsget.h"
23
24static int Site_ID[MAX_SITES];
25static int Next_site = 0;
26
27/*!
28 \brief Check if point set exists
29
30 \param id point set id
31
32 \return 1 found
33 \return 0 not found
34 */
35int GP_site_exists(int id)
36{
37 int i, found = 0;
38
39 G_debug(4, "GP_site_exists(%d)", id);
40
41 if (NULL == gp_get_site(id)) {
42 return 0;
43 }
44
45 for (i = 0; i < Next_site && !found; i++) {
46 if (Site_ID[i] == id) {
47 found = 1;
48 }
49 }
50
51 G_debug(3, "GP_site_exists(): found=%d", found);
52
53 return found;
54}
55
56/*!
57 \brief Create new point set
58
59 \return point set id
60 \return -1 on error (number of point sets exceeded)
61 */
62int GP_new_site(void)
63{
64 geosite *np;
65
66 if (Next_site < MAX_SITES) {
67 np = gp_get_new_site();
69 Site_ID[Next_site] = np->gsite_id;
70 ++Next_site;
71
72 G_debug(3, "GP_new_site() id=%d", np->gsite_id);
73
74 return np->gsite_id;
75 }
76
77 return -1;
78}
79
80/*!
81 \brief Get number of loaded point sets
82
83 \return number of point sets
84 */
85int GP_num_sites(void)
86{
87 return gp_num_sites();
88}
89
90/*!
91 \brief Get list of point sets
92
93 Must freed when no longer needed!
94
95 \param numsites number of point sets
96
97 \return pointer to list of points sets
98 \return NULL on error
99 */
101{
102 int i, *ret;
103
104 *numsites = Next_site;
105
106 if (Next_site) {
107 ret = (int *)G_malloc(Next_site * sizeof(int)); /* G_fatal_error */
108 if (!ret) {
109 return NULL;
110 }
111
112 for (i = 0; i < Next_site; i++) {
113 ret[i] = Site_ID[i];
114 }
115
116 return ret;
117 }
118
119 return NULL;
120}
121
122/*!
123 \brief Delete registered point set
124
125 \param id point set id
126
127 \return 1 on success
128 \return -1 on error (point sets not available)
129 */
130int GP_delete_site(int id)
131{
132 int i, j, found = 0;
133
134 G_debug(4, "GP_delete_site(%d)", id);
135
136 if (GP_site_exists(id)) {
137 gp_delete_site(id);
138
139 for (i = 0; i < Next_site && !found; i++) {
140 if (Site_ID[i] == id) {
141 found = 1;
142 for (j = i; j < Next_site; j++) {
143 Site_ID[j] = Site_ID[j + 1];
144 }
145 }
146 }
147
148 if (found) {
149 --Next_site;
150 return 1;
151 }
152 }
153
154 return -1;
155}
156
157/*!
158 \brief Load point set from file
159
160 Check to see if handle already loaded, if so - free before loading
161 new for now, always load to memory.
162
163 \todo load file handle & ready for reading instead of using memory
164
165 \param id point set id
166 \param filename point set filename
167
168 \return -1 on error
169 \return 1 on success
170 */
171int GP_load_site(int id, const char *filename)
172{
173 geosite *gp;
174
175 G_debug(3, "GP_load_site(id=%d, name=%s)", id, filename);
176
177 if (NULL == (gp = gp_get_site(id))) {
178 return -1;
179 }
180
181 if (gp->points) {
183 }
184
185 gp->filename = G_store(filename);
186
187 gp->points = Gp_load_sites(filename, &(gp->n_sites), &(gp->has_z));
188
189 if (gp->points) {
190 return 1;
191 }
192
193 return -1;
194}
195
196/*!
197 \brief Get point set filename
198
199 Note: char array is allocated by G_store()
200
201 \param id point set id
202 \param[out] filename point set filename
203
204 \return -1 on error (point set not found)
205 \return 1 on success
206 */
207int GP_get_sitename(int id, char **filename)
208{
209 geosite *gp;
210
211 G_debug(4, "GP_get_sitename(%d)", id);
212
213 if (NULL == (gp = gp_get_site(id))) {
214 return -1;
215 }
216
217 *filename = G_store(gp->filename);
218
219 return 1;
220}
221
222/*!
223 \brief Get point set style
224
225 \param id point set id
226 \param color
227 \param width
228 \param size
229 \param symbol
230
231 \return 1 on success
232 \return -1 on error (point set not found)
233 */
234int GP_get_style(int id, int *color, int *width, float *size, int *symbol)
235{
236 geosite *gp;
237
238 G_debug(4, "GP_get_style(%d)", id);
239
240 if (NULL == (gp = gp_get_site(id))) {
241 return -1;
242 }
243
244 *color = gp->style->color;
245 *width = gp->style->width;
246 *symbol = gp->style->symbol;
247 *size = gp->style->size;
248
249 return 1;
250}
251
252/*!
253 \brief Set point style
254
255 Supported icon symbols (markers):
256 - ST_X
257 - ST_BOX
258 - ST_SPHERE
259 - ST_CUBE
260 - ST_DIAMOND
261 - ST_DEC_TREE
262 - ST_CON_TREE
263 - ST_ASTER
264 - ST_GYRO
265 - ST_HISTOGRAM
266
267 \param id point set id
268 \param color icon color
269 \param width icon line width
270 \param size icon size
271 \param symbol icon symbol
272
273 \return 1 on success
274 \return -1 on error (point set not found)
275 */
276int GP_set_style(int id, int color, int width, float size, int symbol)
277{
278 geosite *gp;
279
280 G_debug(4, "GP_set_style(id=%d, color=%d, width=%d, size=%f, symbol=%d)",
281 id, color, width, size, symbol);
282
283 if (NULL == (gp = gp_get_site(id))) {
284 return -1;
285 }
286
287 gp->style->color = color;
288 gp->style->symbol = symbol;
289 gp->style->size = size;
290 gp->style->width = width;
291
292 return 1;
293}
294
295/*!
296 \brief Set point set style for thematic mapping
297
298 Updates also style for each geopoint.
299
300 \param id point set id
301 \param layer layer number for thematic mapping (-1 for undefined)
302 \param color icon color column name
303 \param width icon line width column name
304 \param size icon size column name
305 \param symbol icon symbol column name
306 \param color_rules pointer to Colors structure or NULL
307
308 \return 1 on success
309 \return -1 on error (point set not found)
310 */
311int GP_set_style_thematic(int id, int layer, const char *color,
312 const char *width, const char *size,
313 const char *symbol, struct Colors *color_rules)
314{
315 geosite *gp;
316
317 G_debug(4,
318 "GP_set_style_thematic(id=%d, layer=%d, color=%s, width=%s, "
319 "size=%s, symbol=%s)",
320 id, layer, color, width, size, symbol);
321
322 if (NULL == (gp = gp_get_site(id))) {
323 return -1;
324 }
325
326 if (!gp->tstyle)
327 gp->tstyle = (gvstyle_thematic *)G_malloc(sizeof(gvstyle_thematic));
328 G_zero(gp->tstyle, sizeof(gvstyle_thematic));
329
330 gp->tstyle->active = 1;
331 gp->tstyle->layer = layer;
332 if (color)
333 gp->tstyle->color_column = G_store(color);
334 if (symbol)
335 gp->tstyle->symbol_column = G_store(symbol);
336 if (size)
337 gp->tstyle->size_column = G_store(size);
338 if (width)
339 gp->tstyle->width_column = G_store(width);
340
342
343 return 1;
344}
345
346/*!
347 \brief Make style for thematic mapping inactive
348
349 \param id point set id
350
351 \return 1 on success
352 \return -1 on error (point set not found)
353 */
355{
356 geosite *gp;
357
358 G_debug(4, "GP_unset_style_thematic(): id=%d", id);
359
360 if (NULL == (gp = gp_get_site(id))) {
361 return -1;
362 }
363
364 if (gp->tstyle) {
365 gp->tstyle->active = 0;
366 }
367
368 return 1;
369}
370
371/*!
372 \brief Set z mode for point set
373
374 \param id point set id
375 \param use_z TRUE to use z-coordinaces when vector map is 3D
376
377 \return 1 on success
378 \return 0 vector map is not 3D
379 \return -1 on error (invalid point set id)
380 */
381/* I don't see who is using it? Why it's required? */
382int GP_set_zmode(int id, int use_z)
383{
384 geosite *gp;
385
386 G_debug(3, "GP_set_zmode(%d,%d)", id, use_z);
387
388 if (NULL == (gp = gp_get_site(id))) {
389 return -1;
390 }
391
392 if (use_z) {
393 if (gp->has_z) {
394 gp->use_z = 1;
395 return 1;
396 }
397
398 return 0;
399 }
400
401 gp->use_z = 0;
402 return 1;
403}
404
405/*!
406 \brief Get z-mode
407
408 \todo Who's using this?
409
410 \param id point set id
411 \param[out] use_z non-zero code to use z
412
413 \return -1 on error (invalid point set id)
414 \return 1 on success
415 */
416int GP_get_zmode(int id, int *use_z)
417{
418 geosite *gp;
419
420 G_debug(4, "GP_get_zmode(%d)", id);
421
422 if (NULL == (gp = gp_get_site(id))) {
423 return -1;
424 }
425
426 *use_z = gp->use_z;
427 return 1;
428}
429
430/*!
431 \brief Set transformation params
432
433 \param id point set id
434 \param xtrans,ytrans,ztrans x/y/z values
435 */
436void GP_set_trans(int id, float xtrans, float ytrans, float ztrans)
437{
438 geosite *gp;
439
440 G_debug(3, "GP_set_trans(): id=%d trans=%f,%f,%f", id, xtrans, ytrans,
441 ztrans);
442
443 gp = gp_get_site(id);
444 if (gp) {
445 gp->x_trans = xtrans;
446 gp->y_trans = ytrans;
447 gp->z_trans = ztrans;
448 }
449
450 return;
451}
452
453/*!
454 \brief Get transformation params
455
456 \param id point set id
457 \param[out] xtrans,ytrans,ztrans x/y/z values
458 */
459void GP_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
460{
461 geosite *gp;
462
463 gp = gp_get_site(id);
464
465 if (gp) {
466 *xtrans = gp->x_trans;
467 *ytrans = gp->y_trans;
468 *ztrans = gp->z_trans;
469 }
470
471 G_debug(3, "GP_get_trans(): id=%d, trans=%f,%f,%f", id, *xtrans, *ytrans,
472 *ztrans);
473
474 return;
475}
476
477/*!
478 \brief Select surface for given point set
479
480 \param hp point set id
481 \param hs surface id
482
483 \return 1 surface selected
484 \return -1 on error
485 */
486int GP_select_surf(int hp, int hs)
487{
488 geosite *gp;
489
490 G_debug(3, "GP_select_surf(%d,%d)", hp, hs);
491
492 if (GP_surf_is_selected(hp, hs)) {
493 return 1;
494 }
495
496 gp = gp_get_site(hp);
497
498 if (gp && GS_surf_exists(hs)) {
499 gp->drape_surf_id[gp->n_surfs] = hs;
500 gp->n_surfs += 1;
501 return 1;
502 }
503
504 return -1;
505}
506
507/*!
508 \brief Unselect surface
509
510 \param hp point set id
511 \param hs surface id
512
513 \return 1 surface unselected
514 \return -1 on error
515 */
516int GP_unselect_surf(int hp, int hs)
517{
518 geosite *gp;
519 int i, j;
520
521 G_debug(3, "GP_unselect_surf(%d,%d)", hp, hs);
522
523 if (!GP_surf_is_selected(hp, hs)) {
524 return 1;
525 }
526
527 gp = gp_get_site(hp);
528
529 if (gp) {
530 for (i = 0; i < gp->n_surfs; i++) {
531 if (gp->drape_surf_id[i] == hs) {
532 for (j = i; j < gp->n_surfs - 1; j++) {
533 gp->drape_surf_id[j] = gp->drape_surf_id[j + 1];
534 }
535
536 gp->n_surfs -= 1;
537 return 1;
538 }
539 }
540 }
541
542 return -1;
543}
544
545/*!
546 \brief Check if surface is selected
547
548 \param hp point set id
549 \param hs surface id
550
551 \return 1 selected
552 \return 0 not selected
553 */
555{
556 int i;
557 geosite *gp;
558
559 G_debug(3, "GP_surf_is_selected(%d,%d)", hp, hs);
560
561 gp = gp_get_site(hp);
562
563 if (gp) {
564 for (i = 0; i < gp->n_surfs; i++) {
565 if (hs == gp->drape_surf_id[i]) {
566 return 1;
567 }
568 }
569 }
570
571 return 0;
572}
573
574/*!
575 \brief Draw point set
576
577 \param id point set id
578 */
579void GP_draw_site(int id)
580{
581 geosurf *gs;
582 geosite *gp;
583 int i;
584 float n, yo, xo, e;
585
586 gp = gp_get_site(id);
587 GS_get_region(&n, &yo, &xo, &e);
588
589 /* kind of sloppy - maybe site files should have an origin, too */
590 if (gp) {
591 if (gp->use_z && gp->has_z) {
592 gpd_3dsite(gp, xo, yo, 0);
593 }
594 else {
595 for (i = 0; i < gp->n_surfs; i++) {
596 gs = gs_get_surf(gp->drape_surf_id[i]);
597
598 if (gs) {
599 gpd_2dsite(gp, gs, 0);
600 G_debug(5, "Drawing site %d on Surf %d", id,
601 gp->drape_surf_id[i]);
602 }
603 }
604 }
605 }
606
607 return;
608}
609
610/*!
611 \brief Draw all available point sets
612 */
614{
615 int id;
616
617 for (id = 0; id < Next_site; id++) {
618 GP_draw_site(Site_ID[id]);
619 }
620
621 return;
622}
623
624/*!
625 \brief Set client data
626
627 \param id point set id
628 \param clientd client data
629
630 \return 1 on success
631 \return -1 on error (invalid point set id)
632 */
633int GP_Set_ClientData(int id, void *clientd)
634{
635 geosite *gp;
636
637 gp = gp_get_site(id);
638
639 if (gp) {
640 gp->clientdata = clientd;
641 return 1;
642 }
643
644 return -1;
645}
646
647/*!
648 \brief Get client data
649
650 \param id point set id
651
652 \return pointer to client data
653 \return NULL on error
654 */
655void *GP_Get_ClientData(int id)
656{
657 geosite *gp;
658
659 gp = gp_get_site(id);
660 if (gp) {
661 return (gp->clientdata);
662 }
663
664 return NULL;
665}
666
667/*!
668 \brief Determine point marker symbol for string
669
670 Supported markers:
671 - ST_X
672 - ST_BOX
673 - ST_SPHERE
674 - ST_CUBE
675 - ST_DIAMOND
676 - ST_DEC_TREE
677 - ST_CON_TREE
678 - ST_ASTER
679 - ST_GYRO
680 - ST_HISTOGRAM
681
682 \param str string buffer
683
684 \return marker code (default: ST_SPHERE)
685 */
686int GP_str_to_marker(const char *str)
687{
688 int marker;
689
690 if (strcmp(str, "x") == 0)
691 marker = ST_X;
692 else if (strcmp(str, "box") == 0)
693 marker = ST_BOX;
694 else if (strcmp(str, "sphere") == 0)
695 marker = ST_SPHERE;
696 else if (strcmp(str, "cube") == 0)
697 marker = ST_CUBE;
698 else if (strcmp(str, "diamond") == 0)
699 marker = ST_DIAMOND;
700 else if (strcmp(str, "dec_tree") == 0)
701 marker = ST_DEC_TREE;
702 else if (strcmp(str, "con_tree") == 0)
703 marker = ST_CON_TREE;
704 else if (strcmp(str, "aster") == 0)
705 marker = ST_ASTER;
706 else if (strcmp(str, "gyro") == 0)
707 marker = ST_GYRO;
708 else if (strcmp(str, "histogram") == 0)
709 marker = ST_HISTOGRAM;
710 else {
711 G_warning(_("Unknown icon marker, using \"sphere\""));
712 marker = ST_SPHERE;
713 }
714
715 return marker;
716}
#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_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
int G_debug(int, const char *,...) __attribute__((format(printf
int GS_surf_exists(int)
Definition gs2.c:190
int GS_get_region(float *, float *, float *, float *)
Get 2D region extent.
Definition gs2.c:152
int gpd_3dsite(geosite *, float, float, int)
Draw 3D point set.
Definition gpd.c:310
geosurf * gs_get_surf(int)
Get geosurf struct.
Definition gs.c:59
int gp_set_defaults(geosite *)
Set default value for geosite struct.
Definition gp.c:187
geopoint * Gp_load_sites(const char *, int *, int *)
Load to points to memory.
Definition gp3.c:43
void gp_free_sitemem(geosite *)
Free geosite (lower level)
Definition gp.c:308
geosite * gp_get_new_site(void)
Create new geosite instance and add it to list.
Definition gp.c:117
int gpd_2dsite(geosite *, geosurf *, int)
Draw 2D point set.
Definition gpd.c:213
int gp_num_sites(void)
Get number of loaded point sets.
Definition gp.c:74
void gp_delete_site(int)
Delete point set and remove from list.
Definition gp.c:236
geosite * gp_get_site(int)
Get geosite struct.
Definition gp.c:31
int Gp_load_sites_thematic(geosite *, struct Colors *)
Load styles for geopoints based on thematic mapping.
Definition gp3.c:179
#define _(str)
Definition glocale.h:10
int GP_select_surf(int hp, int hs)
Select surface for given point set.
Definition gp2.c:486
int GP_delete_site(int id)
Delete registered point set.
Definition gp2.c:130
int GP_new_site(void)
Create new point set.
Definition gp2.c:62
void GP_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
Get transformation params.
Definition gp2.c:459
int GP_get_zmode(int id, int *use_z)
Get z-mode.
Definition gp2.c:416
int GP_get_sitename(int id, char **filename)
Get point set filename.
Definition gp2.c:207
int GP_set_zmode(int id, int use_z)
Set z mode for point set.
Definition gp2.c:382
int GP_load_site(int id, const char *filename)
Load point set from file.
Definition gp2.c:171
void GP_set_trans(int id, float xtrans, float ytrans, float ztrans)
Set transformation params.
Definition gp2.c:436
int GP_Set_ClientData(int id, void *clientd)
Set client data.
Definition gp2.c:633
int GP_site_exists(int id)
Check if point set exists.
Definition gp2.c:35
int GP_str_to_marker(const char *str)
Determine point marker symbol for string.
Definition gp2.c:686
int GP_get_style(int id, int *color, int *width, float *size, int *symbol)
Get point set style.
Definition gp2.c:234
int GP_surf_is_selected(int hp, int hs)
Check if surface is selected.
Definition gp2.c:554
int * GP_get_site_list(int *numsites)
Get list of point sets.
Definition gp2.c:100
int GP_num_sites(void)
Get number of loaded point sets.
Definition gp2.c:85
void GP_draw_site(int id)
Draw point set.
Definition gp2.c:579
void * GP_Get_ClientData(int id)
Get client data.
Definition gp2.c:655
void GP_alldraw_site(void)
Draw all available point sets.
Definition gp2.c:613
int GP_unselect_surf(int hp, int hs)
Unselect surface.
Definition gp2.c:516
int GP_set_style_thematic(int id, int layer, const char *color, const char *width, const char *size, const char *symbol, struct Colors *color_rules)
Set point set style for thematic mapping.
Definition gp2.c:311
int GP_set_style(int id, int color, int width, float size, int symbol)
Set point style.
Definition gp2.c:276
int GP_unset_style_thematic(int id)
Make style for thematic mapping inactive.
Definition gp2.c:354
OGSF header file (structures)
#define ST_CUBE
Definition ogsf.h:96
#define ST_GYRO
Definition ogsf.h:101
#define MAX_SITES
Definition ogsf.h:43
#define ST_BOX
Definition ogsf.h:94
#define ST_SPHERE
Definition ogsf.h:95
#define ST_HISTOGRAM
Definition ogsf.h:102
#define ST_CON_TREE
Definition ogsf.h:99
#define ST_DEC_TREE
Definition ogsf.h:98
#define ST_DIAMOND
Definition ogsf.h:97
#define ST_ASTER
Definition ogsf.h:100
#define ST_X
Definition ogsf.h:93
Definition gis.h:689
Vector map (points)
Definition ogsf.h:416
int gsite_id
Definition ogsf.h:417
Definition ogsf.h:267
Struct for vector map (thematic mapping)
Definition ogsf.h:342