GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-3f124c5c78
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, 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  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, &Points_a,
90  &Points_b, &nlines_a, &nlines_b, WITHOUT_Z);
91 
92  if (nlines_a < 2 || nlines_b < 1) /* should not happen */
93  continue;
94 
95  /* calculate distance start point -> point of intersection */
96  for (p_i = 0; p_i < Points_a[0]->n_points; p_i++) {
97  Points_a[0]->z[p_i] = 0;
98  }
99  dist =
100  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 }
#define NULL
Definition: ccmath.h:32
#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:708
int n_values
Number of values in the list.
Definition: gis.h:716
int * value
Array of values.
Definition: gis.h:712
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