GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
vector/Vlib/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 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Radim Blazek
12 */
13
14#include <stdlib.h>
15#include <unistd.h>
16#include <sys/stat.h>
17#include <string.h>
18#include <grass/vector.h>
19#include <grass/glocale.h>
20
21/*!
22 \brief Initialize spatial index structure
23
24 \param si pointer to spatial index structure
25
26 \return void
27 */
28void Vect_spatial_index_init(struct spatial_index *si, int with_z)
29{
30 G_debug(1, "Vect_spatial_index_init()");
31
32 si->si_tree = RTreeCreateTree(-1, 0, 2 + (with_z != 0));
33}
34
35/*!
36 \brief Destroy existing spatial index
37
38 Vect_spatial_index_init() must be call before new use.
39
40 \param si pointer to spatial index structure
41
42 \return void
43 */
45{
46 G_debug(1, "Vect_spatial_index_destroy()");
47
48 RTreeDestroyTree(si->si_tree);
49}
50
51/*!
52 \brief Add a new item to spatial index structure
53
54 \param[in,out] si pointer to spatial index structure
55 \param id item identifier
56 \param box pointer to item bounding box
57
58 \return void
59 */
61 const struct bound_box *box)
62{
63 static struct RTree_Rect rect;
64 static int rect_init = 0;
65
66 if (!rect_init) {
67 rect.boundary = G_malloc(si->si_tree->nsides_alloc * sizeof(RectReal));
68 rect_init = si->si_tree->nsides_alloc;
69 }
70
71 G_debug(3, "Vect_spatial_index_add_item(): id = %d", id);
72
73 rect.boundary[0] = box->W;
74 rect.boundary[1] = box->S;
75 rect.boundary[2] = box->B;
76 rect.boundary[3] = box->E;
77 rect.boundary[4] = box->N;
78 rect.boundary[5] = box->T;
79 RTreeInsertRect(&rect, id, si->si_tree);
80}
81
82/*!
83 \brief Delete item from spatial index structure
84
85 \param[in,out] si pointer to spatial index structure
86 \param id item identifier
87
88 \return void
89 */
91 const struct bound_box *box)
92{
93 int ret;
94 static struct RTree_Rect rect;
95 static int rect_init = 0;
96
97 if (!rect_init) {
98 rect.boundary = G_malloc(si->si_tree->nsides_alloc * sizeof(RectReal));
99 rect_init = si->si_tree->nsides_alloc;
100 }
101
102 G_debug(3, "Vect_spatial_index_del_item(): id = %d", id);
103
104 rect.boundary[0] = box->W;
105 rect.boundary[1] = box->S;
106 rect.boundary[2] = box->B;
107 rect.boundary[3] = box->E;
108 rect.boundary[4] = box->N;
109 rect.boundary[5] = box->T;
110
111 ret = RTreeDeleteRect(&rect, id, si->si_tree);
112
113 if (ret)
114 G_fatal_error(_("Unable to delete item %d from spatial index"), id);
115}
116
117/************************* SELECT BY BOX *********************************/
118/* This function is called by RTreeSearch() to add selected item to the list */
119static int _add_item(int id, const struct RTree_Rect *rect G_UNUSED,
120 struct ilist *list)
121{
122 G_ilist_add(list, id);
123 return 1;
124}
125
126/*!
127 \brief Select items by bounding box to list
128
129 \param si pointer to spatial index structure
130 \param box bounding box
131 \param[out] list pointer to list where selected items are stored
132
133 \return number of selected items
134 */
136 const struct bound_box *box, struct ilist *list)
137{
138 static struct RTree_Rect rect;
139 static int rect_init = 0;
140
141 if (!rect_init) {
142 rect.boundary = G_malloc(si->si_tree->nsides_alloc * sizeof(RectReal));
143 rect_init = si->si_tree->nsides_alloc;
144 }
145
147
148 rect.boundary[0] = box->W;
149 rect.boundary[1] = box->S;
150 rect.boundary[2] = box->B;
151 rect.boundary[3] = box->E;
152 rect.boundary[4] = box->N;
153 rect.boundary[5] = box->T;
154 RTreeSearch(si->si_tree, &rect, (SearchHitCallback *)_add_item, list);
155
156 G_debug(3, "Vect_spatial_index_select(): %d items selected",
157 list->n_values);
158
159 return list->n_values;
160}
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
void G_ilist_add(struct ilist *, int)
Add item to ilist.
Definition ilist.c:75
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_reset_list(struct ilist *)
Reset ilist structure.
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#define _(str)
Definition glocale.h:10
int SearchHitCallback(int id, const struct RTree_Rect *rect, void *arg)
Definition rtree.h:83
RectReal * boundary
Definition rtree.h:52
Bounding box.
Definition dig_structs.h:62
double W
West.
Definition dig_structs.h:78
double T
Top.
Definition dig_structs.h:82
double S
South.
Definition dig_structs.h:70
double N
North.
Definition dig_structs.h:66
double E
East.
Definition dig_structs.h:74
double B
Bottom.
Definition dig_structs.h:86
List of integers.
Definition gis.h:712
Definition manage.h:4
Spatial index info.
void Vect_spatial_index_destroy(struct spatial_index *si)
Destroy existing spatial index.
void Vect_spatial_index_init(struct spatial_index *si, int with_z)
Initialize spatial index structure.
void Vect_spatial_index_del_item(struct spatial_index *si, int id, const struct bound_box *box)
Delete item from spatial index structure.
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.
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.
int RTreeDeleteRect(struct RTree_Rect *r, int tid, struct RTree *t)
Delete an item from a R*-Tree.
struct RTree * RTreeCreateTree(int fd, off_t rootpos, int ndims)
Create new empty R*-Tree.
int RTreeInsertRect(struct RTree_Rect *r, int tid, struct RTree *t)
Insert an item into a R*-Tree.
void RTreeDestroyTree(struct RTree *t)
Destroy an R*-Tree.
int RTreeSearch(struct RTree *t, struct RTree_Rect *r, SearchHitCallback *shcb, void *cbarg)
Search an R*-Tree.