GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
spindex.c
Go to the documentation of this file.
1 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <grass/gis.h>
20 #include <grass/Vect.h>
21 #include <grass/glocale.h>
22 
31 int dig_spidx_init(struct Plus_head *Plus)
32 {
33 
34  G_debug(1, "dig_spidx_init()");
35 
36  Plus->Node_spidx = RTreeNewIndex();
37  Plus->Line_spidx = RTreeNewIndex();
38  Plus->Area_spidx = RTreeNewIndex();
39  Plus->Isle_spidx = RTreeNewIndex();
40 
41  Plus->Node_spidx_offset = 0L;
42  Plus->Edge_spidx_offset = 0L;
43  Plus->Line_spidx_offset = 0L;
44  Plus->Area_spidx_offset = 0L;
45  Plus->Isle_spidx_offset = 0L;
46  Plus->Volume_spidx_offset = 0L;
47  Plus->Hole_spidx_offset = 0L;
48 
49  return 1;
50 }
51 
57 void dig_spidx_free_nodes(struct Plus_head *Plus)
58 {
59  RTreeDestroyNode(Plus->Node_spidx);
60  Plus->Node_spidx = RTreeNewIndex();
61 }
62 
68 void dig_spidx_free_lines(struct Plus_head *Plus)
69 {
70  RTreeDestroyNode(Plus->Line_spidx);
71  Plus->Line_spidx = RTreeNewIndex();
72 }
73 
79 void dig_spidx_free_areas(struct Plus_head *Plus)
80 {
81  RTreeDestroyNode(Plus->Area_spidx);
82  Plus->Area_spidx = RTreeNewIndex();
83 }
84 
90 void dig_spidx_free_isles(struct Plus_head *Plus)
91 {
92  RTreeDestroyNode(Plus->Isle_spidx);
93  Plus->Isle_spidx = RTreeNewIndex();
94 }
95 
101 void dig_spidx_free(struct Plus_head *Plus)
102 {
103  dig_spidx_free_nodes(Plus);
104  dig_spidx_free_lines(Plus);
105  dig_spidx_free_areas(Plus);
106  dig_spidx_free_isles(Plus);
107 }
108 
119 int
120 dig_spidx_add_node(struct Plus_head *Plus, int node,
121  double x, double y, double z)
122 {
123  struct Rect rect;
124 
125  G_debug(3, "dig_spidx_add_node(): node = %d, x,y,z = %f, %f, %f", node, x,
126  y, z);
127 
128  rect.boundary[0] = x;
129  rect.boundary[1] = y;
130  rect.boundary[2] = z;
131  rect.boundary[3] = x;
132  rect.boundary[4] = y;
133  rect.boundary[5] = z;
134  RTreeInsertRect(&rect, node, &(Plus->Node_spidx), 0);
135 
136  return 1;
137 }
138 
148 int dig_spidx_add_line(struct Plus_head *Plus, int line, BOUND_BOX * box)
149 {
150  struct Rect rect;
151 
152  G_debug(3, "dig_spidx_add_line(): line = %d", line);
153 
154  rect.boundary[0] = box->W;
155  rect.boundary[1] = box->S;
156  rect.boundary[2] = box->B;
157  rect.boundary[3] = box->E;
158  rect.boundary[4] = box->N;
159  rect.boundary[5] = box->T;
160  RTreeInsertRect(&rect, line, &(Plus->Line_spidx), 0);
161 
162  return 0;
163 }
164 
174 int dig_spidx_add_area(struct Plus_head *Plus, int area, BOUND_BOX * box)
175 {
176  struct Rect rect;
177 
178  G_debug(3, "dig_spidx_add_area(): area = %d", area);
179 
180  rect.boundary[0] = box->W;
181  rect.boundary[1] = box->S;
182  rect.boundary[2] = box->B;
183  rect.boundary[3] = box->E;
184  rect.boundary[4] = box->N;
185  rect.boundary[5] = box->T;
186  RTreeInsertRect(&rect, area, &(Plus->Area_spidx), 0);
187 
188  return 0;
189 }
190 
201 int dig_spidx_add_isle(struct Plus_head *Plus, int isle, BOUND_BOX * box)
202 {
203  struct Rect rect;
204 
205  G_debug(3, "dig_spidx_add_isle(): isle = %d", isle);
206 
207  rect.boundary[0] = box->W;
208  rect.boundary[1] = box->S;
209  rect.boundary[2] = box->B;
210  rect.boundary[3] = box->E;
211  rect.boundary[4] = box->N;
212  rect.boundary[5] = box->T;
213  RTreeInsertRect(&rect, isle, &(Plus->Isle_spidx), 0);
214 
215  return 0;
216 }
217 
228 int dig_spidx_del_node(struct Plus_head *Plus, int node)
229 {
230  int ret;
231  P_NODE *Node;
232  struct Rect rect;
233 
234  G_debug(3, "dig_spidx_del_node(): node = %d", node);
235 
236  Node = Plus->Node[node];
237 
238  rect.boundary[0] = Node->x;
239  rect.boundary[1] = Node->y;
240  rect.boundary[2] = Node->z;
241  rect.boundary[3] = Node->x;
242  rect.boundary[4] = Node->y;
243  rect.boundary[5] = Node->z;
244 
245  ret = RTreeDeleteRect(&rect, node, &(Plus->Node_spidx));
246 
247  if (ret)
248  G_fatal_error(_("Unable to delete node %d from spatial index"), node);
249 
250  return 0;
251 }
252 
263 int dig_spidx_del_line(struct Plus_head *Plus, int line)
264 {
265  P_LINE *Line;
266  struct Rect rect;
267  int ret;
268 
269  G_debug(3, "dig_spidx_del_line(): line = %d", line);
270 
271  Line = Plus->Line[line];
272 
273  G_debug(3, " box(x1,y1,z1,x2,y2,z2): %f %f %f %f %f %f", Line->W,
274  Line->S, Line->B, Line->E, Line->N, Line->T);
275 
276  rect.boundary[0] = Line->W;
277  rect.boundary[1] = Line->S;
278  rect.boundary[2] = Line->B;
279  rect.boundary[3] = Line->E;
280  rect.boundary[4] = Line->N;
281  rect.boundary[5] = Line->T;
282 
283  ret = RTreeDeleteRect(&rect, line, &(Plus->Line_spidx));
284 
285  G_debug(3, " ret = %d", ret);
286 
287  if (ret)
288  G_fatal_error(_("Unable to delete line %d from spatial index"), line);
289 
290  return 0;
291 }
292 
303 int dig_spidx_del_area(struct Plus_head *Plus, int area)
304 {
305  int ret;
306  P_AREA *Area;
307  struct Rect rect;
308 
309  G_debug(3, "dig_spidx_del_area(): area = %d", area);
310 
311  Area = Plus->Area[area];
312 
313  if (Area == NULL) {
314  G_fatal_error(_("Attempt to delete sidx for dead area"));
315  }
316 
317  rect.boundary[0] = Area->W;
318  rect.boundary[1] = Area->S;
319  rect.boundary[2] = Area->B;
320  rect.boundary[3] = Area->E;
321  rect.boundary[4] = Area->N;
322  rect.boundary[5] = Area->T;
323 
324  ret = RTreeDeleteRect(&rect, area, &(Plus->Area_spidx));
325 
326  if (ret)
327  G_fatal_error(_("Unable to delete area %d from spatial index"), area);
328 
329  return 0;
330 }
331 
342 int dig_spidx_del_isle(struct Plus_head *Plus, int isle)
343 {
344  int ret;
345  P_ISLE *Isle;
346  struct Rect rect;
347 
348  G_debug(3, "dig_spidx_del_isle(): isle = %d", isle);
349 
350  Isle = Plus->Isle[isle];
351 
352  rect.boundary[0] = Isle->W;
353  rect.boundary[1] = Isle->S;
354  rect.boundary[2] = Isle->B;
355  rect.boundary[3] = Isle->E;
356  rect.boundary[4] = Isle->N;
357  rect.boundary[5] = Isle->T;
358 
359  ret = RTreeDeleteRect(&rect, isle, &(Plus->Isle_spidx));
360 
361  if (ret)
362  G_fatal_error(_("Unable to delete isle %d from spatial index"), isle);
363 
364  return 0;
365 }
366 
367 /* This function is called by RTreeSearch() to add selected node/line/area/isle to thelist */
368 static int _add_item(int id, struct ilist *list)
369 {
370  dig_list_add(list, id);
371  return 1;
372 }
373 
384 int
385 dig_select_nodes(struct Plus_head *Plus, BOUND_BOX * box, struct ilist *list)
386 {
387  struct Rect rect;
388 
389  G_debug(3, "dig_select_nodes()");
390 
391  list->n_values = 0;
392 
393  rect.boundary[0] = box->W;
394  rect.boundary[1] = box->S;
395  rect.boundary[2] = box->B;
396  rect.boundary[3] = box->E;
397  rect.boundary[4] = box->N;
398  rect.boundary[5] = box->T;
399  RTreeSearch(Plus->Node_spidx, &rect, (void *)_add_item, list);
400 
401  return (list->n_values);
402 }
403 
404 /* This function is called by RTreeSearch() for nodes to add selected node to list */
405 static int _add_node(int id, int *node)
406 {
407  *node = id;
408  return 0;
409 }
410 
420 int dig_find_node(struct Plus_head *Plus, double x, double y, double z)
421 {
422  struct Rect rect;
423  struct ilist list;
424  int node;
425 
426  G_debug(3, "dig_find_node()");
427 
428  dig_init_list(&list);
429 
430  rect.boundary[0] = x;
431  rect.boundary[1] = y;
432  rect.boundary[2] = z;
433  rect.boundary[3] = x;
434  rect.boundary[4] = y;
435  rect.boundary[5] = z;
436 
437  node = 0;
438  RTreeSearch(Plus->Node_spidx, &rect, (void *)_add_node, &node);
439 
440  return node;
441 }
442 
452 int
453 dig_select_lines(struct Plus_head *Plus, BOUND_BOX * box, struct ilist *list)
454 {
455  struct Rect rect;
456 
457  G_debug(3, "dig_select_lines()");
458 
459  list->n_values = 0;
460 
461  rect.boundary[0] = box->W;
462  rect.boundary[1] = box->S;
463  rect.boundary[2] = box->B;
464  rect.boundary[3] = box->E;
465  rect.boundary[4] = box->N;
466  rect.boundary[5] = box->T;
467  RTreeSearch(Plus->Line_spidx, &rect, (void *)_add_item, list);
468 
469  return (list->n_values);
470 }
471 
481 int
482 dig_select_areas(struct Plus_head *Plus, BOUND_BOX * box, struct ilist *list)
483 {
484  struct Rect rect;
485 
486  G_debug(3, "dig_select_areas()");
487 
488  list->n_values = 0;
489 
490  rect.boundary[0] = box->W;
491  rect.boundary[1] = box->S;
492  rect.boundary[2] = box->B;
493  rect.boundary[3] = box->E;
494  rect.boundary[4] = box->N;
495  rect.boundary[5] = box->T;
496  RTreeSearch(Plus->Area_spidx, &rect, (void *)_add_item, list);
497 
498  return (list->n_values);
499 }
500 
510 int
511 dig_select_isles(struct Plus_head *Plus, BOUND_BOX * box, struct ilist *list)
512 {
513  struct Rect rect;
514 
515  G_debug(3, "dig_select_isles()");
516 
517  list->n_values = 0;
518 
519  rect.boundary[0] = box->W;
520  rect.boundary[1] = box->S;
521  rect.boundary[2] = box->B;
522  rect.boundary[3] = box->E;
523  rect.boundary[4] = box->N;
524  rect.boundary[5] = box->T;
525  RTreeSearch(Plus->Isle_spidx, &rect, (void *)_add_item, list);
526 
527  return (list->n_values);
528 }
RectReal boundary[NUMSIDES]
Definition: index.h:42
int dig_init_list(struct ilist *list)
void dig_spidx_free(struct Plus_head *Plus)
Free spatial index (nodes, lines, areas, isles)
Definition: spindex.c:101
int dig_list_add(struct ilist *list, int val)
int dig_spidx_add_line(struct Plus_head *Plus, int line, BOUND_BOX *box)
Add new line to spatial index.
Definition: spindex.c:148
int dig_spidx_del_node(struct Plus_head *Plus, int node)
Delete node from spatial index.
Definition: spindex.c:228
int RTreeSearch(struct Node *N, struct Rect *R, SearchHitCallback shcb, void *cbarg)
int dig_spidx_init(struct Plus_head *Plus)
Initit spatial index (nodes, lines, areas, isles)
Definition: spindex.c:31
Definition: index.h:56
void RTreeDestroyNode(struct Node *)
Definition: node.c:217
int RTreeInsertRect(struct Rect *R, int Tid, struct Node **Root, int Level)
int y
Definition: plot.c:34
int dig_select_isles(struct Plus_head *Plus, BOUND_BOX *box, struct ilist *list)
Select isles by box.
Definition: spindex.c:511
void dig_spidx_free_lines(struct Plus_head *Plus)
Free spatial index for lines.
Definition: spindex.c:68
tuple box
surface = wx.CheckBox(parent = panel, id = wx.ID_ANY, label = _(&quot;Follow source viewpoint&quot;)) pageSizer...
Definition: tools.py:1527
void dig_spidx_free_areas(struct Plus_head *Plus)
Free spatial index for areas.
Definition: spindex.c:79
int dig_spidx_add_area(struct Plus_head *Plus, int area, BOUND_BOX *box)
Add new area to spatial index.
Definition: spindex.c:174
int dig_spidx_add_isle(struct Plus_head *Plus, int isle, BOUND_BOX *box)
Add new island to spatial index.
Definition: spindex.c:201
int dig_spidx_del_area(struct Plus_head *Plus, int area)
Delete area from spatial index.
Definition: spindex.c:303
tuple id
self.OnVectorSurface(event)
Definition: tools.py:3426
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
void dig_spidx_free_isles(struct Plus_head *Plus)
Free spatial index for isles.
Definition: spindex.c:90
int dig_spidx_del_isle(struct Plus_head *Plus, int isle)
Delete isle from spatial index.
Definition: spindex.c:342
int dig_spidx_add_node(struct Plus_head *Plus, int node, double x, double y, double z)
Add new node to spatial index.
Definition: spindex.c:120
int dig_select_lines(struct Plus_head *Plus, BOUND_BOX *box, struct ilist *list)
Select lines by box.
Definition: spindex.c:453
return NULL
Definition: dbfopen.c:1394
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
struct Node * RTreeNewIndex(void)
int dig_find_node(struct Plus_head *Plus, double x, double y, double z)
Find one node by coordinates.
Definition: spindex.c:420
int RTreeDeleteRect(struct Rect *R, int Tid, struct Node **Nn)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: index.h:40
void dig_spidx_free_nodes(struct Plus_head *Plus)
Free spatial index for nodes.
Definition: spindex.c:57
int dig_spidx_del_line(struct Plus_head *Plus, int line)
Delete line from spatial index.
Definition: spindex.c:263