KDTreeMultiIndex.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file KDTreeIndex.h
22 
23  \brief This class makes easier and more generic the use of the te::sam::rtree::Index
24 
25  \ingroup vp
26  */
27 
28 #ifndef __TERRALIB_VP_INTERNAL_KDTREEMULTIINDEX_H
29 #define __TERRALIB_VP_INTERNAL_KDTREEMULTIINDEX_H
30 
31 // Terralib
32 #include "Config.h"
33 
34 #include "../common/STLUtils.h"
35 #include "../sam/kdtree/Index.h"
36 #include "CommonDataStructures.h"
37 
38 //STL
39 #include <memory>
40 #include <vector>
41 
42 namespace te
43 {
44  namespace vp
45  {
46  /*!
47  \class KDTreeIndex
48 
49  \brief This class makes easier and more generic the use of the te::sam::kdtree::Index
50 
51  \ingroup vp
52  */
53  template<class T>
55  {
57 
58  public:
59 
60  //!< Constructor
62 
63  //!< Destructor
65 
66  //!< Clears the index
67  void clear();
68 
69  //!< Inserts the given data considering the given envelope as key
70  void insert(const te::gm::Coord2D& coord, const T& data);
71 
72  //!< Removes the given data considering the given envelope as key
73  void remove(const te::gm::Coord2D& coord, std::size_t dataIndex);
74 
75  //!< Gets all data from the KDTreeIndex
76  std::unique_ptr<te::vp::IndexReport<T>> getData() const;
77 
78  //!< Searches from all data consdering the given mbr
79  std::unique_ptr<te::vp::IndexReport<T>> search(const te::gm::Envelope& mbr) const;
80 
81  private:
82  te::sam::kdtree::Index<KD_NODE> m_index; //!< The internal rtree to store the data indexes
83  IndexContainer<T> m_indexContainer; //!< The indexed data container
84  };
85  }
86 }
87 
88 //Implementation
89 template<class T>
91  : m_index(te::gm::Envelope())
92 {
93 
94 }
95 template<class T>
97 {
98  clear();
99 }
100 
101 template<class T>
103 {
104  m_index.clear();
105  m_indexContainer.clear();
106 }
107 
108 
109 template<class T>
110 void te::vp::KDTreeMultiIndex<T>::insert(const te::gm::Coord2D& coord, const T& data)
111 {
112  std::size_t newDataIndex = m_indexContainer.insert(data);
113  m_index.insert(coord, newDataIndex);
114 }
115 
116 template<class T>
117 void te::vp::KDTreeMultiIndex<T>::remove(const te::gm::Coord2D& coord, std::size_t dataIndex)
118 {
119  m_indexContainer.remove(dataIndex);
120 }
121 
122 template<class T>
123 std::unique_ptr<te::vp::IndexReport<T>> te::vp::KDTreeMultiIndex<T>::getData() const
124 {
125  std::vector<std::size_t> vecValidIndexes = m_indexContainer.getValidIndexes();
126 
127  std::unique_ptr<te::vp::IndexReport<T>> indexReport(new te::vp::IndexReport<T>(&m_indexContainer, vecValidIndexes));
128  return indexReport;
129 }
130 
131 template<class T>
132 std::unique_ptr<te::vp::IndexReport<T>> te::vp::KDTreeMultiIndex<T>::search(const te::gm::Envelope& mbr) const
133 {
134  std::vector<KD_NODE*> vecNodesReport;
135  m_index.search(mbr, vecNodesReport);
136 
137  std::vector<std::size_t> vecFullIndexesReport;
138  for (std::size_t i = 0; i < vecNodesReport.size(); ++i)
139  {
140  const std::vector<std::size_t>& vecNodeIndexes = vecNodesReport.at(i)->getData();
141  vecFullIndexesReport.insert(vecFullIndexesReport.end(), vecNodeIndexes.begin(), vecNodeIndexes.end());
142  }
143 
144  std::vector<std::size_t> vecNetIndexesReport = m_indexContainer.getValidIndexes(vecFullIndexesReport);
145 
146  std::unique_ptr<te::vp::IndexReport<T>> indexReport(new te::vp::IndexReport<T>(&m_indexContainer, vecNetIndexesReport));
147  return indexReport;
148 }
149 
150 #endif // __TERRALIB_VP_INTERNAL_KDTREEINDEX_H
te::gm::Envelope
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:52
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::vp::KDTreeMultiIndex::getData
std::unique_ptr< te::vp::IndexReport< T > > getData() const
Searches from all data consdering the given mbr.
Definition: KDTreeMultiIndex.h:123
te::vp::KDTreeMultiIndex::m_index
te::sam::kdtree::Index< KD_NODE > m_index
The internal rtree to store the data indexes.
Definition: KDTreeMultiIndex.h:82
te::vp::KDTreeMultiIndex::clear
void clear()
Inserts the given data considering the given envelope as key.
Definition: KDTreeMultiIndex.h:102
te::vp::IndexContainer
Definition: CommonDataStructures.h:86
te::sam::kdtree::Index
A class that represents a two dimensional K-d Tree (2-d Tree).
Definition: Index.h:68
te::vp::KDTreeMultiIndex::KDTreeMultiIndex
KDTreeMultiIndex()
< Constructor
Definition: KDTreeMultiIndex.h:90
te::vp::KDTreeMultiIndex
Definition: KDTreeMultiIndex.h:55
te::vp::KDTreeMultiIndex::~KDTreeMultiIndex
~KDTreeMultiIndex()
Clears the index.
Definition: KDTreeMultiIndex.h:96
te::sam::kdtree::kd_node_m_dataset_tag
Kd-Tree node type for nodes with multuple elements (used by template instantiation).
Definition: Node.h:39
te::vp::KDTreeMultiIndex::m_indexContainer
IndexContainer< T > m_indexContainer
The indexed data container.
Definition: KDTreeMultiIndex.h:83
te::vp::IndexReport
Definition: CommonDataStructures.h:190
te::vp::KDTreeMultiIndex::remove
void remove(const te::gm::Coord2D &coord, std::size_t dataIndex)
Gets all data from the KDTreeIndex.
Definition: KDTreeMultiIndex.h:117
te::gm::Coord2D
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:41
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::sam::kdtree::Node
A class that represents an Kd-tree node.
Definition: Node.h:62
CommonDataStructures.h
Utility classes, structures and definitions for Vector Processing.
te::vp::KDTreeMultiIndex::search
std::unique_ptr< te::vp::IndexReport< T > > search(const te::gm::Envelope &mbr) const
Definition: KDTreeMultiIndex.h:132
te::vp::KDTreeMultiIndex::insert
void insert(const te::gm::Coord2D &coord, const T &data)
Removes the given data considering the given envelope as key.
Definition: KDTreeMultiIndex.h:110