libspatialindex API Reference  (git-trunk)
src/rtree/RTree.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libspatialindex - A C++ library for spatial indexing
3  * Author: Marios Hadjieleftheriou, mhadji@gmail.com
4  ******************************************************************************
5  * Copyright (c) 2002, Marios Hadjieleftheriou
6  *
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26 ******************************************************************************/
27 
28 #pragma once
29 
30 #include "Statistics.h"
31 #include "Node.h"
32 #include "PointerPoolNode.h"
33 #include <memory>
34 
35 namespace SpatialIndex
36 {
37  namespace RTree
38  {
39  class RTree : public ISpatialIndex
40  {
41  //class NNEntry;
42 
43  public:
45  // String Value Description
46  // ----------------------------------------------
47  // IndexIndentifier VT_LONG If specified an existing index will be openened from the supplied
48  // storage manager with the given index id. Behaviour is unspecified
49  // if the index id or the storage manager are incorrect.
50  // Dimension VT_ULONG Dimensionality of the data that will be inserted.
51  // IndexCapacity VT_ULONG The index node capacity. Default is 100.
52  // LeafCapactiy VT_ULONG The leaf node capacity. Default is 100.
53  // FillFactor VT_DOUBLE The fill factor. Default is 70%
54  // TreeVariant VT_LONG Can be one of Linear, Quadratic or Rstar. Default is Rstar
55  // NearMinimumOverlapFactor VT_ULONG Default is 32.
56  // SplitDistributionFactor VT_DOUBLE Default is 0.4
57  // ReinsertFactor VT_DOUBLE Default is 0.3
58  // EnsureTightMBRs VT_BOOL Default is true
59  // IndexPoolCapacity VT_LONG Default is 100
60  // LeafPoolCapacity VT_LONG Default is 100
61  // RegionPoolCapacity VT_LONG Default is 1000
62  // PointPoolCapacity VT_LONG Default is 500
63 
64  ~RTree() ;
65 
66 
67 
68  //
69  // ISpatialIndex interface
70  //
71  virtual void insertData(uint32_t len, const uint8_t* pData, const IShape& shape, id_type shapeIdentifier) ;
72  virtual bool deleteData(const IShape& shape, id_type id) ;
73  virtual void internalNodesQuery(const IShape& query, IVisitor& v) ;
74  virtual void containsWhatQuery(const IShape& query, IVisitor& v) ;
75  virtual void intersectsWithQuery(const IShape& query, IVisitor& v) ;
76  virtual void pointLocationQuery(const Point& query, IVisitor& v) ;
77  virtual void nearestNeighborQuery(uint32_t k, const IShape& query, IVisitor& v, INearestNeighborComparator&);
78  virtual void nearestNeighborQuery(uint32_t k, const IShape& query, IVisitor& v);
79  virtual void selfJoinQuery(const IShape& s, IVisitor& v) ;
80  virtual void queryStrategy(IQueryStrategy& qs) ;
81  virtual void getIndexProperties(Tools::PropertySet& out) const ;
82  virtual void addCommand(ICommand* pCommand, CommandType ct) ;
83  virtual bool isIndexValid() ;
84  virtual void getStatistics(IStatistics** out) const ;
85  virtual void flush() ;
86 
87  private:
88  void initNew(Tools::PropertySet&);
89  void initOld(Tools::PropertySet& ps);
90  void storeHeader();
91  void loadHeader();
92 
93  void insertData_impl(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id);
94  void insertData_impl(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id, uint32_t level, uint8_t* overflowTable);
95  bool deleteData_impl(const Region& mbr, id_type id);
96 
97  id_type writeNode(Node*);
98  NodePtr readNode(id_type page);
99  void deleteNode(Node*);
100 
101  void rangeQuery(RangeQueryType type, const IShape& query, IVisitor& v);
102  void selfJoinQuery(id_type id1, id_type id2, const Region& r, IVisitor& vis);
103  void visitSubTree(NodePtr subTree, IVisitor& v);
104 
105  IStorageManager* m_pStorageManager;
106 
107  id_type m_rootID, m_headerID;
108 
109  RTreeVariant m_treeVariant;
110 
111  double m_fillFactor;
112 
113  uint32_t m_indexCapacity;
114 
115  uint32_t m_leafCapacity;
116 
117  uint32_t m_nearMinimumOverlapFactor;
118  // The R*-Tree 'p' constant, for calculating nearly minimum overlap cost.
119  // [Beckmann, Kriegel, Schneider, Seeger 'The R*-tree: An efficient and Robust Access Method
120  // for Points and Rectangles', Section 4.1]
121 
122  double m_splitDistributionFactor;
123  // The R*-Tree 'm' constant, for calculating spliting distributions.
124  // [Beckmann, Kriegel, Schneider, Seeger 'The R*-tree: An efficient and Robust Access Method
125  // for Points and Rectangles', Section 4.2]
126 
127  double m_reinsertFactor;
128  // The R*-Tree 'p' constant, for removing entries at reinserts.
129  // [Beckmann, Kriegel, Schneider, Seeger 'The R*-tree: An efficient and Robust Access Method
130  // for Points and Rectangles', Section 4.3]
131 
132  uint32_t m_dimension;
133 
134  Region m_infiniteRegion;
135 
136  Statistics m_stats;
137 
138  bool m_bTightMBRs;
139 
140  Tools::PointerPool<Point> m_pointPool;
141  Tools::PointerPool<Region> m_regionPool;
142  Tools::PointerPool<Node> m_indexPool;
143  Tools::PointerPool<Node> m_leafPool;
144 
145  std::vector<std::shared_ptr<ICommand> > m_writeNodeCommands;
146  std::vector<std::shared_ptr<ICommand> > m_readNodeCommands;
147  std::vector<std::shared_ptr<ICommand> > m_deleteNodeCommands;
148 
149  class NNEntry
150  {
151  public:
152  id_type m_id;
153  IEntry* m_pEntry;
154  double m_minDist;
155 
156  NNEntry(id_type id, IEntry* e, double f) : m_id(id), m_pEntry(e), m_minDist(f) {}
157  ~NNEntry() = default;
158 
159  }; // NNEntry
160 
161  class NNComparator : public INearestNeighborComparator
162  {
163  public:
164  double getMinimumDistance(const IShape& query, const IShape& entry)
165  {
166  return query.getMinimumDistance(entry);
167  }
168 
169  double getMinimumDistance(const IShape& query, const IData& data)
170  {
171  IShape* pS;
172  data.getShape(&pS);
173  double ret = query.getMinimumDistance(*pS);
174  delete pS;
175  return ret;
176  }
177  }; // NNComparator
178 
179  class ValidateEntry
180  {
181  public:
182  ValidateEntry(Region& r, NodePtr& pNode) : m_parentMBR(r), m_pNode(pNode) {}
183 
184  Region m_parentMBR;
185  NodePtr m_pNode;
186  }; // ValidateEntry
187 
188  friend class Node;
189  friend class Leaf;
190  friend class Index;
191  friend class BulkLoader;
192 
193  friend std::ostream& operator<<(std::ostream& os, const RTree& t);
194  }; // RTree
195 
196  std::ostream& operator<<(std::ostream& os, const RTree& t);
197  }
198 }
virtual void getShape(IShape **out) const =0
virtual double getMinimumDistance(const IShape &in) const =0
virtual bool deleteData(const IShape &shape, id_type id)
Definition: RTree.cc:424
virtual void getStatistics(IStatistics **out) const
Definition: RTree.cc:841
virtual void queryStrategy(IQueryStrategy &qs)
Definition: RTree.cc:646
RTree(IStorageManager &, Tools::PropertySet &)
Definition: RTree.cc:358
virtual void addCommand(ICommand *pCommand, CommandType ct)
Definition: RTree.cc:733
virtual void flush()
Definition: RTree.cc:846
virtual void getIndexProperties(Tools::PropertySet &out) const
Definition: RTree.cc:658
virtual void pointLocationQuery(const Point &query, IVisitor &v)
Definition: RTree.cc:558
virtual void internalNodesQuery(const IShape &query, IVisitor &v)
Definition: RTree.cc:436
virtual void containsWhatQuery(const IShape &query, IVisitor &v)
Definition: RTree.cc:500
friend std::ostream & operator<<(std::ostream &os, const RTree &t)
virtual bool isIndexValid()
Definition: RTree.cc:749
virtual void nearestNeighborQuery(uint32_t k, const IShape &query, IVisitor &v, INearestNeighborComparator &)
Definition: RTree.cc:565
virtual void intersectsWithQuery(const IShape &query, IVisitor &v)
Definition: RTree.cc:552
virtual void insertData(uint32_t len, const uint8_t *pData, const IShape &shape, id_type shapeIdentifier)
Definition: RTree.cc:404
virtual void selfJoinQuery(const IShape &s, IVisitor &v)
Definition: RTree.cc:636
std::ostream & operator<<(std::ostream &os, const RTree &t)
Definition: RTree.cc:1547
int64_t id_type
Definition: SpatialIndex.h:41