libspatialindex API Reference  (git-trunk)
src/tprtree/TPRTree.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 TPRTree
38  {
39  class TPRTree : 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  // Horizon VT_DOUBLE Horizon. Default is 20.0.
55  // TreeVariant VT_LONG Can be one of Linear, Quadratic or Rstar. Default is Rstar
56  // NearMinimumOverlapFactor VT_ULONG Default is 32.
57  // SplitDistributionFactor VT_DOUBLE Default is 0.4
58  // ReinsertFactor VT_DOUBLE Default is 0.3
59  // EnsureTightMBRs VT_BOOL Default is true
60  // IndexPoolCapacity VT_LONG Default is 100
61  // LeafPoolCapacity VT_LONG Default is 100
62  // RegionPoolCapacity VT_LONG Default is 1000
63  // PointPoolCapacity VT_LONG Default is 500
64 
65  ~TPRTree() ;
66 
67  //
68  // ISpatialIndex interface
69  //
70  virtual void insertData(uint32_t len, const uint8_t* pData, const IShape& shape, id_type shapeIdentifier) ;
71  virtual bool deleteData(const IShape& shape, id_type id) ;
72  virtual void internalNodesQuery(const IShape& query, IVisitor& v) ;
73  virtual void containsWhatQuery(const IShape& query, IVisitor& v) ;
74  virtual void intersectsWithQuery(const IShape& query, IVisitor& v) ;
75  virtual void pointLocationQuery(const Point& query, IVisitor& v) ;
76  virtual void nearestNeighborQuery(uint32_t k, const IShape& query, IVisitor& v, INearestNeighborComparator&) ;
77  virtual void nearestNeighborQuery(uint32_t k, const IShape& query, IVisitor& v) ;
78  virtual void selfJoinQuery(const IShape& s, IVisitor& v) ;
79  virtual void queryStrategy(IQueryStrategy& qs) ;
80  virtual void getIndexProperties(Tools::PropertySet& out) const ;
81  virtual void addCommand(ICommand* pCommand, CommandType ct) ;
82  virtual bool isIndexValid() ;
83  virtual void getStatistics(IStatistics** out) const ;
84  virtual void flush() ;
85 
86  private:
87  void initNew(Tools::PropertySet&);
88  void initOld(Tools::PropertySet& ps);
89  void storeHeader();
90  void loadHeader();
91 
92  void insertData_impl(uint32_t dataLength, uint8_t* pData, MovingRegion& mbr, id_type id);
93  void insertData_impl(uint32_t dataLength, uint8_t* pData, MovingRegion& mbr, id_type id, uint32_t level, uint8_t* overflowTable);
94  bool deleteData_impl(const MovingRegion& mbr, id_type id);
95 
96  id_type writeNode(Node*);
97  NodePtr readNode(id_type id);
98  void deleteNode(Node*);
99 
100  void rangeQuery(RangeQueryType type, const IShape& query, IVisitor& v);
101 
102  IStorageManager* m_pStorageManager;
103 
104  id_type m_rootID, m_headerID;
105 
106  TPRTreeVariant m_treeVariant;
107 
108  double m_fillFactor;
109 
110  uint32_t m_indexCapacity;
111 
112  uint32_t m_leafCapacity;
113 
114  uint32_t m_nearMinimumOverlapFactor;
115  // The R*-Tree 'p' constant, for calculating nearly minimum overlap cost.
116  // [Beckmann, Kriegel, Schneider, Seeger 'The R*-tree: An efficient and Robust Access Method
117  // for Points and Rectangles', Section 4.1]
118 
119  double m_splitDistributionFactor;
120  // The R*-Tree 'm' constant, for calculating spliting distributions.
121  // [Beckmann, Kriegel, Schneider, Seeger 'The R*-tree: An efficient and Robust Access Method
122  // for Points and Rectangles', Section 4.2]
123 
124  double m_reinsertFactor;
125  // The R*-Tree 'p' constant, for removing entries at reinserts.
126  // [Beckmann, Kriegel, Schneider, Seeger 'The R*-tree: An efficient and Robust Access Method
127  // for Points and Rectangles', Section 4.3]
128 
129  uint32_t m_dimension;
130 
131  MovingRegion m_infiniteRegion;
132 
133  Statistics m_stats;
134 
135  bool m_bTightMBRs;
136 
137  double m_currentTime;
138 
139  double m_horizon;
140 
141  Tools::PointerPool<Point> m_pointPool;
143  Tools::PointerPool<Node> m_indexPool;
144  Tools::PointerPool<Node> m_leafPool;
145 
146  std::vector<std::shared_ptr<ICommand> > m_writeNodeCommands;
147  std::vector<std::shared_ptr<ICommand> > m_readNodeCommands;
148  std::vector<std::shared_ptr<ICommand> > m_deleteNodeCommands;
149 
150  class NNEntry
151  {
152  public:
153  id_type m_id;
154  IEntry* m_pEntry;
155  double m_minDist;
156 
157  NNEntry(id_type id, IEntry* e, double f) : m_id(id), m_pEntry(e), m_minDist(f) {}
158  ~NNEntry() = default;
159 
160  }; // NNEntry
161 
162  class NNComparator : public INearestNeighborComparator
163  {
164  public:
165  double getMinimumDistance(const IShape& query, const IShape& entry)
166  {
167  return query.getMinimumDistance(entry);
168  }
169 
170  double getMinimumDistance(const IShape& query, const IData& data)
171  {
172  IShape* pS;
173  data.getShape(&pS);
174  double ret = query.getMinimumDistance(*pS);
175  delete pS;
176  return ret;
177  }
178  }; // NNComparator
179 
180  class ValidateEntry
181  {
182  public:
183  ValidateEntry(MovingRegion& r, NodePtr& pNode) : m_parentMBR(r), m_pNode(pNode) {}
184 
185  MovingRegion m_parentMBR;
186  NodePtr m_pNode;
187  }; // ValidateEntry
188 
189  friend class Node;
190  friend class Leaf;
191  friend class Index;
192 
193  friend std::ostream& operator<<(std::ostream& os, const TPRTree& t);
194  }; // TPRTree
195 
196  std::ostream& operator<<(std::ostream& os, const TPRTree& t);
197  }
198 }
virtual void selfJoinQuery(const IShape &s, IVisitor &v)
Definition: TPRTree.cc:359
virtual void insertData(uint32_t len, const uint8_t *pData, const IShape &shape, id_type shapeIdentifier)
Definition: TPRTree.cc:252
virtual void containsWhatQuery(const IShape &query, IVisitor &v)
Definition: TPRTree.cc:328
virtual void addCommand(ICommand *pCommand, CommandType ct)
Definition: TPRTree.cc:456
virtual bool deleteData(const IShape &shape, id_type id)
Definition: TPRTree.cc:293
virtual double getMinimumDistance(const IShape &in) const =0
friend std::ostream & operator<<(std::ostream &os, const TPRTree &t)
virtual void nearestNeighborQuery(uint32_t k, const IShape &query, IVisitor &v, INearestNeighborComparator &)
Definition: TPRTree.cc:347
virtual void getStatistics(IStatistics **out) const
Definition: TPRTree.cc:576
virtual void queryStrategy(IQueryStrategy &qs)
Definition: TPRTree.cc:364
virtual void intersectsWithQuery(const IShape &query, IVisitor &v)
Definition: TPRTree.cc:334
virtual void getIndexProperties(Tools::PropertySet &out) const
Definition: TPRTree.cc:376
virtual void internalNodesQuery(const IShape &query, IVisitor &v)
Definition: TPRTree.cc:323
virtual void pointLocationQuery(const Point &query, IVisitor &v)
Definition: TPRTree.cc:340
virtual bool isIndexValid()
Definition: TPRTree.cc:472
int64_t id_type
Definition: SpatialIndex.h:41
TPRTree(IStorageManager &, Tools::PropertySet &)
Definition: TPRTree.cc:204
virtual void getShape(IShape **out) const =0