GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
zbulk.c
Go to the documentation of this file.
1 
15 #include <grass/dbmi.h>
16 #include <grass/vedit.h>
17 
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 
44  /* for intersection */
45  struct line_pnts **Points_a, **Points_b;
46  int nlines_a, nlines_b;
47 
48  dbCatValArray cv; /* line_id / dist */
49 
50  nlines_modified = 0;
51 
52  value = start;
53 
54  Points = Vect_new_line_struct();
55  Points_se = Vect_new_line_struct();
56  Cats = Vect_new_cats_struct();
57 
58  //cv = (dbCatValArray *) G_malloc (sizeof (dbCatValArray));
59  db_CatValArray_alloc(&cv, List->n_values);
60  cv.ctype = DB_C_TYPE_DOUBLE;
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  /* determine order of lines */
73  cv_i = 0;
74  for (i = 0; i < List->n_values; i++) {
75  line = List->value[i];
76 
77  if (!Vect_line_alive(Map, line))
78  continue;
79 
80  type = Vect_read_line(Map, Points, NULL, line);
81 
82  if (!(type & GV_LINE))
83  continue;
84 
85  if (Vect_line_check_intersection(Points_se, Points, WITH_Z)) {
86  Vect_line_intersection(Points_se, Points,
87  &Points_a, &Points_b, &nlines_a, &nlines_b,
88  WITHOUT_Z);
89 
90  if (nlines_a < 2 || nlines_b < 1) /* should not happen */
91  continue;
92 
93  /* calculate distance start point -> point of intersection */
94  for (p_i = 0; p_i < Points_a[0]->n_points; p_i++) {
95  Points_a[0]->z[p_i] = 0;
96  }
97  dist = Vect_line_length(Points_a[0]); /* always first line in array? */
98 
99  cv.value[cv_i].cat = line;
100  cv.value[cv_i++].val.d = dist;
101  cv.n_values++;
102  }
103  }
104 
105  /* sort array by distance */
107 
108  /* z bulk-labeling */
109  for (cv_i = 0; cv_i < cv.n_values; cv_i++) {
110  line = cv.value[cv_i].cat;
111  Vect_read_line(Map, Points, Cats, line);
112 
113  for (p_i = 0; p_i < Points->n_points; p_i++) {
114  Points->z[p_i] = value;
115  }
116 
117  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
118  return -1;
119  }
120  nlines_modified++;
121 
122  value += step;
123  }
124 
125  if (Vect_delete_line(Map, temp_line) < 0) {
126  return -1;
127  }
128 
129  db_CatValArray_free(&cv);
130  Vect_destroy_line_struct(Points);
131  Vect_destroy_line_struct(Points_se);
133 
134  return nlines_modified;
135 }