1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/*!
\file sindex.c
\brief Vector library - spatial index
Higher level functions for reading/writing/manipulating vectors.
(C) 2001-2008 by the GRASS Development Team
This program is free software under the
GNU General Public License (>=v2).
Read the file COPYING that comes with GRASS
for details.
\author Radim Blazek
\date 2001
*/
#include <stdlib.h>
#include <string.h>
#include <grass/glocale.h>
#include <grass/gis.h>
#include <grass/Vect.h>
#include <grass/glocale.h>
/*!
\brief Init spatial index
\param si pointer to spatial index structure
\return void
*/
void Vect_spatial_index_init(SPATIAL_INDEX * si)
{
G_debug(1, "Vect_spatial_index_init()");
si->root = RTreeNewIndex();
}
/*!
\brief Destroy existing spatial index
Vect_spatial_index_init() must be call before new use.
\param si pointer to spatial index structure
\return void
*/
void Vect_spatial_index_destroy(SPATIAL_INDEX * si)
{
G_debug(1, "Vect_spatial_index_destroy()");
RTreeDestroyNode(si->root);
}
/*!
\brief Add a new item to spatial index
\param si pointer to spatial index structure
\param id item identifier
\param box pointer to item bounding box
\return void
*/
void Vect_spatial_index_add_item(SPATIAL_INDEX * si, int id, BOUND_BOX * box)
{
struct Rect rect;
G_debug(3, "Vect_spatial_index_add_item(): id = %d", id);
rect.boundary[0] = box->W;
rect.boundary[1] = box->S;
rect.boundary[2] = box->B;
rect.boundary[3] = box->E;
rect.boundary[4] = box->N;
rect.boundary[5] = box->T;
RTreeInsertRect(&rect, id, &(si->root), 0);
}
/*!
\brief Delete item from spatial index
\param si pointer to spatial index structure
\param id item identifier
\return void
*/
void Vect_spatial_index_del_item(SPATIAL_INDEX * si, int id)
{
int ret;
struct Rect rect;
G_debug(3, "Vect_spatial_index_del_item(): id = %d", id);
/* TODO */
G_fatal_error("Vect_spatial_index_del_item() %s", _("not implemented"));
/* Bounding box of item would be needed, which is not stored in si. */
/*
rect.boundary[0] = ; rect.boundary[1] = ; rect.boundary[2] = ;
rect.boundary[3] = ; rect.boundary[4] = ; rect.boundary[5] = ;
*/
ret = RTreeDeleteRect(&rect, id, &(si->root));
if (ret)
G_fatal_error(_("Unable to delete item %d from spatial index"), id);
}
/*!
\brief Create spatial index if necessary.
To be used in modules.
Map must be opened on level 2.
\param Map pointer to vector map
\param out print progress here
\return 0 OK
\return 1 error
*/
int Vect_build_spatial_index(struct Map_info *Map)
{
if (Map->level < 2) {
G_fatal_error(_("Unable to build spatial index from topology, "
"vector map is not opened at topo level 2"));
}
if (!(Map->plus.Spidx_built)) {
return (Vect_build_sidx_from_topo(Map));
}
return 0;
}
/*!
\brief Create spatial index from topo if necessary
\param Map pointer to vector map
\return 0 OK
\return 1 error
*/
int Vect_build_sidx_from_topo(struct Map_info *Map)
{
int i, total, done;
struct Plus_head *plus;
BOUND_BOX box;
P_LINE *Line;
P_NODE *Node;
P_AREA *Area;
P_ISLE *Isle;
G_debug(3, "Vect_build_sidx_from_topo()");
plus = &(Map->plus);
dig_spidx_init(plus);
total = plus->n_nodes + plus->n_lines + plus->n_areas + plus->n_isles;
/* Nodes */
for (i = 1; i <= plus->n_nodes; i++) {
G_percent(i, total, 3);
Node = plus->Node[i];
if (!Node)
G_fatal_error(_("BUG (Vect_build_sidx_from_topo): node does not exist"));
dig_spidx_add_node(plus, i, Node->x, Node->y, Node->z);
}
/* Lines */
done = plus->n_nodes;
for (i = 1; i <= plus->n_lines; i++) {
G_percent(done + i, total, 3);
Line = plus->Line[i];
if (!Line)
G_fatal_error(_("BUG (Vect_build_sidx_from_topo): line does not exist"));
box.N = Line->N;
box.S = Line->S;
box.E = Line->E;
box.W = Line->W;
box.T = Line->T;
box.B = Line->B;
dig_spidx_add_line(plus, i, &box);
}
/* Areas */
done += plus->n_lines;
for (i = 1; i <= plus->n_areas; i++) {
G_percent(done + i, total, 3);
Area = plus->Area[i];
if (!Area)
G_fatal_error(_("BUG (Vect_build_sidx_from_topo): area does not exist"));
box.N = Area->N;
box.S = Area->S;
box.E = Area->E;
box.W = Area->W;
box.T = Area->T;
box.B = Area->B;
dig_spidx_add_area(plus, i, &box);
}
/* Isles */
done += plus->n_areas;
for (i = 1; i <= plus->n_isles; i++) {
G_percent(done + i, total, 3);
Isle = plus->Isle[i];
if (!Isle)
G_fatal_error(_("BUG (Vect_build_sidx_from_topo): isle does not exist"));
box.N = Isle->N;
box.S = Isle->S;
box.E = Isle->E;
box.W = Isle->W;
box.T = Isle->T;
box.B = Isle->B;
dig_spidx_add_isle(plus, i, &box);
}
Map->plus.Spidx_built = 1;
G_debug(3, "Spatial index was built");
return 0;
}
/************************* SELECT BY BOX *********************************/
/* This function is called by RTreeSearch() to add selected item to the list */
static int _add_item(int id, struct ilist *list)
{
dig_list_add(list, id);
return 1;
}
/*!
\brief Select items by bounding box to list
\param si pointer to spatial index structure
\param box bounding box
\param list pointer to list where selected items are stored
\return number of selected items
*/
int
Vect_spatial_index_select(SPATIAL_INDEX * si, BOUND_BOX * box,
struct ilist *list)
{
struct Rect rect;
G_debug(3, "Vect_spatial_index_select()");
list->n_values = 0;
rect.boundary[0] = box->W;
rect.boundary[1] = box->S;
rect.boundary[2] = box->B;
rect.boundary[3] = box->E;
rect.boundary[4] = box->N;
rect.boundary[5] = box->T;
RTreeSearch(si->root, &rect, (void *)_add_item, list);
G_debug(3, " %d items selected", list->n_values);
return (list->n_values);
}
|