grass  7.8.6
About: GRASS (Geographic Resources Analysis Support System) is a raster- and vector-based GIS, image processing system, graphics production system and spatial modeling system.
  Fossies Dox: grass-7.8.6.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

select.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/select.c
3
4 \brief Vector library - spatial index
5
6 Higher level functions for a custom spatial index.
7
8 (C) 2001-2009 by the GRASS Development Team
9
10 This program is free software under the GNU General Public License
11 (>=v2). Read the file COPYING that comes with GRASS for details.
12
13 \author Radim Blazek
14 */
15
16#include <stdlib.h>
17#include <unistd.h>
18#include <sys/stat.h>
19#include <string.h>
20#include <grass/vector.h>
21#include <grass/glocale.h>
22
23/*!
24 \brief Initialize spatial index structure
25
26 \param si pointer to spatial index structure
27
28 \return void
29 */
30void Vect_spatial_index_init(struct spatial_index * si, int with_z)
31{
32 G_debug(1, "Vect_spatial_index_init()");
33
34 si->si_tree = RTreeCreateTree(-1, 0, 2 + (with_z != 0));
35}
36
37/*!
38 \brief Destroy existing spatial index
39
40 Vect_spatial_index_init() must be call before new use.
41
42 \param si pointer to spatial index structure
43
44 \return void
45 */
47{
48 G_debug(1, "Vect_spatial_index_destroy()");
49
51}
52
53/*!
54 \brief Add a new item to spatial index structure
55
56 \param[in,out] si pointer to spatial index structure
57 \param id item identifier
58 \param box pointer to item bounding box
59
60 \return void
61 */
63 const struct bound_box * box)
64{
65 static struct RTree_Rect rect;
66 static int rect_init = 0;
67
68 if (!rect_init) {
69 rect.boundary = G_malloc(si->si_tree->nsides_alloc * sizeof(RectReal));
70 rect_init = si->si_tree->nsides_alloc;
71 }
72
73 G_debug(3, "Vect_spatial_index_add_item(): id = %d", id);
74
75 rect.boundary[0] = box->W;
76 rect.boundary[1] = box->S;
77 rect.boundary[2] = box->B;
78 rect.boundary[3] = box->E;
79 rect.boundary[4] = box->N;
80 rect.boundary[5] = box->T;
81 RTreeInsertRect(&rect, id, si->si_tree);
82}
83
84/*!
85 \brief Delete item from spatial index structure
86
87 \param[in,out] si pointer to spatial index structure
88 \param id item identifier
89
90 \return void
91 */
93 const struct bound_box * box)
94{
95 int ret;
96 static struct RTree_Rect rect;
97 static int rect_init = 0;
98
99 if (!rect_init) {
100 rect.boundary = G_malloc(si->si_tree->nsides_alloc * sizeof(RectReal));
101 rect_init = si->si_tree->nsides_alloc;
102 }
103
104 G_debug(3, "Vect_spatial_index_del_item(): id = %d", id);
105
106 rect.boundary[0] = box->W;
107 rect.boundary[1] = box->S;
108 rect.boundary[2] = box->B;
109 rect.boundary[3] = box->E;
110 rect.boundary[4] = box->N;
111 rect.boundary[5] = box->T;
112
113 ret = RTreeDeleteRect(&rect, id, si->si_tree);
114
115 if (ret)
116 G_fatal_error(_("Unable to delete item %d from spatial index"), id);
117}
118
119/************************* SELECT BY BOX *********************************/
120/* This function is called by RTreeSearch() to add selected item to the list */
121static int _add_item(int id, const struct RTree_Rect *rect, struct ilist *list)
122{
123 G_ilist_add(list, id);
124 return 1;
125}
126
127/*!
128 \brief Select items by bounding box to list
129
130 \param si pointer to spatial index structure
131 \param box bounding box
132 \param[out] list pointer to list where selected items are stored
133
134 \return number of selected items
135 */
136int Vect_spatial_index_select(const struct spatial_index * si, const struct bound_box * box,
137 struct ilist *list)
138{
139 static struct RTree_Rect rect;
140 static int rect_init = 0;
141
142 if (!rect_init) {
143 rect.boundary = G_malloc(si->si_tree->nsides_alloc * sizeof(RectReal));
144 rect_init = si->si_tree->nsides_alloc;
145 }
146
148
149 rect.boundary[0] = box->W;
150 rect.boundary[1] = box->S;
151 rect.boundary[2] = box->B;
152 rect.boundary[3] = box->E;
153 rect.boundary[4] = box->N;
154 rect.boundary[5] = box->T;
155 RTreeSearch(si->si_tree, &rect, (void *)_add_item, list);
156
157 G_debug(3, "Vect_spatial_index_select(): %d items selected", list->n_values);
158
159 return list->n_values;
160}
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: error.c:160
#define _(str)
Definition: glocale.h:13
void G_ilist_add(struct ilist *list, int val)
Add item to ilist.
Definition: ilist.c:77
double RectReal
Definition: rtree.h:28
unsigned char nsides_alloc
Definition: rtree.h:135
Bounding box.
Definition: dig_structs.h:66
double W
West.
Definition: dig_structs.h:82
double T
Top.
Definition: dig_structs.h:86
double S
South.
Definition: dig_structs.h:74
double N
North.
Definition: dig_structs.h:70
double E
East.
Definition: dig_structs.h:78
double B
Bottom.
Definition: dig_structs.h:90
List of integers.
Definition: gis.h:691
Definition: manage.h:5
Spatial index info.
Definition: dig_structs.h:1800
struct RTree * si_tree
Pointer to the search tree (R*-Tree)
Definition: dig_structs.h:1804
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
Definition: list.c:57
void Vect_spatial_index_destroy(struct spatial_index *si)
Destroy existing spatial index.
Definition: select.c:46
void Vect_spatial_index_init(struct spatial_index *si, int with_z)
Initialize spatial index structure.
Definition: select.c:30
void Vect_spatial_index_del_item(struct spatial_index *si, int id, const struct bound_box *box)
Delete item from spatial index structure.
Definition: select.c:92
static int _add_item(int id, const struct RTree_Rect *rect, struct ilist *list)
Definition: select.c:121
int Vect_spatial_index_select(const struct spatial_index *si, const struct bound_box *box, struct ilist *list)
Select items by bounding box to list.
Definition: select.c:136
void Vect_spatial_index_add_item(struct spatial_index *si, int id, const struct bound_box *box)
Add a new item to spatial index structure.
Definition: select.c:62
int RTreeDeleteRect(struct RTree_Rect *r, int tid, struct RTree *t)
Delete an item from a R*-Tree.
Definition: index.c:342
struct RTree * RTreeCreateTree(int fd, off_t rootpos, int ndims)
Create new empty R*-Tree.
Definition: index.c:61
int RTreeInsertRect(struct RTree_Rect *r, int tid, struct RTree *t)
Insert an item into a R*-Tree.
Definition: index.c:315
void RTreeDestroyTree(struct RTree *t)
Destroy an R*-Tree.
Definition: index.c:222
int RTreeSearch(struct RTree *t, struct RTree_Rect *r, SearchHitCallback *shcb, void *cbarg)
Search an R*-Tree.
Definition: index.c:298