GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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  const char *dstr;
97  int debug_level;
98 
99  dstr = G__getenv("DEBUG");
100 
101  if (dstr != NULL)
102  debug_level = atoi(dstr);
103  else
104  debug_level = 0;
105 
106  G_debug(3, "Vect_select_areas_by_box()");
107  G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
108  Box->E, Box->W, Box->T, Box->B);
109 
110  if (!(Map->plus.Spidx_built)) {
111  G_debug(3, "Building spatial index.");
113  }
114 
115  dig_select_areas(&(Map->plus), Box, list);
116  G_debug(3, " %d areas selected", list->n_values);
117  /* avoid loop when not debugging */
118  if (debug_level > 2) {
119  for (i = 0; i < list->n_values; i++) {
120  G_debug(3, " area = %d pointer to area structure = %lx",
121  list->value[i],
122  (unsigned long)Map->plus.Area[list->value[i]]);
123  }
124  }
125 
126  return list->n_values;
127 }
128 
129 
142 int
143 Vect_select_isles_by_box(struct Map_info *Map, BOUND_BOX * Box,
144  struct ilist *list)
145 {
146  G_debug(3, "Vect_select_isles_by_box()");
147  G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
148  Box->E, Box->W, Box->T, Box->B);
149 
150  if (!(Map->plus.Spidx_built)) {
151  G_debug(3, "Building spatial index.");
153  }
154 
155  dig_select_isles(&(Map->plus), Box, list);
156  G_debug(3, " %d isles selected", list->n_values);
157 
158  return list->n_values;
159 }
160 
170 int
171 Vect_select_nodes_by_box(struct Map_info *Map, BOUND_BOX * Box,
172  struct ilist *list)
173 {
174  struct Plus_head *plus;
175 
176  G_debug(3, "Vect_select_nodes_by_box()");
177  G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
178  Box->E, Box->W, Box->T, Box->B);
179 
180  plus = &(Map->plus);
181 
182  if (!(plus->Spidx_built)) {
183  G_debug(3, "Building spatial index.");
185  }
186 
187  list->n_values = 0;
188 
189  dig_select_nodes(plus, Box, list);
190  G_debug(3, " %d nodes selected", list->n_values);
191 
192  return list->n_values;
193 }
194 
209 int
210 Vect_select_lines_by_polygon(struct Map_info *Map, struct line_pnts *Polygon,
211  int nisles, struct line_pnts **Isles, int type,
212  struct ilist *List)
213 {
214  int i;
215  BOUND_BOX box;
216  static struct line_pnts *LPoints = NULL;
217  static struct ilist *LocList = NULL;
218 
219  /* TODO: this function was not tested with isles */
220  G_debug(3, "Vect_select_lines_by_polygon() nisles = %d", nisles);
221 
222  List->n_values = 0;
223  if (!LPoints)
224  LPoints = Vect_new_line_struct();
225  if (!LocList)
226  LocList = Vect_new_list();
227 
228  /* Select first all lines by box */
229  dig_line_box(Polygon, &box);
230  box.T = PORT_DOUBLE_MAX;
231  box.B = -PORT_DOUBLE_MAX;
232  Vect_select_lines_by_box(Map, &box, type, LocList);
233  G_debug(3, " %d lines selected by box", LocList->n_values);
234 
235  /* Check all lines if intersect the polygon */
236  for (i = 0; i < LocList->n_values; i++) {
237  int j, line, intersect = 0;
238 
239  line = LocList->value[i];
240  /* Read line points */
241  Vect_read_line(Map, LPoints, NULL, line);
242 
243  /* Check if any of line vertices is within polygon */
244  for (j = 0; j < LPoints->n_points; j++) {
245  if (Vect_point_in_poly(LPoints->x[j], LPoints->y[j], Polygon) >= 1) { /* inside polygon */
246  int k, inisle = 0;
247 
248  for (k = 0; k < nisles; k++) {
249  if (Vect_point_in_poly(LPoints->x[j], LPoints->y[j], Isles[k]) >= 1) { /* in isle */
250  inisle = 1;
251  break;
252  }
253  }
254 
255  if (!inisle) { /* inside polygon, outside isles -> select */
256  intersect = 1;
257  break;
258  }
259  }
260  }
261  if (intersect) {
262  dig_list_add(List, line);
263  continue;
264  }
265 
266  /* Check intersections of the line with area/isles boundary */
267  /* Outer boundary */
268  if (Vect_line_check_intersection(LPoints, Polygon, 0)) {
269  dig_list_add(List, line);
270  continue;
271  }
272 
273  /* Islands */
274  for (j = 0; j < nisles; j++) {
275  if (Vect_line_check_intersection(LPoints, Isles[j], 0)) {
276  intersect = 1;
277  break;
278  }
279  }
280  if (intersect) {
281  dig_list_add(List, line);
282  }
283  }
284 
285  G_debug(4, " %d lines selected by polygon", List->n_values);
286 
287  return List->n_values;
288 }
289 
290 
307 int
308 Vect_select_areas_by_polygon(struct Map_info *Map, struct line_pnts *Polygon,
309  int nisles, struct line_pnts **Isles,
310  struct ilist *List)
311 {
312  int i, area;
313  static struct ilist *BoundList = NULL;
314 
315  /* TODO: this function was not tested with isles */
316  G_debug(3, "Vect_select_areas_by_polygon() nisles = %d", nisles);
317 
318  List->n_values = 0;
319  if (!BoundList)
320  BoundList = Vect_new_list();
321 
322  /* Select boundaries by polygon */
323  Vect_select_lines_by_polygon(Map, Polygon, nisles, Isles, GV_BOUNDARY,
324  BoundList);
325 
326  /* Add areas on left/right side of selected boundaries */
327  for (i = 0; i < BoundList->n_values; i++) {
328  int line, left, right;
329 
330  line = BoundList->value[i];
331 
332  Vect_get_line_areas(Map, line, &left, &right);
333  G_debug(4, "boundary = %d left = %d right = %d", line, left, right);
334 
335  if (left > 0) {
336  dig_list_add(List, left);
337  }
338  else if (left < 0) { /* island */
339  area = Vect_get_isle_area(Map, abs(left));
340  G_debug(4, " left island -> area = %d", area);
341  if (area > 0)
342  dig_list_add(List, area);
343  }
344 
345  if (right > 0) {
346  dig_list_add(List, right);
347  }
348  else if (right < 0) { /* island */
349  area = Vect_get_isle_area(Map, abs(right));
350  G_debug(4, " right island -> area = %d", area);
351  if (area > 0)
352  dig_list_add(List, area);
353  }
354  }
355 
356  /* But the Polygon may be completely inside the area (only one), in that case
357  * we find the area by one polygon point and add it to the list */
358  area = Vect_find_area(Map, Polygon->x[0], Polygon->y[0]);
359  if (area > 0)
360  dig_list_add(List, area);
361 
362  G_debug(3, " %d areas selected by polygon", List->n_values);
363 
364  return List->n_values;
365 }
int Vect_select_isles_by_box(struct Map_info *Map, BOUND_BOX *Box, struct ilist *list)
Select isles by box.
char * G__getenv(const char *name)
Get environment variable.
Definition: env.c:312
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:272
float Box[8][3]
Vertices for box.
Definition: gsd_objs.c:1420
int debug_level
Definition: core.py:69
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:1527
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:1310
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.