GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 functions)
5 
6  (C) 1999-2008, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Bill Brown USACERL, GMSL/University of Illinois
12  \author Updated by Martin landa <landa.martin gmail.com>
13  (doxygenized in May 2008, thematic mapping in June 2011)
14  */
15 
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <grass/gis.h>
20 #include <grass/ogsf.h>
21 
22 #include "gsget.h"
23 
24 static int Vect_ID[MAX_VECTS];
25 static int Next_vect = 0;
26 
27 /*!
28  \brief Check if vector set exists
29 
30  \param id vector set id
31 
32  \return 0 not found
33  \return 1 found
34  */
35 int GV_vect_exists(int id)
36 {
37  int i, found = 0;
38 
39  G_debug(3, "GV_vect_exists");
40 
41  if (NULL == gv_get_vect(id)) {
42  return (0);
43  }
44 
45  for (i = 0; i < Next_vect && !found; i++) {
46  if (Vect_ID[i] == id) {
47  found = 1;
48  }
49  }
50 
51  return (found);
52 }
53 
54 /*!
55  \brief Register new vector set
56 
57  \return vector set id
58  \return -1 on error
59  */
60 int GV_new_vector(void)
61 {
62  geovect *nv;
63 
64  if (Next_vect < MAX_VECTS) {
65  nv = gv_get_new_vect();
66  gv_set_defaults(nv);
67  Vect_ID[Next_vect] = nv->gvect_id;
68  ++Next_vect;
69 
70  G_debug(3, "GV_new_vector(): id=%d", nv->gvect_id);
71 
72  return nv->gvect_id;
73  }
74 
75  return -1;
76 }
77 
78 /*!
79  \brief Get number of available vector sets
80 
81  \return number of vector sets
82  */
83 int GV_num_vects(void)
84 {
85  return (gv_num_vects());
86 }
87 
88 /*!
89  \brief Get list of vector sets
90 
91  Must free when no longer needed!
92 
93  \param numvects number of vector sets
94 
95  \return pointer to list of point sets
96  \return NULL on error
97  */
98 int *GV_get_vect_list(int *numvects)
99 {
100  int i, *ret;
101 
102  *numvects = Next_vect;
103 
104  if (Next_vect) {
105  ret = (int *)G_malloc(Next_vect * sizeof(int));
106  if (!ret) {
107  return (NULL);
108  }
109 
110  for (i = 0; i < Next_vect; i++) {
111  ret[i] = Vect_ID[i];
112  }
113 
114  return (ret);
115  }
116 
117  return (NULL);
118 }
119 
120 /*!
121  \brief Delete vector set from list
122 
123  \param id vector set id
124 
125  \return 1 on success
126  \return -1 on error
127  */
128 int GV_delete_vector(int id)
129 {
130  int i, j, found = 0;
131 
132  G_debug(3, "GV_delete_vect");
133 
134  if (GV_vect_exists(id)) {
135  gv_delete_vect(id);
136 
137  for (i = 0; i < Next_vect && !found; i++) {
138  if (Vect_ID[i] == id) {
139  found = 1;
140 
141  for (j = i; j < Next_vect; j++) {
142  Vect_ID[j] = Vect_ID[j + 1];
143  }
144  }
145  }
146 
147  if (found) {
148  --Next_vect;
149  return (1);
150  }
151  }
152 
153  return (-1);
154 }
155 
156 /*!
157  \brief Load vector set
158 
159  Check to see if handle already loaded, if so - free before loading
160  new for now, always load to memory
161 
162  \todo Load file handle & ready for reading instead of using
163  memory
164 
165  \param id vector set id
166  \param filename filename
167 
168  \return -1 on error (invalid vector set id)
169  \return 1 on success
170  */
171 int GV_load_vector(int id, const char *filename)
172 {
173  geovect *gv;
174 
175  if (NULL == (gv = gv_get_vect(id))) {
176  return (-1);
177  }
178 
179  if (gv->lines) {
180  gv_free_vectmem(gv);
181  }
182 
183  gv->filename = G_store(filename);
184 
185  if ((gv->lines = Gv_load_vect(filename, &(gv->n_lines)))) {
186  return (1);
187  }
188 
189  return (-1);
190 }
191 
192 /*!
193  \brief Get vector map name
194 
195  Note: char array is allocated by G_store()
196 
197  \param id vector set id
198  \param filename &filename
199 
200  \return -1 on error (invalid vector set id)
201  \return 1 on success
202  */
203 int GV_get_vectname(int id, char **filename)
204 {
205  geovect *gv;
206 
207  if (NULL == (gv = gv_get_vect(id))) {
208  return (-1);
209  }
210 
211  *filename = G_store(gv->filename);
212 
213  return (1);
214 }
215 
216 /*!
217  \brief Set vector style
218 
219  \param id vector set id
220  \param mem non-zero for use memory
221  \param color color value
222  \param width line width
223  \param use_z non-zero for 3d mode
224 
225  \return -1 on error (invalid vector set id)
226  \return 1 on success
227  */
228 int GV_set_style(int id, int mem, int color, int width, int use_z)
229 {
230  geovect *gv;
231 
232  if (NULL == (gv = gv_get_vect(id))) {
233  return -1;
234  }
235 
236  gv->use_mem = mem;
237  gv->use_z = use_z;
238  gv->style->color = color;
239  gv->style->width = width;
240 
241  return 1;
242 }
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, const char* width,
288  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 }
float x_trans
Definition: ogsf.h:338
#define G_malloc(n)
Definition: defs/gis.h:112
geosurf * gs_get_surf(int)
Get geosurf struct.
Definition: gs.c:62
void gv_delete_vect(int)
Delete vector set (unload)
Definition: gv.c:239
int gv_set_defaults(geovect *)
Set attributes of vector set to default values.
Definition: gv.c:184
void * GV_Get_ClientData(int id)
Get client data.
Definition: gv2.c:590
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
float z_trans
Definition: ogsf.h:338
int use_z
Definition: ogsf.h:335
int n_surfs
Definition: ogsf.h:336
int gvect_id
Definition: ogsf.h:332
int GV_load_vector(int id, const char *filename)
Load vector set.
Definition: gv2.c:171
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
gvstyle_thematic * tstyle
Definition: ogsf.h:346
int color
Definition: ogsf.h:288
gvstyle * style
Definition: ogsf.h:347
void GV_alldraw_vect(void)
Draw all loaded vector sets.
Definition: gv2.c:506
int GV_delete_vector(int id)
Delete vector set from list.
Definition: gv2.c:128
geoline * Gv_load_vect(const char *, int *)
Load vector map to memory.
Definition: gv3.c:47
geoline * lines
Definition: ogsf.h:340
#define NULL
Definition: ccmath.h:32
int gv_num_vects(void)
Get number of loaded vector sets.
Definition: gv.c:76
char * color_column
Definition: ogsf.h:307
int GV_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
Get trans ?
Definition: gv2.c:366
int drape_surf_id[MAX_SURFS]
Definition: ogsf.h:334
int width
Definition: ogsf.h:291
int GV_Set_ClientData(int id, void *clientd)
Set client data.
Definition: gv2.c:568
int GV_get_vectname(int id, char **filename)
Get vector map name.
Definition: gv2.c:203
int n_lines
Definition: ogsf.h:333
int GV_vect_exists(int id)
Check if vector set exists.
Definition: gv2.c:35
void GV_draw_vect(int vid)
Draw vector set.
Definition: gv2.c:482
int GV_unselect_surf(int hv, int hs)
Unselect surface.
Definition: gv2.c:422
int GV_set_style(int id, int mem, int color, int width, int use_z)
Set vector style.
Definition: gv2.c:228
Definition: ogsf.h:330
int GV_num_vects(void)
Get number of available vector sets.
Definition: gv2.c:83
void GV_draw_fastvect(int vid)
Draw vector set (fast mode)
Definition: gv2.c:524
char * width_column
Definition: ogsf.h:310
int GV_surf_is_selected(int hv, int hs)
Check if surface is selected.
Definition: gv2.c:459
void gv_free_vectmem(geovect *)
Free allocated memory.
Definition: gv.c:310
geovect * gv_get_new_vect(void)
Allocate memory for new vector set.
Definition: gv.c:115
Definition: gis.h:665
int Gv_load_vect_thematic(geovect *, struct Colors *)
Load styles for geolines based on thematic mapping.
Definition: gv3.c:313
int gvd_vect(geovect *, geosurf *, int)
Draw vector set.
Definition: gvd.c:79
int use_mem
Definition: ogsf.h:333
int GV_unset_style_thematic(int id)
Make style for thematic mapping inactive.
Definition: gv2.c:320
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
void * clientdata
Definition: ogsf.h:344
void GV_set_trans(int id, float xtrans, float ytrans, float ztrans)
Set trans ?
Definition: gv2.c:343
float y_trans
Definition: ogsf.h:338
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
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
char * filename
Definition: ogsf.h:337
geovect * gv_get_vect(int)
Get vector set.
Definition: gv.c:33
int * GV_get_vect_list(int *numvects)
Get list of vector sets.
Definition: gv2.c:98
int G_debug(int, const char *,...) __attribute__((format(printf
Definition: ogsf.h:257
int GV_new_vector(void)
Register new vector set.
Definition: gv2.c:60
int GS_surf_exists(int)
Definition: gs2.c:194
#define MAX_VECTS
Definition: ogsf.h:39