All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Repository.cpp
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/edit/Repository.cpp
22 
23  \brief This class represents a repository of geometries and features.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/STLUtils.h"
29 #include "../common/Translator.h"
30 #include "../dataaccess/dataset/ObjectId.h"
31 #include "../datatype/SimpleData.h"
32 #include "../geometry/Coord2D.h"
33 #include "../geometry/Envelope.h"
34 #include "../geometry/Geometry.h"
35 #include "../geometry/Point.h"
36 #include "../geometry/Utils.h"
37 #include "Feature.h"
38 #include "Repository.h"
39 #include "Utils.h"
40 
41 // STL
42 #include <cassert>
43 #include <memory>
44 
45 te::edit::Repository::Repository(const std::string& source, int srid)
46  : m_source(source),
47  m_srid(srid)
48 {
49 }
50 
52 {
53  clear();
54 }
55 
57 {
58  assert(geom);
59 
61 
62  assert(!hasIdentifier(id));
63 
64  add(id, geom);
65 }
66 
68 {
69  assert(id);
70  assert(geom);
71 
72  Feature* f = new Feature(id, geom);
73 
74  add(f);
75 }
76 
78 {
79  assert(f);
80 
81  // Try find the given feature
82  std::size_t pos = getPosition(f->getId());
83 
84  if(pos == std::string::npos) // Not found! Insert
85  {
86  m_features.push_back(f);
87 
88  buildIndex(m_features.size() - 1, f->getGeometry());
89 
90  return;
91  }
92 
93  // Else, set the new values
94  set(pos, f);
95 }
96 
98 {
99  assert(id);
100  assert(geom);
101 
102  Feature* f = new Feature(id, geom);
103 
104  set(f);
105 }
106 
108 {
109  assert(f);
110 
111  // Try find the given feature
112  std::size_t pos = getPosition(f->getId());
113 
114  if(pos == std::string::npos)
115  throw te::common::Exception(TE_TR("Identifier not found!"));
116 
117  set(pos, f);
118 }
119 
121 {
122  assert(id);
123 
124  // Try find the given identifier
125  std::size_t pos = getPosition(id);
126 
127  if(pos == std::string::npos)
128  throw te::common::Exception(TE_TR("Identifier not found!"));
129 
130  // Cleaning...
131  delete m_features[pos];
132 
133  // Removing...
134  m_features.erase(m_features.begin() + pos);
135 
136  // Indexing...
137  buildIndex();
138 }
139 
141 {
142  assert(id);
143 
144  for(std::size_t i = 0; i < m_features.size(); ++i)
145  if(m_features[i]->isEquals(id))
146  return i;
147 
148  return std::string::npos;
149 }
150 
152 {
153  return getPosition(id) != std::string::npos;
154 }
155 
156 const std::string& te::edit::Repository::getSource() const
157 {
158  return m_source;
159 }
160 
161 const std::vector<te::edit::Feature*>& te::edit::Repository::getAllFeatures() const
162 {
163  return m_features;
164 }
165 
166 std::vector<te::edit::Feature*> te::edit::Repository::getNewFeatures() const
167 {
168  std::vector<te::edit::Feature*> result;
169 
170  for(std::size_t i = 0; i < m_features.size(); ++i)
171  {
172  Feature* f = m_features[i];
173  assert(f);
174  assert(f->getId());
175 
176  if(f->getId()->getValueAsString().find(NEW_FEATURE_ID_SUFFIX) != std::string::npos)
177  result.push_back(f);
178  }
179 
180  return result;
181 }
182 
183 std::vector<te::edit::Feature*> te::edit::Repository::getFeatures(const te::gm::Envelope& e, int /*srid*/) const
184 {
185  assert(e.isValid());
186 
187  std::vector<te::edit::Feature*> result;
188 
189  // Search on rtree
190  std::vector<std::size_t> report;
191  m_rtree.search(e, report);
192 
193  for(std::size_t i = 0; i < report.size(); ++i)
194  {
195  std::size_t pos = report[i];
196 
197  assert(pos < m_features.size());
198 
199  result.push_back(m_features[pos]);
200  }
201 
202  return result;
203 }
204 
206 {
207  std::vector<te::edit::Feature*> candidates = getFeatures(e, srid);
208 
209  if(candidates.empty())
210  return 0;
211 
212  // Generates a geometry from the given extent. It will be used to refine the results
213  std::auto_ptr<te::gm::Geometry> geometryFromEnvelope(te::gm::GetGeomFromEnvelope(&e, srid));
214 
215  // The restriction point. It will be used to refine the results
216  te::gm::Coord2D center = e.getCenter();
217  te::gm::Point point(center.x, center.y, srid);
218 
219  for(std::size_t i = 0; i < candidates.size(); ++i)
220  {
221  te::gm::Geometry* g = candidates[i]->getGeometry();
222 
223  if(g->contains(&point) || g->crosses(geometryFromEnvelope.get()) || geometryFromEnvelope->contains(g)) // Geometry found!
224  return candidates[i];
225  }
226 
227  return 0;
228 }
229 
231 {
232  te::common::FreeContents(m_features);
233  m_features.clear();
234 
235  clearIndex();
236 }
237 
238 void te::edit::Repository::set(const std::size_t& pos, Feature* f)
239 {
240  assert(pos != std::string::npos);
241  assert(f);
242  assert(pos < m_features.size());
243 
244  // Cleaning...
245  delete m_features[pos];
246 
247  // Set the new values
248  m_features[pos] = f;
249 
250  // Indexing...
251  buildIndex();
252 }
253 
255 {
256  m_rtree.clear();
257 }
258 
260 {
261  clearIndex();
262 
263  for(std::size_t i = 0; i < m_features.size(); ++i)
264  buildIndex(i, m_features[i]->getGeometry());
265 }
266 
267 void te::edit::Repository::buildIndex(const std::size_t& pos, te::gm::Geometry* geom)
268 {
269  assert(pos != std::string::npos);
270  assert(geom);
271 
272  te::gm::Envelope mbr(*geom->getMBR());
273 
274  // Indexing...
275  m_rtree.insert(mbr, pos);
276 }
void set(te::da::ObjectId *id, te::gm::Geometry *geom)
Definition: Repository.cpp:97
Feature * getFeature(const te::gm::Envelope &e, int srid) const
Definition: Repository.cpp:205
double y
y-coordinate.
Definition: Coord2D.h:114
TEEDITEXPORT te::da::ObjectId * GenerateId()
Definition: Utils.cpp:361
const std::vector< Feature * > & getAllFeatures() const
Definition: Repository.cpp:161
std::vector< Feature * > getNewFeatures() const
Definition: Repository.cpp:166
double x
x-coordinate.
Definition: Coord2D.h:113
te::da::ObjectId * getId() const
Definition: Feature.cpp:93
#define NEW_FEATURE_ID_SUFFIX
Definition: Config.h:34
Repository(const std::string &source, int srid=TE_UNKNOWN_SRS)
Definition: Repository.cpp:45
te::gm::Geometry * getGeometry() const
Definition: Feature.cpp:98
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual bool contains(const Geometry *const rhs) const
It returns true if this geometry object spatially contains rhs geometry.
Definition: Geometry.cpp:330
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
This class represents a geographic feature.
bool hasIdentifier(te::da::ObjectId *id)
Definition: Repository.cpp:151
virtual bool crosses(const Geometry *const rhs) const
It returns true if the geometry object spatially crosses rhs geometry.
Definition: Geometry.cpp:292
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:51
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void add(te::gm::Geometry *geom)
Definition: Repository.cpp:56
void remove(te::da::ObjectId *id)
Definition: Repository.cpp:120
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
This class represents a repository of geometries and features.
std::string getValueAsString() const
It gets the properties values used to uniquely identify a data set element as string.
Definition: ObjectId.cpp:51
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
std::vector< Feature * > getFeatures(const te::gm::Envelope &e, int srid) const
Definition: Repository.cpp:183
std::size_t getPosition(te::da::ObjectId *id)
Definition: Repository.cpp:140
Utility functions for TerraLib Edit module.
const std::string & getSource() const
Definition: Repository.cpp:156
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
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:104
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38