GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 point_start_end 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,
33  double x1, double y1, double x2, double y2,
34  double start, 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 temporaly line */
67  temp_line = Vect_write_line(Map, GV_LINE, Points_se, Cats);
68  if (temp_line < 0) {
69  return -1;
70  }
71 
72  Vect_line_box(Points_se, &box_se);
73 
74  /* determine order of lines */
75  cv_i = 0;
76  for (i = 0; i < List->n_values; i++) {
77  line = List->value[i];
78 
79  if (!Vect_line_alive(Map, line))
80  continue;
81 
82  type = Vect_read_line(Map, Points, NULL, line);
83 
84  if (!(type & GV_LINE))
85  continue;
86 
87  Vect_line_box(Points, &box);
88  if (Vect_line_check_intersection(Points_se, Points, WITH_Z)) {
89  Vect_line_intersection(Points_se, Points, &box_se, &box,
90  &Points_a, &Points_b, &nlines_a, &nlines_b,
91  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 = Vect_line_length(Points_a[0]); /* always first line in array? */
101 
102  cv.value[cv_i].cat = line;
103  cv.value[cv_i++].val.d = dist;
104  cv.n_values++;
105  }
106  }
107 
108  /* sort array by distance */
110 
111  /* z bulk-labeling */
112  for (cv_i = 0; cv_i < cv.n_values; cv_i++) {
113  line = cv.value[cv_i].cat;
114  type = Vect_read_line(Map, Points, Cats, line);
115 
116  for (p_i = 0; p_i < Points->n_points; p_i++) {
117  Points->z[p_i] = value;
118  }
119 
120  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
121  return -1;
122  }
123  nlines_modified++;
124 
125  value += step;
126  }
127 
128  if (Vect_delete_line(Map, temp_line) < 0) {
129  return -1;
130  }
131 
132  db_CatValArray_free(&cv);
133  Vect_destroy_line_struct(Points);
134  Vect_destroy_line_struct(Points_se);
136 
137  return nlines_modified;
138 }
Bounding box.
Definition: dig_structs.h:65
int n_values
Definition: dbmi.h:286
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)
int n_points
Number of points.
Definition: dig_structs.h:1692
int ctype
Definition: dbmi.h:288
int n_values
Number of values in the list.
Definition: gis.h:709
double Vect_line_length(const struct line_pnts *)
Calculate line length, 3D-length in case of 3D vector line.
Definition: line.c:576
void Vect_line_box(const struct line_pnts *, struct bound_box *)
Get bounding box of line.
Definition: line.c:922
#define NULL
Definition: ccmath.h:32
dbCatVal * value
Definition: dbmi.h:289
int db_CatValArray_alloc(dbCatValArray *, int)
Allocate dbCatValArray.
Definition: value.c:401
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
int db_CatValArray_sort_by_value(dbCatValArray *)
Sort key/value array by value.
Feature category info.
Definition: dig_structs.h:1702
#define GV_LINE
Definition: dig_defines.h:183
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)
Intersect 2 lines.
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
int cat
Definition: dbmi.h:268
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition: line.c:45
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
Definition: line.c:149
#define WITH_Z
Definition: dig_defines.h:171
#define WITHOUT_Z
2D/3D vector data
Definition: dig_defines.h:170
Vector map info.
Definition: dig_structs.h:1259
#define PORT_DOUBLE_MAX
Limits for portable types.
Definition: dig_defines.h:66
int Vect_delete_line(struct Map_info *, off_t)
Delete existing feature (topological level required)
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
void db_CatValArray_free(dbCatValArray *)
Free allocated dbCatValArray.
Definition: value.c:373
#define DB_C_TYPE_DOUBLE
Definition: dbmi.h:109
int Vect_line_alive(const struct Map_info *, int)
Check if feature is alive or dead (topological level required)
union dbCatVal::@1 val
double * z
Array of Z coordinates.
Definition: dig_structs.h:1688
List of integers.
Definition: gis.h:700
int * value
Array of values.
Definition: gis.h:705
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
int Vect_read_line(const struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
double d
Definition: dbmi.h:273
int Vect_line_check_intersection(struct line_pnts *, struct line_pnts *, int)
Check if 2 lines intersect.