GEOS 3.11.1
SimpleSTRtree.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2020 Paul Ramsey
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************/
14
15#pragma once
16
17#include <geos/export.h>
18#include <geos/index/SpatialIndex.h> // for inheritance
19#include <geos/geom/Envelope.h>
20#include <geos/index/strtree/SimpleSTRnode.h>
21
22#include <vector>
23#include <utility>
24
25
26#ifdef _MSC_VER
27#pragma warning(push)
28#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
29#endif
30
31
32// Forward declarations
33namespace geos {
34namespace geom {
35class Geometry;
36class Envelope;
37}
38namespace index {
39namespace strtree {
40class ItemDistance;
41}
42}
43}
44
45
46namespace geos {
47namespace index { // geos::index
48namespace strtree { // geos::index::strtree
49
65class GEOS_DLL SimpleSTRtree: public SpatialIndex {
66
67
68private:
69
70 /* Members */
71 std::deque<SimpleSTRnode> nodesQue;
72 std::vector<SimpleSTRnode*> nodes;
73 std::size_t nodeCapacity;
74 bool built;
75
76 /*
77 * Allocate node in nodesQue std::deque for memory locality,
78 * return reference to node.
79 */
80 SimpleSTRnode* createNode(int newLevel, const geom::Envelope* itemEnv, void* item);
81 SimpleSTRnode* createNode(int newLevel);
82
83
84 void build();
85
86 static void sortNodesY(std::vector<SimpleSTRnode*>& nodeList);
87 static void sortNodesX(std::vector<SimpleSTRnode*>& nodeList);
88
89 void query(const geom::Envelope* searchEnv, const SimpleSTRnode* node, ItemVisitor& visitor);
90 void query(const geom::Envelope* searchEnv, const SimpleSTRnode* node, std::vector<void*>& matches);
91
92 /* Turn off copy constructors for MSVC */
93 SimpleSTRtree(const SimpleSTRtree&) = delete;
94 SimpleSTRtree& operator=(const SimpleSTRtree&) = delete;
95
96 std::vector<SimpleSTRnode*> createHigherLevels(
97 std::vector<SimpleSTRnode*>& nodesOfALevel, int level);
98
99 void addParentNodesFromVerticalSlice(
100 std::vector<SimpleSTRnode*>& verticalSlice,
101 int newLevel,
102 std::vector<SimpleSTRnode*>& parentNodes);
103
104 std::vector<SimpleSTRnode*> createParentNodes(
105 std::vector<SimpleSTRnode*>& childNodes,
106 int newLevel);
107
108 bool remove(const geom::Envelope* searchBounds, SimpleSTRnode* node, void* item);
109
110
111public:
112
113 /* Member */
114 SimpleSTRnode* root;
115
120 SimpleSTRtree(std::size_t capacity = 10)
121 : nodeCapacity(capacity)
122 , built(false)
123 , root(nullptr)
124 {};
125
126 std::size_t getNodeCapacity() const {
127 return nodeCapacity;
128 }
129
130 std::size_t getNumLeafNodes() const {
131 if (!root)
132 return 0;
133 else
134 return root->getNumLeafNodes();
135 }
136
137
138
139 bool getBuilt() const {
140 return built;
141 }
142
143 SimpleSTRnode* getRoot() {
144 build();
145 return root;
146 }
147
148 void insert(geom::Geometry* geom);
149
150 void insert(const geom::Envelope* itemEnv, void* item) override;
151
152 void iterate(ItemVisitor& visitor);
153
154 void query(const geom::Envelope* searchEnv, std::vector<void*>& matches) override;
155
156 void query(const geom::Envelope* searchEnv, ItemVisitor& visitor) override;
157
158 bool remove(const geom::Envelope* searchBounds, void* item) override;
159
160 friend std::ostream& operator<<(std::ostream& os, const SimpleSTRtree& tree);
161
162
163 /*********************************************************************************/
164 /* Nearest neighbor searches, public API */
165
166 /* Internal nearest node to node */
167 std::pair<const void*, const void*> nearestNeighbour(ItemDistance* itemDist);
168
169 /* Nearest to another geometry/item */
170 const void* nearestNeighbour(const geom::Envelope* env, const void* item, ItemDistance* itemDist);
171
172 /* Nearest to another tree */
173 std::pair<const void*, const void*> nearestNeighbour(SimpleSTRtree& tree, ItemDistance* itemDist);
174
175 /* Radius test */
176 bool isWithinDistance(SimpleSTRtree& tree, ItemDistance* itemDist, double maxDistance);
177
178
179};
180
181} // namespace geos::index::strtree
182} // namespace geos::index
183} // namespace geos
184
185
186#ifdef _MSC_VER
187#pragma warning(pop)
188#endif
189
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:58
A visitor for items in an index.
Definition: ItemVisitor.h:28
Abstract class defines basic insertion and query operations supported by classes implementing spatial...
Definition: SpatialIndex.h:46
A function method which computes the distance between two ItemBoundables in an STRtree....
Definition: ItemDistance.h:33
A node of the STR tree.
Definition: SimpleSTRnode.h:37
A query-only R-tree created using the Sort-Tile-Recursive (STR) algorithm. For two-dimensional spatia...
Definition: SimpleSTRtree.h:65
bool remove(const geom::Envelope *searchBounds, void *item) override
Removes a single item from the tree.
SimpleSTRtree(std::size_t capacity=10)
Definition: SimpleSTRtree.h:120
void query(const geom::Envelope *searchEnv, ItemVisitor &visitor) override
Queries the index for all items whose extents intersect the given search Envelope and applies an Item...
void query(const geom::Envelope *searchEnv, std::vector< void * > &matches) override
Queries the index for all items whose extents intersect the given search Envelope.
void insert(const geom::Envelope *itemEnv, void *item) override
Adds a spatial item with an extent specified by the given Envelope to the index.
Basic namespace for all GEOS functionalities.
Definition: geos.h:39