GEOS  3.14.0dev
Quadtree.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2006 Refractions Research Inc.
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  * Last port: index/quadtree/Quadtree.java rev. 1.16 (JTS-1.10)
16  *
17  **********************************************************************/
18 
19 #pragma once
20 
21 #include <geos/export.h>
22 #include <geos/geom/Envelope.h>
23 #include <geos/index/SpatialIndex.h> // for inheritance
24 #include <geos/index/quadtree/Root.h> // for composition
25 
26 #include <memory>
27 #include <vector>
28 #include <string>
29 
30 #ifdef _MSC_VER
31 #pragma warning(push)
32 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
33 #endif
34 
35 // Forward declarations
36 namespace geos {
37 namespace index {
38 namespace quadtree {
39 // class Root;
40 }
41 }
42 }
43 
44 namespace geos {
45 namespace index { // geos::index
46 namespace quadtree { // geos::index::quadtree
47 
70 class GEOS_DLL Quadtree: public SpatialIndex {
71 
72 private:
73 
74  std::vector<std::unique_ptr<geom::Envelope>> newEnvelopes;
75 
76  void collectStats(const geom::Envelope& itemEnv);
77 
78  Root root;
79 
91  double minExtent;
92 
93 public:
102  double minExtent);
103 
109  :
110  root(),
111  minExtent(1.0)
112  {}
113 
114  ~Quadtree() override = default;
115 
117  std::size_t depth();
118 
120  std::size_t size();
121 
122  void insert(const geom::Envelope* itemEnv, void* item) override;
123 
141  void query(const geom::Envelope* searchEnv, std::vector<void*>& ret) override;
142 
143 
160  void
161  query(const geom::Envelope* searchEnv, ItemVisitor& visitor) override
162  {
163  /*
164  * the items that are matched are the items in quads which
165  * overlap the search envelope
166  */
167  root.visit(searchEnv, visitor);
168  }
169 
177  bool remove(const geom::Envelope* itemEnv, void* item) override;
178 
180  std::vector<void*>* queryAll();
181 
182  std::string toString() const;
183 
188  Quadtree(const Quadtree&) = delete;
189  Quadtree& operator=(const Quadtree&) = delete;
190 
191 
192  // Move constructor
193  Quadtree(Quadtree&& other) noexcept :
194  newEnvelopes(std::move(other.newEnvelopes)),
195  root(other.root),
196  minExtent(other.minExtent)
197  {
198  // The contents of 'other' are moved to 'this'
199  // 'other' will be empty after the move
200  };
201 
202  // Move assignment operator
203  Quadtree& operator=(Quadtree&& other) noexcept
204  {
205  if (this != &other) {
206  // Move the vector from 'other' to 'this'
207  // 'newEnvelopes.data' will be empty after this operation
208  newEnvelopes.clear();
209  newEnvelopes = std::move(other.newEnvelopes);
210  minExtent = other.minExtent;
211  root = other.root;
212  }
213  return *this;
214  };
215 
216 };
217 
218 } // namespace geos::index::quadtree
219 } // namespace geos::index
220 } // namespace geos
221 
222 #ifdef _MSC_VER
223 #pragma warning(pop)
224 #endif
225 
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:59
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 Quadtree is a spatial index structure for efficient querying of 2D rectangles. If other kinds of sp...
Definition: Quadtree.h:70
std::vector< void * > * queryAll()
Return a list of all items in the Quadtree.
std::size_t size()
Returns the number of items in the tree.
Quadtree(const Quadtree &)=delete
bool remove(const geom::Envelope *itemEnv, void *item) override
std::size_t depth()
Returns the number of levels in the tree.
void query(const geom::Envelope *searchEnv, ItemVisitor &visitor) override
Queries the tree and visits items which may lie in the given search envelope.
Definition: Quadtree.h:161
static geom::Envelope * ensureExtent(const geom::Envelope *itemEnv, double minExtent)
Ensure that the envelope for the inserted item has non-zero extents.
void insert(const geom::Envelope *itemEnv, void *item) override
Adds a spatial item with an extent specified by the given Envelope to the index.
Quadtree()
Constructs a Quadtree with zero items.
Definition: Quadtree.h:108
void query(const geom::Envelope *searchEnv, std::vector< void * > &ret) override
Queries the tree and returns items which may lie in the given search envelope.
QuadRoot is the root of a single Quadtree. It is centred at the origin, and does not have a defined e...
Definition: quadtree/Root.h:48
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25