GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
vector/Vlib/select.c
Go to the documentation of this file.
1 
20 #include <stdlib.h>
21 #include <grass/gis.h>
22 #include <grass/Vect.h>
23 
37 int
38 Vect_select_lines_by_box(struct Map_info *Map, BOUND_BOX * Box,
39  int type, struct ilist *list)
40 {
41  int i, line, nlines;
42  struct Plus_head *plus;
43  P_LINE *Line;
44  static struct ilist *LocList = NULL;
45 
46  G_debug(3, "Vect_select_lines_by_box()");
47  G_debug(3, " Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
48  Box->E, Box->W, Box->T, Box->B);
49  plus = &(Map->plus);
50 
51  if (!(plus->Spidx_built)) {
52  G_debug(3, "Building spatial index.");
54  }
55 
56  list->n_values = 0;
57  if (!LocList)
58  LocList = Vect_new_list();
59 
60  nlines = dig_select_lines(plus, Box, LocList);
61  G_debug(3, " %d lines selected (all types)", nlines);
62 
63  /* Remove lines of not requested types */
64  for (i = 0; i < nlines; i++) {
65  line = LocList->value[i];
66  if (plus->Line[line] == NULL)
67  continue; /* Should not happen */
68  Line = plus->Line[line];
69  if (!(Line->type & type))
70  continue;
71  dig_list_add(list, line);
72  }
73 
74  G_debug(3, " %d lines of requested type", list->n_values);
75 
76  return list->n_values;
77 }
78 
91 int
92 Vect_select_areas_by_box(struct Map_info *Map, BOUND_BOX * Box,
93  struct ilist *list)
94 {
95  int i;
96 
97  G_debug(3, "Vect_select_areas_by_box()");
98  G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
99  Box->E, Box->W, Box->T, Box->B);
100 
101  if (!(Map->plus.Spidx_built)) {
102  G_debug(3, "Building spatial index.");
104  }
105 
106  dig_select_areas(&(Map->plus), Box, list);
107  G_debug(3, " %d areas selected", list->n_values);
108  for (i = 0; i < list->n_values; i++) {
109  G_debug(3, " area = %d pointer to area structure = %lx",
110  list->value[i],
111  (unsigned long)Map->plus.Area[list->value[i]]);
112 
113  }
114  return list->n_values;
115 }
116 
117 
130 int
131 Vect_select_isles_by_box(struct Map_info *Map, BOUND_BOX * Box,
132  struct ilist *list)
133 {
134  G_debug(3, "Vect_select_isles_by_box()");
135  G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
136  Box->E, Box->W, Box->T, Box->B);
137 
138  if (!(Map->plus.Spidx_built)) {
139  G_debug(3, "Building spatial index.");
141  }
142 
143  dig_select_isles(&(Map->plus), Box, list);
144  G_debug(3, " %d isles selected", list->n_values);
145 
146  return list->n_values;
147 }
148 
158 int
159 Vect_select_nodes_by_box(struct Map_info *Map, BOUND_BOX * Box,
160  struct ilist *list)
161 {
162  struct Plus_head *plus;
163 
164  G_debug(3, "Vect_select_nodes_by_box()");
165  G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
166  Box->E, Box->W, Box->T, Box->B);
167 
168  plus = &(Map->plus);
169 
170  if (!(plus->Spidx_built)) {
171  G_debug(3, "Building spatial index.");
173  }
174 
175  list->n_values = 0;
176 
177  dig_select_nodes(plus, Box, list);
178  G_debug(3, " %d nodes selected", list->n_values);
179 
180  return list->n_values;
181 }
182 
197 int
198 Vect_select_lines_by_polygon(struct Map_info *Map, struct line_pnts *Polygon,
199  int nisles, struct line_pnts **Isles, int type,
200  struct ilist *List)
201 {
202  int i;
203  BOUND_BOX box;
204  static struct line_pnts *LPoints = NULL;
205  static struct ilist *LocList = NULL;
206 
207  /* TODO: this function was not tested with isles */
208  G_debug(3, "Vect_select_lines_by_polygon() nisles = %d", nisles);
209 
210  List->n_values = 0;
211  if (!LPoints)
212  LPoints = Vect_new_line_struct();
213  if (!LocList)
214  LocList = Vect_new_list();
215 
216  /* Select first all lines by box */
217  dig_line_box(Polygon, &box);
218  box.T = PORT_DOUBLE_MAX;
219  box.B = -PORT_DOUBLE_MAX;
220  Vect_select_lines_by_box(Map, &box, type, LocList);
221  G_debug(3, " %d lines selected by box", LocList->n_values);
222 
223  /* Check all lines if intersect the polygon */
224  for (i = 0; i < LocList->n_values; i++) {
225  int j, line, intersect = 0;
226 
227  line = LocList->value[i];
228  /* Read line points */
229  Vect_read_line(Map, LPoints, NULL, line);
230 
231  /* Check if any of line vertices is within polygon */
232  for (j = 0; j < LPoints->n_points; j++) {
233  if (Vect_point_in_poly(LPoints->x[j], LPoints->y[j], Polygon) >= 1) { /* inside polygon */
234  int k, inisle = 0;
235 
236  for (k = 0; k < nisles; k++) {
237  if (Vect_point_in_poly(LPoints->x[j], LPoints->y[j], Isles[k]) >= 1) { /* in isle */
238  inisle = 1;
239  break;
240  }
241  }
242 
243  if (!inisle) { /* inside polygon, outside isles -> select */
244  intersect = 1;
245  break;
246  }
247  }
248  }
249  if (intersect) {
250  dig_list_add(List, line);
251  continue;
252  }
253 
254  /* Check intersections of the line with area/isles boundary */
255  /* Outer boundary */
256  if (Vect_line_check_intersection(LPoints, Polygon, 0)) {
257  dig_list_add(List, line);
258  continue;
259  }
260 
261  /* Islands */
262  for (j = 0; j < nisles; j++) {
263  if (Vect_line_check_intersection(LPoints, Isles[j], 0)) {
264  intersect = 1;
265  break;
266  }
267  }
268  if (intersect) {
269  dig_list_add(List, line);
270  }
271  }
272 
273  G_debug(4, " %d lines selected by polygon", List->n_values);
274 
275  return List->n_values;
276 }
277 
278 
295 int
296 Vect_select_areas_by_polygon(struct Map_info *Map, struct line_pnts *Polygon,
297  int nisles, struct line_pnts **Isles,
298  struct ilist *List)
299 {
300  int i, area;
301  static struct ilist *BoundList = NULL;
302 
303  /* TODO: this function was not tested with isles */
304  G_debug(3, "Vect_select_areas_by_polygon() nisles = %d", nisles);
305 
306  List->n_values = 0;
307  if (!BoundList)
308  BoundList = Vect_new_list();
309 
310  /* Select boundaries by polygon */
311  Vect_select_lines_by_polygon(Map, Polygon, nisles, Isles, GV_BOUNDARY,
312  BoundList);
313 
314  /* Add areas on left/right side of selected boundaries */
315  for (i = 0; i < BoundList->n_values; i++) {
316  int line, left, right;
317 
318  line = BoundList->value[i];
319 
320  Vect_get_line_areas(Map, line, &left, &right);
321  G_debug(4, "boundary = %d left = %d right = %d", line, left, right);
322 
323  if (left > 0) {
324  dig_list_add(List, left);
325  }
326  else if (left < 0) { /* island */
327  area = Vect_get_isle_area(Map, abs(left));
328  G_debug(4, " left island -> area = %d", area);
329  if (area > 0)
330  dig_list_add(List, area);
331  }
332 
333  if (right > 0) {
334  dig_list_add(List, right);
335  }
336  else if (right < 0) { /* island */
337  area = Vect_get_isle_area(Map, abs(right));
338  G_debug(4, " right island -> area = %d", area);
339  if (area > 0)
340  dig_list_add(List, area);
341  }
342  }
343 
344  /* But the Polygon may be completely inside the area (only one), in that case
345  * we find the area by one polygon point and add it to the list */
346  area = Vect_find_area(Map, Polygon->x[0], Polygon->y[0]);
347  if (area > 0)
348  dig_list_add(List, area);
349 
350  G_debug(3, " %d areas selected by polygon", List->n_values);
351 
352  return List->n_values;
353 }
int Vect_select_isles_by_box(struct Map_info *Map, BOUND_BOX *Box, struct ilist *list)
Select isles by box.
int Vect_get_isle_area(struct Map_info *Map, int isle)
Returns area for isle.
int dig_list_add(struct ilist *list, int val)
int Vect_select_nodes_by_box(struct Map_info *Map, BOUND_BOX *Box, struct ilist *list)
Select nodes by box.
int Vect_build_sidx_from_topo(struct Map_info *Map)
Create spatial index from topo if necessary.
Definition: sindex.c:144
int dig_line_box(struct line_pnts *Points, BOUND_BOX *Box)
Definition: diglib/box.c:24
int Vect_get_line_areas(struct Map_info *Map, int line, int *left, int *right)
Get area/isle ids on the left and right.
Definition: level_two.c:255
float Box[8][3]
Vertices for box.
Definition: gsd_objs.c:1420
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
Definition: line.c:57
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_line_check_intersection(struct line_pnts *APoints, struct line_pnts *BPoints, int with_z)
Check if 2 lines intersect.
int dig_select_isles(struct Plus_head *Plus, BOUND_BOX *box, struct ilist *list)
Select isles by box.
Definition: spindex.c:511
tuple box
surface = wx.CheckBox(parent = panel, id = wx.ID_ANY, label = _(&quot;Follow source viewpoint&quot;)) pageSizer...
Definition: tools.py:1522
int Vect_find_area(struct Map_info *Map, double x, double y)
Find the nearest area.
int dig_select_areas(struct Plus_head *Plus, BOUND_BOX *box, struct ilist *list)
Select areas by box.
Definition: spindex.c:482
int dig_select_nodes(struct Plus_head *Plus, BOUND_BOX *box, struct ilist *list)
Select nodes by bbox.
Definition: spindex.c:385
int Vect_select_lines_by_polygon(struct Map_info *Map, struct line_pnts *Polygon, int nisles, struct line_pnts **Isles, int type, struct ilist *List)
Select lines by Polygon with optional isles.
int Vect_select_areas_by_polygon(struct Map_info *Map, struct line_pnts *Polygon, int nisles, struct line_pnts **Isles, struct ilist *List)
Select areas by Polygon with optional isles.
int dig_select_lines(struct Plus_head *Plus, BOUND_BOX *box, struct ilist *list)
Select lines by box.
Definition: spindex.c:453
int Vect_select_lines_by_box(struct Map_info *Map, BOUND_BOX *Box, int type, struct ilist *list)
Select lines by box.
int Vect_point_in_poly(double X, double Y, struct line_pnts *Points)
Determines if a point (X,Y) is inside a polygon.
Definition: Vlib/poly.c:665
int Vect_select_areas_by_box(struct Map_info *Map, BOUND_BOX *Box, struct ilist *list)
Select areas by box.
return NULL
Definition: dbfopen.c:1394
tuple Map
Definition: render.py:1301
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.