CommonDataStructures.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 terralib/vp/CommonDataStructures.h
22 
23  \brief Utility classes, structures and definitions for Vector Processing.
24  */
25 
26 #ifndef __TERRALIB_VP_INTERNAL_COMMONDATASTRCTURES_H
27 #define __TERRALIB_VP_INTERNAL_COMMONDATASTRCTURES_H
28 
29 //Terralib include files
30 #include "Config.h"
31 
32 #include "../common/STLUtils.h"
33 #include "../geometry/Coord2D.h"
34 #include "../geometry/Envelope.h"
35 
36 //STL include files
37 #include <set>
38 #include <vector>
39 
40 namespace te
41 {
42  namespace dt
43  {
44  class AbstractData;
45  }
46 
47  namespace gm
48  {
49  class Geometry;
50  }
51 
52  namespace vp
53  {
54  //!< Defines an alias for a geometry vector
55  struct SegmentInfoImpl;
56  struct GeometryInfoImpl;
57 
58  /*!
59  \class RTreeIndexData
60 
61  \brief Represents a data to be indexed by the RTree Index
62 
63  \ingroup vp
64 
65  \sa CoordinateSnapper
66  */
67  template<class T>
68  class IndexData
69  {
70  public:
71 
72  T m_data; //!< The templated data
73  std::size_t m_dataIndex; //!< The index of the data inside the data vector
74 
75  //<! Constructor
76  IndexData(const T& data, std::size_t dataIndex)
77  : m_data(data)
78  , m_dataIndex(dataIndex)
79  {
80 
81  }
82  };
83 
84  template<class T>
86  {
87  public:
88 
90 
92  {
93  clear();
94  }
95 
96  void clear()
97  {
99 
100  m_vecData.clear();
101  m_freeIndexes.clear();
102  }
103 
104  std::size_t insert(const T& data)
105  {
106  std::size_t position = 0;
107  if (m_freeIndexes.empty() == true)
108  {
109  //we must add to the end of the list
110  position = m_vecData.size();
111  te::vp::IndexData<T>* indexData = new te::vp::IndexData<T>(data, position);
112  m_vecData.push_back(indexData);
113  }
114  else
115  {
116  //we can get an avaialble position to avoid the vector to grow forever
117  position = *m_freeIndexes.begin();
118  m_freeIndexes.erase(m_freeIndexes.begin());
119 
120  te::vp::IndexData<T>* indexData = new te::vp::IndexData<T>(data, position);
121  m_vecData[position] = indexData;
122  }
123 
124  return position;
125  }
126 
127  void remove(std::size_t dataIndex)
128  {
129  m_freeIndexes.insert(dataIndex);
130 
131  delete m_vecData.at(dataIndex);
132  m_vecData.at(dataIndex) = nullptr;
133  }
134 
135  te::vp::IndexData<T>* getData(std::size_t dataIndex) const
136  {
137  return m_vecData.at(dataIndex);
138  }
139 
140  std::vector<std::size_t> getValidIndexes() const
141  {
142  std::vector<std::size_t> vecValidIndexes;
143  vecValidIndexes.reserve(m_vecData.size());
144 
145  for (std::size_t i = 0; i < m_vecData.size(); ++i)
146  {
147  if (m_vecData.at(i) != nullptr)
148  {
149  vecValidIndexes.push_back(i);
150  }
151  }
152 
153  return vecValidIndexes;
154  }
155 
156  std::vector<std::size_t> getValidIndexes(const std::vector<std::size_t>& vecIndexes) const
157  {
158  if (m_freeIndexes.empty() == true)
159  {
160  return vecIndexes;
161  }
162 
163  std::vector<std::size_t> vecValidIndexes;
164  vecValidIndexes.reserve(vecIndexes.size());
165 
166  std::size_t limit = m_vecData.size();
167 
168  for (std::size_t i = 0; i < vecIndexes.size(); ++i)
169  {
170  std::size_t index = vecIndexes.at(i);
171  if (index < limit)
172  {
173  if (m_vecData.at(index) != nullptr)
174  {
175  vecValidIndexes.push_back(index);
176  }
177  }
178  }
179 
180  return vecValidIndexes;
181  }
182 
183  private:
184  std::set<std::size_t> m_freeIndexes; //!< Stores the list of removed indexes
185  std::vector< IndexData<T>* > m_vecData; //!< The actual data stored
186  };
187 
188  template<class T>
190  {
191  public:
192 
193  IndexReport(const IndexContainer<T>* indexContainer, const std::vector<std::size_t>& vecFilter)
194  : m_indexContainer(indexContainer)
195  , m_vecFilter(vecFilter)
196  {
197 
198  }
199 
200  std::size_t size() const
201  {
202  return m_vecFilter.size();
203  }
204 
205  te::vp::IndexData<T>* getData(std::size_t dataIndex) const
206  {
207  std::size_t position = m_vecFilter.at(dataIndex);
208  return m_indexContainer->getData(position);
209  }
210 
211  std::vector<T> getAllData() const
212  {
213  std::vector<T> vecAllData;
214  vecAllData.reserve(m_vecFilter.size());
215 
216  for (std::size_t i = 0; i < m_vecFilter.size(); ++i)
217  {
218  std::size_t position = m_vecFilter.at(i);
219  vecAllData.push_back(m_indexContainer->getData(position)->m_data);
220  }
221 
222  return vecAllData;
223  }
224 
225  private:
227  std::vector<std::size_t> m_vecFilter;
228  };
229 
231  {
232  public:
233 
234  /*!
235  * \brief Constructor
236  *
237  * \param coord1 The segment start coordinate
238  * \param coord2 The segment end coordinate
239  * \param evenlope The segment envelope
240  * \param polygonIndex The index of the polygon from which this segment came from
241  */
242  SegmentInfo(const te::gm::Coord2D& coord1, const te::gm::Coord2D& coord2, const te::gm::Envelope& envelope, std::size_t geometryIndex);
243 
244  //!< Destructor
245  virtual ~SegmentInfo();
246 
247  //!< Clones the SegmentInfo
248  SegmentInfo* clone() const;
249 
250  //!< Gets the segment start coordinate
251  const te::gm::Coord2D& getCoord1() const;
252 
253  //!< Gets the segment end coordinate
254  const te::gm::Coord2D& getCoord2() const;
255 
256  //!< Gets the segment envelope
258 
259  //!< Gets the geometry index
260  std::size_t getGeometryIndex() const;
261 
262  //!< Sets the geometry index
263  void setGeometryIndex(std::size_t geometryIndex);
264 
265  private:
266  te::gm::Coord2D m_coord1; //! The segment start coordinate
267  te::gm::Coord2D m_coord2; //! The segment end coordinate
268  te::gm::Envelope m_envelope; //! The segment envelope
269  std::size_t m_geometryIndex; //! The index of the geometry from which this segment came from
270  };
271 
273  {
274  public:
275  /*!
276  * \brief Constructor
277  *
278  * \param geometrIndex The index of this geometry in a vector. Can be zero if it is not added to a vector
279  * \param geometry The geometry to be fragmented
280  */
281  GeometryInfo(std::size_t geometryIndex, te::gm::Geometry* geometry);
282 
283  /*!
284  * \brief Constructor
285  *
286  * \param geometrIndex The index of this geometry in a vector. Can be zero if it is not added to a vector
287  * \param geometry The geometry to be fragmented
288  * \param filter The enveope to be used as filter. Only segments intercepting the filter will be fragmented and indexed
289  */
290  GeometryInfo(std::size_t geometryIndex, te::gm::Geometry* geometry, const te::gm::Envelope& filter);
291 
292  //!< Destructor
293  virtual ~GeometryInfo();
294 
295  //!< Searches the segments that interscepts the given filter
296  std::size_t search(const te::gm::Envelope& filter, std::vector<SegmentInfo>& vecResult) const;
297 
298  //!< Gets the list of all segments of the geometry
299  std::vector<SegmentInfo> getAllSegments() const;
300 
301  //!< Gets the geometry
303 
304  //!< Sets the geometry. This will cause the old geometry to be deleted and the index to be recreated
305  void setGeometry(te::gm::Geometry* geometry);
306 
307  private:
308 
309  //!< Initializes the index
310  void init(const te::gm::Envelope& filter);
311 
312  protected:
313  GeometryInfoImpl* m_impl; //!< A internal pointer to the implementation of the class. This is used to avoid exporting dependencies
314  };
315  }
316 }
317 
318 #endif //__TERRALIB_VP_INTERNAL_COMMONDATASTRCTURES_H
te::gm::Envelope
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:52
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::vp::GeometryInfo::m_impl
GeometryInfoImpl * m_impl
A internal pointer to the implementation of the class. This is used to avoid exporting dependencies.
Definition: CommonDataStructures.h:313
te::vp::SegmentInfo::getEnvelope
const te::gm::Envelope & getEnvelope() const
Gets the geometry index.
te::vp::GeometryInfo::GeometryInfo
GeometryInfo(std::size_t geometryIndex, te::gm::Geometry *geometry, const te::gm::Envelope &filter)
Constructor.
te::vp::IndexData
Definition: CommonDataStructures.h:69
te::vp::IndexReport::m_vecFilter
std::vector< std::size_t > m_vecFilter
Definition: CommonDataStructures.h:227
te::vp::IndexContainer
Definition: CommonDataStructures.h:86
te::vp::SegmentInfo::clone
SegmentInfo * clone() const
Gets the segment start coordinate.
te::vp::SegmentInfo::m_coord2
te::gm::Coord2D m_coord2
The segment start coordinate.
Definition: CommonDataStructures.h:267
te::vp::IndexContainer::getValidIndexes
std::vector< std::size_t > getValidIndexes() const
Definition: CommonDataStructures.h:140
te::vp::IndexContainer::~IndexContainer
~IndexContainer()
Definition: CommonDataStructures.h:91
te::vp::IndexContainer::insert
std::size_t insert(const T &data)
Definition: CommonDataStructures.h:104
te::vp::IndexContainer::remove
void remove(std::size_t dataIndex)
Definition: CommonDataStructures.h:127
te::vp::IndexData::m_dataIndex
std::size_t m_dataIndex
The index of the data inside the data vector.
Definition: CommonDataStructures.h:73
te::vp::SegmentInfo::m_envelope
te::gm::Envelope m_envelope
The segment end coordinate.
Definition: CommonDataStructures.h:268
te::vp::IndexData::IndexData
IndexData(const T &data, std::size_t dataIndex)
Definition: CommonDataStructures.h:76
te::vp::IndexContainer::IndexContainer
IndexContainer()
Definition: CommonDataStructures.h:89
te::vp::SegmentInfo::m_coord1
te::gm::Coord2D m_coord1
Definition: CommonDataStructures.h:266
te::vp::SegmentInfo::m_geometryIndex
std::size_t m_geometryIndex
The segment envelope.
Definition: CommonDataStructures.h:269
te::vp::GeometryInfo::getGeometry
const te::gm::Geometry * getGeometry() const
Sets the geometry. This will cause the old geometry to be deleted and the index to be recreated.
te::vp::SegmentInfo::~SegmentInfo
virtual ~SegmentInfo()
Clones the SegmentInfo.
te::common::FreeContents
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map.
Definition: BoostUtils.h:55
TEVPEXPORT
#define TEVPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:61
te::vp::IndexReport::getData
te::vp::IndexData< T > * getData(std::size_t dataIndex) const
Definition: CommonDataStructures.h:205
te::vp::GeometryInfo::GeometryInfo
GeometryInfo(std::size_t geometryIndex, te::gm::Geometry *geometry)
Constructor.
te::vp::IndexReport::m_indexContainer
const IndexContainer< T > * m_indexContainer
Definition: CommonDataStructures.h:226
te::vp::IndexReport
Definition: CommonDataStructures.h:190
te::vp::SegmentInfo::getCoord2
const te::gm::Coord2D & getCoord2() const
Gets the segment envelope.
te::vp::IndexReport::IndexReport
IndexReport(const IndexContainer< T > *indexContainer, const std::vector< std::size_t > &vecFilter)
Definition: CommonDataStructures.h:193
te::vp::SegmentInfo::getGeometryIndex
std::size_t getGeometryIndex() const
Sets the geometry index.
te::vp::IndexContainer::clear
void clear()
Definition: CommonDataStructures.h:96
te::vp::GeometryInfo::~GeometryInfo
virtual ~GeometryInfo()
Searches the segments that interscepts the given filter.
te::vp::GeometryInfo::init
void init(const te::gm::Envelope &filter)
< Initializes the index
te::vp::GeometryInfo::getAllSegments
std::vector< SegmentInfo > getAllSegments() const
Gets the geometry.
te::vp::IndexContainer::m_vecData
std::vector< IndexData< T > * > m_vecData
The actual data stored.
Definition: CommonDataStructures.h:185
te::vp::GeometryInfo::search
std::size_t search(const te::gm::Envelope &filter, std::vector< SegmentInfo > &vecResult) const
Gets the list of all segments of the geometry.
te::gm::Coord2D
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:41
te::vp::IndexReport::getAllData
std::vector< T > getAllData() const
Definition: CommonDataStructures.h:211
te::vp::GeometryInfo
Definition: CommonDataStructures.h:273
te::vp::SegmentInfo::getCoord1
const te::gm::Coord2D & getCoord1() const
Gets the segment end coordinate.
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::vp::IndexContainer::m_freeIndexes
std::set< std::size_t > m_freeIndexes
Stores the list of removed indexes.
Definition: CommonDataStructures.h:184
te::gm::Geometry
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:78
te::vp::GeometryInfo::setGeometry
void setGeometry(te::gm::Geometry *geometry)
te::vp::SegmentInfo
Definition: CommonDataStructures.h:231
te::vp::IndexContainer::getData
te::vp::IndexData< T > * getData(std::size_t dataIndex) const
Definition: CommonDataStructures.h:135
te::vp::IndexReport::size
std::size_t size() const
Definition: CommonDataStructures.h:200
te::vp::SegmentInfo::setGeometryIndex
void setGeometryIndex(std::size_t geometryIndex)
te::vp::SegmentInfo::SegmentInfo
SegmentInfo(const te::gm::Coord2D &coord1, const te::gm::Coord2D &coord2, const te::gm::Envelope &envelope, std::size_t geometryIndex)
Constructor.
te::vp::IndexContainer::getValidIndexes
std::vector< std::size_t > getValidIndexes(const std::vector< std::size_t > &vecIndexes) const
Definition: CommonDataStructures.h:156
te::vp::IndexData::m_data
T m_data
The templated data.
Definition: CommonDataStructures.h:72