GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
gv2.c
Go to the documentation of this file.
1/*!
2 \file lib/ogsf/gv2.c
3
4 \brief OGSF library - loading and manipulating vector 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, GMSL/University of Illinois
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
21#include "gsget.h"
22
23static int Vect_ID[MAX_VECTS];
24static int Next_vect = 0;
25
26/*!
27 \brief Check if vector set exists
28
29 \param id vector set id
30
31 \return 0 not found
32 \return 1 found
33 */
34int GV_vect_exists(int id)
35{
36 int i, found = 0;
37
38 G_debug(3, "GV_vect_exists");
39
40 if (NULL == gv_get_vect(id)) {
41 return (0);
42 }
43
44 for (i = 0; i < Next_vect && !found; i++) {
45 if (Vect_ID[i] == id) {
46 found = 1;
47 }
48 }
49
50 return (found);
51}
52
53/*!
54 \brief Register new vector set
55
56 \return vector set id
57 \return -1 on error
58 */
60{
61 geovect *nv;
62
63 if (Next_vect < MAX_VECTS) {
66 Vect_ID[Next_vect] = nv->gvect_id;
67 ++Next_vect;
68
69 G_debug(3, "GV_new_vector(): id=%d", nv->gvect_id);
70
71 return nv->gvect_id;
72 }
73
74 return -1;
75}
76
77/*!
78 \brief Get number of available vector sets
79
80 \return number of vector sets
81 */
82int GV_num_vects(void)
83{
84 return (gv_num_vects());
85}
86
87/*!
88 \brief Get list of vector sets
89
90 Must free when no longer needed!
91
92 \param numvects number of vector sets
93
94 \return pointer to list of point sets
95 \return NULL on error
96 */
98{
99 int i, *ret;
100
101 *numvects = Next_vect;
102
103 if (Next_vect) {
104 ret = (int *)G_malloc(Next_vect * sizeof(int));
105 if (!ret) {
106 return (NULL);
107 }
108
109 for (i = 0; i < Next_vect; i++) {
110 ret[i] = Vect_ID[i];
111 }
112
113 return (ret);
114 }
115
116 return (NULL);
117}
118
119/*!
120 \brief Delete vector set from list
121
122 \param id vector set id
123
124 \return 1 on success
125 \return -1 on error
126 */
128{
129 int i, j, found = 0;
130
131 G_debug(3, "GV_delete_vect");
132
133 if (GV_vect_exists(id)) {
134 gv_delete_vect(id);
135
136 for (i = 0; i < Next_vect && !found; i++) {
137 if (Vect_ID[i] == id) {
138 found = 1;
139
140 for (j = i; j < Next_vect; j++) {
141 Vect_ID[j] = Vect_ID[j + 1];
142 }
143 }
144 }
145
146 if (found) {
147 --Next_vect;
148 return (1);
149 }
150 }
151
152 return (-1);
153}
154
155/*!
156 \brief Load vector set
157
158 Check to see if handle already loaded, if so - free before loading
159 new for now, always load to memory
160
161 \todo Load file handle & ready for reading instead of using
162 memory
163
164 \param id vector set id
165 \param filename filename
166
167 \return -1 on error (invalid vector set id)
168 \return 1 on success
169 */
170int GV_load_vector(int id, const char *filename)
171{
172 geovect *gv;
173
174 if (NULL == (gv = gv_get_vect(id))) {
175 return (-1);
176 }
177
178 if (gv->lines) {
180 }
181
182 gv->filename = G_store(filename);
183
184 if ((gv->lines = Gv_load_vect(filename, &(gv->n_lines)))) {
185 return (1);
186 }
187
188 return (-1);
189}
190
191/*!
192 \brief Get vector map name
193
194 Note: char array is allocated by G_store()
195
196 \param id vector set id
197 \param filename &filename
198
199 \return -1 on error (invalid vector set id)
200 \return 1 on success
201 */
202int GV_get_vectname(int id, char **filename)
203{
204 geovect *gv;
205
206 if (NULL == (gv = gv_get_vect(id))) {
207 return (-1);
208 }
209
210 *filename = G_store(gv->filename);
211
212 return (1);
213}
214
215/*!
216 \brief Set vector style
217
218 \param id vector set id
219 \param mem non-zero for use memory
220 \param color color value
221 \param width line width
222 \param use_z non-zero for 3d mode
223
224 \return -1 on error (invalid vector set id)
225 \return 1 on success
226 */
227int GV_set_style(int id, int mem, int color, int width, int use_z)
228{
229 geovect *gv;
230
231 if (NULL == (gv = gv_get_vect(id))) {
232 return -1;
233 }
234
235 gv->use_mem = mem;
236 gv->use_z = use_z;
237 gv->style->color = color;
238 gv->style->width = width;
239
240 return 1;
241}
242
243/*!
244 \brief Get vector style
245
246 \param id vector set id
247 \param[out] mem non-zero for use memory
248 \param[out] color color value
249 \param[out] width line width
250 \param[out] use_z non-zero for 3d mode
251
252 \return -1 on error (invalid vector set id)
253 \return 1 on success
254 */
255int GV_get_style(int id, int *mem, int *color, int *width, int *use_z)
256{
257 geovect *gv;
258
259 if (NULL == (gv = gv_get_vect(id))) {
260 return -1;
261 }
262
263 *mem = gv->use_mem;
264 *color = gv->style->color;
265 *width = gv->style->width;
266 *use_z = gv->use_z;
267
268 return 1;
269}
270
271/*!
272 \brief Set vector set style for thematic mapping
273
274 Updates also style for each geoline.
275
276 \param id vector set id
277 \param layer layer number for thematic mapping
278 \param color color column name
279 \param width width column name
280 \param color_rules pointer to Colors structure or NULL
281
282 \return 1 on success
283 \return -1 on error (point set not found)
284 */
285int GV_set_style_thematic(int id, int layer, const char *color,
286 const char *width, struct Colors *color_rules)
287{
288 geovect *gv;
289
290 if (NULL == (gv = gv_get_vect(id))) {
291 return -1;
292 }
293
294 if (!gv->tstyle)
295 gv->tstyle = (gvstyle_thematic *)G_malloc(sizeof(gvstyle_thematic));
296 G_zero(gv->tstyle, sizeof(gvstyle_thematic));
297
298 gv->tstyle->active = 1;
299 gv->tstyle->layer = layer;
300 if (color)
301 gv->tstyle->color_column = G_store(color);
302 if (width)
303 gv->tstyle->width_column = G_store(width);
304
306
307 return 1;
308}
309
310/*!
311 \brief Make style for thematic mapping inactive
312
313 \param id vector set id
314
315 \return 1 on success
316 \return -1 on error (point set not found)
317 */
319{
320 geovect *gv;
321
322 G_debug(4, "GV_unset_style_thematic(): id=%d", id);
323
324 if (NULL == (gv = gv_get_vect(id))) {
325 return -1;
326 }
327
328 if (gv->tstyle) {
329 gv->tstyle->active = 0;
330 }
331
332 return 1;
333}
334
335/*!
336 \brief Set trans ?
337
338 \param id vector set id
339 \param xtrans,ytrans,ztrans x/y/z trans values
340 */
341void GV_set_trans(int id, float xtrans, float ytrans, float ztrans)
342{
343 geovect *gv;
344
345 G_debug(3, "GV_set_trans");
346
347 gv = gv_get_vect(id);
348
349 if (gv) {
350 gv->x_trans = xtrans;
351 gv->y_trans = ytrans;
352 gv->z_trans = ztrans;
353 }
354
355 return;
356}
357
358/*!
359 \brief Get trans ?
360
361 \param id vector set id
362 \param[out] xtrans,ytrans,ztrans x/y/z trans values
363 */
364int GV_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
365{
366 geovect *gv;
367
368 gv = gv_get_vect(id);
369
370 if (gv) {
371 *xtrans = gv->x_trans;
372 *ytrans = gv->y_trans;
373 *ztrans = gv->z_trans;
374
375 return (1);
376 }
377
378 return (-1);
379}
380
381/*!
382 \brief Select surface identified by hs to have vector identified
383 by hv draped over it
384
385 \param hv vector set id
386 \param hs surface id
387
388 \return 1 on success
389 \return -1 on error
390 */
391int GV_select_surf(int hv, int hs)
392{
393 geovect *gv;
394
395 if (GV_surf_is_selected(hv, hs)) {
396 return (1);
397 }
398
399 gv = gv_get_vect(hv);
400
401 if (gv && GS_surf_exists(hs)) {
402 gv->drape_surf_id[gv->n_surfs] = hs;
403 gv->n_surfs += 1;
404
405 return (1);
406 }
407
408 return (-1);
409}
410
411/*!
412 \brief Unselect surface
413
414 \param hv vector set id
415 \param hs surface id
416
417 \return 1 on success
418 \return -1 on error
419 */
420int GV_unselect_surf(int hv, int hs)
421{
422 geovect *gv;
423 int i, j;
424
425 if (!GV_surf_is_selected(hv, hs)) {
426 return (1);
427 }
428
429 gv = gv_get_vect(hv);
430
431 if (gv) {
432 for (i = 0; i < gv->n_surfs; i++) {
433 if (gv->drape_surf_id[i] == hs) {
434 for (j = i; j < gv->n_surfs - 1; j++) {
435 gv->drape_surf_id[j] = gv->drape_surf_id[j + 1];
436 }
437
438 gv->n_surfs -= 1;
439
440 return (1);
441 }
442 }
443 }
444
445 return (-1);
446}
447
448/*!
449 \brief Check if surface is selected
450
451 \param hv vector set id
452 \param hs surface id
453
454 \return 1 selected
455 \return 0 not selected
456 */
458{
459 int i;
460 geovect *gv;
461
462 gv = gv_get_vect(hv);
463
464 if (gv) {
465 for (i = 0; i < gv->n_surfs; i++) {
466 if (hs == gv->drape_surf_id[i]) {
467 return (1);
468 }
469 }
470 }
471
472 return (0);
473}
474
475/*!
476 \brief Draw vector set
477
478 \param vid vector set id
479 */
481{
482 geosurf *gs;
483 geovect *gv;
484 int i;
485
486 gv = gv_get_vect(vid);
487
488 if (gv) {
489 for (i = 0; i < gv->n_surfs; i++) {
490 gs = gs_get_surf(gv->drape_surf_id[i]);
491
492 if (gs) {
493 gvd_vect(gv, gs, 0);
494 }
495 }
496 }
497
498 return;
499}
500
501/*!
502 \brief Draw all loaded vector sets
503 */
505{
506 int id;
507
508 for (id = 0; id < Next_vect; id++) {
509 GV_draw_vect(Vect_ID[id]);
510 }
511
512 return;
513}
514
515/*!
516 \brief Draw vector set (fast mode)
517
518 \todo Seems to be broken, nothing is drawn
519
520 \param vid vector set id
521 */
523{
524 geosurf *gs;
525 geovect *gv;
526 int i;
527
528 gv = gv_get_vect(vid);
529
530 if (gv) {
531 for (i = 0; i < gv->n_surfs; i++) {
532 gs = gs_get_surf(gv->drape_surf_id[i]);
533
534 if (gs) {
535 gvd_vect(gv, gs, 1);
536 }
537 }
538 }
539
540 return;
541}
542
543/*!
544 \brief Draw all loaded vector sets (fast mode)
545 */
547{
548 int id;
549
550 for (id = 0; id < Next_vect; id++) {
551 GV_draw_fastvect(Vect_ID[id]);
552 }
553
554 return;
555}
556
557/*!
558 \brief Set client data
559
560 \param id vector set id
561 \param clientd pointer to client data
562
563 \return 1 on success
564 \return -1 on error
565 */
566int GV_Set_ClientData(int id, void *clientd)
567{
568 geovect *gv;
569
570 gv = gv_get_vect(id);
571 if (gv) {
572 gv->clientdata = clientd;
573
574 return (1);
575 }
576
577 return (-1);
578}
579
580/*!
581 \brief Get client data
582
583 \param id vector set id
584
585 \return pointer to client data
586 \return NULL on error
587 */
588void *GV_Get_ClientData(int id)
589{
590 geovect *gv;
591
592 gv = gv_get_vect(id);
593
594 if (gv) {
595 return (gv->clientdata);
596 }
597
598 return (NULL);
599}
#define NULL
Definition ccmath.h:32
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:21
#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
void gv_free_vectmem(geovect *)
Free allocated memory.
Definition gv.c:311
int GS_surf_exists(int)
Definition gs2.c:190
geovect * gv_get_vect(int)
Get vector set.
Definition gv.c:32
int gvd_vect(geovect *, geosurf *, int)
Draw vector set.
Definition gvd.c:77
geosurf * gs_get_surf(int)
Get geosurf struct.
Definition gs.c:59
int gv_num_vects(void)
Get number of loaded vector sets.
Definition gv.c:75
geoline * Gv_load_vect(const char *, int *)
Load vector map to memory.
Definition gv3.c:45
void gv_delete_vect(int)
Delete vector set (unload)
Definition gv.c:240
geovect * gv_get_new_vect(void)
Allocate memory for new vector set.
Definition gv.c:116
int gv_set_defaults(geovect *)
Set attributes of vector set to default values.
Definition gv.c:185
int Gv_load_vect_thematic(geovect *, struct Colors *)
Load styles for geolines based on thematic mapping.
Definition gv3.c:317
int GV_get_vectname(int id, char **filename)
Get vector map name.
Definition gv2.c:202
int GV_delete_vector(int id)
Delete vector set from list.
Definition gv2.c:127
int GV_Set_ClientData(int id, void *clientd)
Set client data.
Definition gv2.c:566
int * GV_get_vect_list(int *numvects)
Get list of vector sets.
Definition gv2.c:97
int GV_load_vector(int id, const char *filename)
Load vector set.
Definition gv2.c:170
int GV_new_vector(void)
Register new vector set.
Definition gv2.c:59
void GV_draw_fastvect(int vid)
Draw vector set (fast mode)
Definition gv2.c:522
void * GV_Get_ClientData(int id)
Get client data.
Definition gv2.c:588
int GV_unselect_surf(int hv, int hs)
Unselect surface.
Definition gv2.c:420
int GV_get_style(int id, int *mem, int *color, int *width, int *use_z)
Get vector style.
Definition gv2.c:255
void GV_alldraw_fastvect(void)
Draw all loaded vector sets (fast mode)
Definition gv2.c:546
int GV_select_surf(int hv, int hs)
Select surface identified by hs to have vector identified by hv draped over it.
Definition gv2.c:391
void GV_set_trans(int id, float xtrans, float ytrans, float ztrans)
Set trans ?
Definition gv2.c:341
void GV_draw_vect(int vid)
Draw vector set.
Definition gv2.c:480
int GV_surf_is_selected(int hv, int hs)
Check if surface is selected.
Definition gv2.c:457
int GV_set_style_thematic(int id, int layer, const char *color, const char *width, struct Colors *color_rules)
Set vector set style for thematic mapping.
Definition gv2.c:285
int GV_num_vects(void)
Get number of available vector sets.
Definition gv2.c:82
void GV_alldraw_vect(void)
Draw all loaded vector sets.
Definition gv2.c:504
int GV_unset_style_thematic(int id)
Make style for thematic mapping inactive.
Definition gv2.c:318
int GV_set_style(int id, int mem, int color, int width, int use_z)
Set vector style.
Definition gv2.c:227
int GV_vect_exists(int id)
Check if vector set exists.
Definition gv2.c:34
int GV_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
Get trans ?
Definition gv2.c:364
OGSF header file (structures)
#define MAX_VECTS
Definition ogsf.h:42
Definition gis.h:689
Definition ogsf.h:267
Struct for vector map (thematic mapping)
Definition ogsf.h:342
Vector map (lines)
Definition ogsf.h:373