GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-56109d36f5
zbulk.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/vedit/zbulk.c
3 
4  \brief Vedit library - Bulk labeling (automated labeling of vector
5  features)
6 
7  (C) 2007-2008 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 Martin Landa <landa.martin gmail.com>
13  */
14 
15 #include <grass/dbmi.h>
16 #include <grass/vedit.h>
17 
18 /*!
19  \brief Lines z-bulk labeling
20 
21  Automated labeling (z coordinate assignment) of vector lines (iso-lines).
22 
23  \param Map pointer to Map_info
24  \param List list of selected lines
25  \param x1,y1,x2,y2 staring and ending point
26  \param start starting value
27  \param step step value
28 
29  \return number of modified features
30  \return -1 on error
31  */
32 int Vedit_bulk_labeling(struct Map_info *Map, struct ilist *List, double x1,
33  double y1, double x2, double y2, double start,
34  double step)
35 {
36  int i, cv_i, p_i;
37  int line, type, temp_line;
38  int nlines_modified;
39  double value, dist;
40 
41  struct line_cats *Cats;
42  struct line_pnts *Points, *Points_se; /* start - end */
43  struct bound_box box, box_se;
44 
45  /* for intersection */
46  struct line_pnts **Points_a, **Points_b;
47  int nlines_a, nlines_b;
48 
49  dbCatValArray cv; /* line_id / dist */
50 
51  nlines_modified = 0;
52 
53  value = start;
54 
55  Points = Vect_new_line_struct();
56  Points_se = Vect_new_line_struct();
57  Cats = Vect_new_cats_struct();
58 
59  db_CatValArray_alloc(&cv, List->n_values);
61  cv.n_values = 0;
62 
63  Vect_append_point(Points_se, x1, y1, -PORT_DOUBLE_MAX);
64  Vect_append_point(Points_se, x2, y2, PORT_DOUBLE_MAX);
65 
66  /* write temporary line */
67  temp_line = Vect_write_line(Map, GV_LINE, Points_se, Cats);
68  if (temp_line < 0) {
69  nlines_modified = -1;
70  goto free_exit;
71  }
72 
73  Vect_line_box(Points_se, &box_se);
74 
75  /* determine order of lines */
76  cv_i = 0;
77  for (i = 0; i < List->n_values; i++) {
78  line = List->value[i];
79 
80  if (!Vect_line_alive(Map, line))
81  continue;
82 
83  type = Vect_read_line(Map, Points, NULL, line);
84 
85  if (!(type & GV_LINE))
86  continue;
87 
88  Vect_line_box(Points, &box);
89  if (Vect_line_check_intersection(Points_se, Points, WITH_Z)) {
90  Vect_line_intersection(Points_se, Points, &box_se, &box, &Points_a,
91  &Points_b, &nlines_a, &nlines_b, WITHOUT_Z);
92 
93  if (nlines_a < 2 || nlines_b < 1) /* should not happen */
94  continue;
95 
96  /* calculate distance start point -> point of intersection */
97  for (p_i = 0; p_i < Points_a[0]->n_points; p_i++) {
98  Points_a[0]->z[p_i] = 0;
99  }
100  dist =
101  Vect_line_length(Points_a[0]); /* always first line in array? */
102 
103  cv.value[cv_i].cat = line;
104  cv.value[cv_i++].val.d = dist;
105  cv.n_values++;
106  }
107  }
108 
109  /* sort array by distance */
111 
112  /* z bulk-labeling */
113  for (cv_i = 0; cv_i < cv.n_values; cv_i++) {
114  line = cv.value[cv_i].cat;
115  type = Vect_read_line(Map, Points, Cats, line);
116 
117  for (p_i = 0; p_i < Points->n_points; p_i++) {
118  Points->z[p_i] = value;
119  }
120 
121  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
122  nlines_modified = -1;
123  goto free_exit;
124  }
125  nlines_modified++;
126 
127  value += step;
128  }
129 
130  if (Vect_delete_line(Map, temp_line) < 0) {
131  nlines_modified = -1;
132  }
133 
134 free_exit:
135  db_CatValArray_free(&cv);
136  Vect_destroy_line_struct(Points);
137  Vect_destroy_line_struct(Points_se);
139 
140  return nlines_modified;
141 }
#define NULL
Definition: ccmath.h:32
Main header of GRASS DataBase Management Interface.
#define DB_C_TYPE_DOUBLE
Definition: dbmi.h:109
void db_CatValArray_free(dbCatValArray *)
Free allocated dbCatValArray.
Definition: value.c:373
int db_CatValArray_sort_by_value(dbCatValArray *)
Sort key/value array by value.
int db_CatValArray_alloc(dbCatValArray *, int)
Allocate dbCatValArray.
Definition: value.c:401
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition: line.c:77
off_t Vect_rewrite_line(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites existing feature (topological level required)
double Vect_line_length(const struct line_pnts *)
Calculate line length, 3D-length in case of 3D vector line.
Definition: line.c:575
void Vect_line_box(const struct line_pnts *, struct bound_box *)
Get bounding box of line.
Definition: line.c:888
int Vect_line_intersection(struct line_pnts *, struct line_pnts *, struct bound_box *, struct bound_box *, struct line_pnts ***, struct line_pnts ***, int *, int *, int)
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
int Vect_line_check_intersection(struct line_pnts *, struct line_pnts *, int)
Check if 2 lines intersect.
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
int Vect_delete_line(struct Map_info *, off_t)
Delete existing feature (topological level required)
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition: line.c:45
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
Definition: line.c:148
#define GV_LINE
Definition: dig_defines.h:184
#define WITH_Z
Definition: dig_defines.h:172
#define PORT_DOUBLE_MAX
Limits for portable types.
Definition: dig_defines.h:66
#define WITHOUT_Z
2D/3D vector data
Definition: dig_defines.h:171
Vector map info.
Definition: dig_structs.h:1243
Bounding box.
Definition: dig_structs.h:64
int ctype
Definition: dbmi.h:272
int n_values
Definition: dbmi.h:270
dbCatVal * value
Definition: dbmi.h:273
union dbCatVal::@1 val
int cat
Definition: dbmi.h:254
double d
Definition: dbmi.h:258
List of integers.
Definition: gis.h:715
int n_values
Number of values in the list.
Definition: gis.h:723
int * value
Array of values.
Definition: gis.h:719
Feature category info.
Definition: dig_structs.h:1677
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
int n_points
Number of points.
Definition: dig_structs.h:1667
double * z
Array of Z coordinates.
Definition: dig_structs.h:1663
int Vedit_bulk_labeling(struct Map_info *Map, struct ilist *List, double x1, double y1, double x2, double y2, double start, double step)
Lines z-bulk labeling.
Definition: zbulk.c:32