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