GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vector/Vlib/find.c
Go to the documentation of this file.
1 
18 #include <math.h>
19 #include <grass/gis.h>
20 #include <grass/Vect.h>
21 
22 #ifndef HUGE_VAL
23 #define HUGE_VAL 9999999999999.0
24 #endif
25 
37 int
38 Vect_find_node(struct Map_info *Map,
39  double ux, double uy, double uz, double maxdist, int with_z)
40 {
41  int i, nnodes, node;
42  BOUND_BOX box;
43  struct ilist *NList;
44  double x, y, z;
45  double cur_dist, dist;
46 
47  G_debug(3, "Vect_find_node() for %f %f %f maxdist = %f", ux, uy, uz,
48  maxdist);
49  NList = Vect_new_list();
50 
51  /* Select all nodes in box */
52  box.N = uy + maxdist;
53  box.S = uy - maxdist;
54  box.E = ux + maxdist;
55  box.W = ux - maxdist;
56  if (with_z) {
57  box.T = uz + maxdist;
58  box.B = uz - maxdist;
59  }
60  else {
61  box.T = HUGE_VAL;
62  box.B = -HUGE_VAL;
63  }
64 
65  nnodes = Vect_select_nodes_by_box(Map, &box, NList);
66  G_debug(3, " %d nodes in box", nnodes);
67 
68  if (nnodes == 0)
69  return 0;
70 
71  /* find nearest */
72  cur_dist = PORT_DOUBLE_MAX;
73  node = 0;
74  for (i = 0; i < nnodes; i++) {
75  Vect_get_node_coor(Map, NList->value[i], &x, &y, &z);
76  dist = Vect_points_distance(ux, uy, uz, x, y, z, with_z);
77  if (dist < cur_dist) {
78  cur_dist = dist;
79  node = i;
80  }
81  }
82  G_debug(3, " nearest node %d in distance %f", NList->value[node],
83  cur_dist);
84 
85  /* Check if in max distance */
86  if (cur_dist <= maxdist)
87  return (NList->value[node]);
88  else
89  return 0;
90 }
91 
108 /* original dig_point_to_line() in grass50 */
109 int
110 Vect_find_line(struct Map_info *map,
111  double ux, double uy, double uz,
112  int type, double maxdist, int with_z, int exclude)
113 {
114  int line;
115  struct ilist *exclude_list;
116 
117  exclude_list = Vect_new_list();
118 
119  Vect_list_append(exclude_list, exclude);
120 
121  line = Vect_find_line_list(map, ux, uy, uz,
122  type, maxdist, with_z, exclude_list, NULL);
123 
124  Vect_destroy_list(exclude_list);
125 
126  return line;
127 }
128 
144 int
145 Vect_find_line_list(struct Map_info *map,
146  double ux, double uy, double uz,
147  int type, double maxdist, int with_z,
148  struct ilist *exclude, struct ilist *found)
149 {
150  int choice;
151  double new_dist;
152  double cur_dist;
153  int gotone;
154  int i, line;
155  static struct line_pnts *Points;
156  static int first_time = 1;
157  struct Plus_head *Plus;
158  BOUND_BOX box;
159  struct ilist *List;
160 
161  G_debug(3, "Vect_find_line_list() for %f %f %f type = %d maxdist = %f",
162  ux, uy, uz, type, maxdist);
163 
164  if (first_time) {
165  Points = Vect_new_line_struct();
166  first_time = 0;
167  }
168 
169  Plus = &(map->plus);
170  gotone = 0;
171  choice = 0;
172  cur_dist = HUGE_VAL;
173 
174  box.N = uy + maxdist;
175  box.S = uy - maxdist;
176  box.E = ux + maxdist;
177  box.W = ux - maxdist;
178  if (with_z) {
179  box.T = uz + maxdist;
180  box.B = uz - maxdist;
181  }
182  else {
183  box.T = PORT_DOUBLE_MAX;
184  box.B = -PORT_DOUBLE_MAX;
185  }
186 
187  List = Vect_new_list();
188 
189  if (found)
190  Vect_reset_list(found);
191 
192  Vect_select_lines_by_box(map, &box, type, List);
193  for (i = 0; i < List->n_values; i++) {
194  line = List->value[i];
195  if (Vect_val_in_list(exclude, line)) {
196  G_debug(3, " line = %d exclude", line);
197  continue;
198  }
199 
200  /* No more needed */
201  /*
202  Line = Plus->Line[line];
203  if ( Line == NULL ) continue;
204  if ( !(type & Line->type) ) continue;
205  */
206 
207  Vect_read_line(map, Points, NULL, line);
208 
209  Vect_line_distance(Points, ux, uy, uz, with_z, NULL, NULL, NULL,
210  &new_dist, NULL, NULL);
211  G_debug(3, " line = %d distance = %f", line, new_dist);
212 
213  if (found && new_dist <= maxdist) {
214  Vect_list_append(found, line);
215  }
216 
217  if ((++gotone == 1) || (new_dist <= cur_dist)) {
218  if (new_dist == cur_dist) {
219  /* TODO */
220  /* choice = dig_center_check (map->Line, choice, a, ux, uy); */
221  continue;
222  }
223 
224  choice = line;
225  cur_dist = new_dist;
226  }
227  }
228 
229  G_debug(3, "min distance found = %f", cur_dist);
230  if (cur_dist > maxdist)
231  choice = 0;
232 
233  Vect_destroy_list(List);
234 
235  return (choice);
236 }
237 
247 /* original dig_point_to_area() in grass50 */
248 int Vect_find_area(struct Map_info *Map, double x, double y)
249 {
250  int i, ret, area;
251  static int first = 1;
252  BOUND_BOX box;
253  static struct ilist *List;
254 
255  G_debug(3, "Vect_find_area() x = %f y = %f", x, y);
256 
257  if (first) {
258  List = Vect_new_list();
259  first = 0;
260  }
261 
262  /* select areas by box */
263  box.E = x;
264  box.W = x;
265  box.N = y;
266  box.S = y;
267  box.T = PORT_DOUBLE_MAX;
268  box.B = -PORT_DOUBLE_MAX;
269  Vect_select_areas_by_box(Map, &box, List);
270  G_debug(3, " %d areas selected by box", List->n_values);
271 
272  for (i = 0; i < List->n_values; i++) {
273  area = List->value[i];
274  ret = Vect_point_in_area(Map, area, x, y);
275 
276  G_debug(3, " area = %d Vect_point_in_area() = %d", area, ret);
277 
278  if (ret >= 1)
279  return (area);
280  }
281 
282  return 0;
283 }
284 
294 /* original dig_point_to_area() in grass50 */
295 int Vect_find_island(struct Map_info *Map, double x, double y)
296 {
297  int i, ret, island, current, current_size, size;
298  static int first = 1;
299  BOUND_BOX box;
300  static struct ilist *List;
301  static struct line_pnts *Points;
302 
303  G_debug(3, "Vect_find_island() x = %f y = %f", x, y);
304 
305  if (first) {
306  List = Vect_new_list();
307  Points = Vect_new_line_struct();
308  first = 0;
309  }
310 
311  /* select islands by box */
312  box.E = x;
313  box.W = x;
314  box.N = y;
315  box.S = y;
316  box.T = PORT_DOUBLE_MAX;
317  box.B = -PORT_DOUBLE_MAX;
318  Vect_select_isles_by_box(Map, &box, List);
319  G_debug(3, " %d islands selected by box", List->n_values);
320 
321  current_size = -1;
322  current = 0;
323  for (i = 0; i < List->n_values; i++) {
324  island = List->value[i];
325  ret = Vect_point_in_island(x, y, Map, island);
326 
327  if (ret >= 1) { /* inside */
328  if (current > 0) { /* not first */
329  if (current_size == -1) { /* second */
331  Vect_get_isle_points(Map, current, Points);
332  current_size =
333  G_area_of_polygon(Points->x, Points->y,
334  Points->n_points);
335  }
336 
337  Vect_get_isle_points(Map, island, Points);
338  size =
339  G_area_of_polygon(Points->x, Points->y, Points->n_points);
340 
341  if (size < current_size) {
342  current = island;
343  current_size = size;
344  }
345  }
346  else { /* first */
347  current = island;
348  }
349  }
350  }
351 
352  return current;
353 }
int Vect_destroy_list(struct ilist *list)
Frees all memory associated with a struct ilist, including the struct itself.
int Vect_select_isles_by_box(struct Map_info *Map, BOUND_BOX *Box, struct ilist *list)
Select isles by box.
int Vect_get_isle_points(struct Map_info *Map, int isle, struct line_pnts *BPoints)
Returns the polygon array of points in BPoints.
double G_area_of_polygon(const double *x, const double *y, int n)
Area in square meters of polygon.
Definition: gis/area.c:165
int Vect_select_nodes_by_box(struct Map_info *Map, BOUND_BOX *Box, struct ilist *list)
Select nodes by box.
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_point_in_island(double X, double Y, struct Map_info *Map, int isle)
Determines if a point (X,Y) is inside an island.
Definition: Vlib/poly.c:760
int Vect_find_line_list(struct Map_info *map, double ux, double uy, double uz, int type, double maxdist, int with_z, struct ilist *exclude, struct ilist *found)
Find the nearest line(s).
int y
Definition: plot.c:34
tuple box
surface = wx.CheckBox(parent = panel, id = wx.ID_ANY, label = _(&quot;Follow source viewpoint&quot;)) pageSizer...
Definition: tools.py:1527
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int Vect_find_area(struct Map_info *Map, double x, double y)
Find the nearest area.
int G_begin_polygon_area_calculations(void)
Begin polygon area calculations.
Definition: gis/area.c:123
int Vect_point_in_area(struct Map_info *Map, int area, double x, double y)
Returns 1 if point is in area.
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
#define HUGE_VAL
int first
Definition: form/open.c:25
int Vect_select_lines_by_box(struct Map_info *Map, BOUND_BOX *Box, int type, struct ilist *list)
Select lines by box.
int Vect_find_node(struct Map_info *Map, double ux, double uy, double uz, double maxdist, int with_z)
Find the nearest node.
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
int Vect_find_island(struct Map_info *Map, double x, double y)
Find the nearest island.
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
double Vect_points_distance(double x1, double y1, double z1, double x2, double y2, double z2, int with_z)
Calculate distance of 2 points.
Definition: line.c:745
int Vect_val_in_list(struct ilist *list, int val)
Find a given item in the list.
int Vect_get_node_coor(struct Map_info *map, int num, double *x, double *y, double *z)
Get node coordinates.
Definition: level_two.c:223
int Vect_line_distance(struct line_pnts *points, double ux, double uy, double uz, int with_z, double *px, double *py, double *pz, double *dist, double *spdist, double *lpdist)
calculate line distance.
Definition: line.c:631
int Vect_find_line(struct Map_info *map, double ux, double uy, double uz, int type, double maxdist, int with_z, int exclude)
Find the nearest line.
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.