VectorDataset.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file VectorDataset.hpp
34 
35  @author Aline LIBRES <aline.libres@gmail.com>
36  @author Michael RABOTIN <michael.rabotin@supagro.inra.fr>
37  */
38 
39 #ifndef __OPENFLUID_LANDR_VECTORDATASET_HPP__
40 #define __OPENFLUID_LANDR_VECTORDATASET_HPP__
41 
42 
43 #include <string>
44 #include <map>
45 #include <list>
46 
47 #include <ogrsf_frmts.h>
48 
49 #include <openfluid/dllexport.hpp>
51 
52 
53 namespace geos { namespace geom {
54 class Geometry;
55 } }
56 
57 
58 namespace openfluid { namespace landr {
59 
60 
61 /**
62  @brief Interface for managing Vector Data format.
63 */
65 {
66  public:
67 
68  /**
69  @brief A list of pair of OGRFeature and geos::geom::Geometry related to this VectorDataset entities.
70  */
71  typedef std::list<std::pair<OGRFeature*, geos::geom::Geometry*> > FeaturesList_t;
72 
73  private:
74 
75  /**
76  @brief The OGRDataSource related to this VectorDataset.
77  */
78  GDALDataset_COMPAT* mp_DataSource;
79 
80  /**
81  @brief A list of all features of layers of this VectorDataset, indexed by layer index.
82  */
83  std::map<unsigned int, FeaturesList_t> m_Features;
84 
85  /**
86  @brief A map of geos::geom::Geometry representing a collection of all
87  the geometries of the layers of this VectorDataset, indexed by layer index.
88  */
89  std::map<unsigned int, geos::geom::Geometry*> m_Geometries;
90 
91  /**
92  @brief Returns the path of this VectorDataset associated with time.
93  */
94  std::string getTimestampedPath(const std::string& OriginalFileName);
95 
96  /**
97  @brief Returns the path of this VectorDataset.
98  */
99  std::string getInitializedTmpPath();
100 
101  /**
102  @brief Returns true if this VectorDataset exists.
103  @param Path The pathname to this VectorDataset.
104  @return True if already exists, false otherwise.
105  */
106  bool isAlreadyExisting(const std::string& Path);
107 
108  /**
109  @brief Parse the geometry of this VectorDataset.
110  @param LayerIndex The index layer.
111  */
112  void parse(unsigned int LayerIndex);
113 
114  void snapLineNodes(double Threshold,unsigned int LayerIndex=0);
115 
116  void snapPolygonVertices(double Threshold,unsigned int LayerIndex=0);
117 
118 
119  public:
120 
121  /**
122  @brief Creates a new empty OGRDatasource in the openfluid temp directory, with filename suffixes with timestamp.
123  @param FileName The name of the file to create.
124  @throw openfluid::base::FrameworkException if fails.
125  */
126  VectorDataset(const std::string& FileName);
127 
128  /**
129  @brief Creates in the openfluid temp directory a copy of Value OGRDatasource,
130  using Value filename suffixed with timestamp as filename.
131  @param Value The GeoVectorValue to copy
132  @throw openfluid::base::FrameworkException if fails.
133  */
135 
136  /**
137  @brief Copy constructor.
138  @throw openfluid::base::FrameworkException if fails.
139  */
140  VectorDataset(const VectorDataset& Other);
141 
142  /**
143  @brief Delete the OGRDatasource and relative files in openfluid temp directory.
144  */
145  ~VectorDataset();
146 
147  /**
148  @brief Returns the OGRDataSource associated to this VectorDataset.
149  */
150  GDALDataset_COMPAT* source();
151 
152  /**
153  @brief Returns the const OGRDataSource associated to this VectorDataset.
154  */
155  GDALDataset_COMPAT* source() const;
156 
157  /**
158  @brief Write to disk a copy of the OGRDataSource.
159  @param FilePath The path to the directory where writing, will be created if needed.
160  @param FileName The name of the file to write.
161  @param ReplaceIfExists If true and the file FilePath/FileName already exists, overwrite it.
162  */
163  void copyToDisk(const std::string& FilePath,
164  const std::string& FileName,
165  bool ReplaceIfExists);
166 
167  /**
168  @brief Add to DataSource an empty new layer.
169  @param LayerName The name of the layer to create.
170  @param LayerType The type of the layer to create, default wkbUnknown.
171  @param SpatialRef The coordinate system to use for the new layer,
172  or nullptr (default) if no coordinate system is available.
173  @throw openfluid::base::FrameworkException if the creation of layer failed.
174  */
175  void addALayer(std::string LayerName = "",
176  OGRwkbGeometryType LayerType = wkbUnknown,
177  OGRSpatialReference* SpatialRef = nullptr);
178 
179  /**
180  @brief Get a layer of the shape.
181  @param LayerIndex The index of the asked layer, default 0.
182  @return The layer indexed LayerIndex.
183  */
184  OGRLayer* layer(unsigned int LayerIndex = 0);
185 
186  /**
187  @brief Get the Feature definition of a layer.
188  @param LayerIndex The index of the asked layer definition, default 0.
189  @return The OGR Feature definition of the LayerIndex layer.
190  */
191  OGRFeatureDefn* layerDef(unsigned int LayerIndex = 0);
192 
193  /**
194  @brief Add a field to a layer.
195  @param FieldName The name of the field to add.
196  @param FieldType The type of the field to add (default OFTString).
197  @param LayerIndex The index of the layer to add the field, default 0.
198  @throw openfluid::base::FrameworkException if creating field failed.
199  */
200  void addAField(const std::string& FieldName,
201  OGRFieldType FieldType = OFTString,
202  unsigned int LayerIndex = 0);
203 
204  /**
205  @brief Returns true if the VectorDataset is line type.
206  @param LayerIndex The index of the layer to compare the type, default 0.
207  @return True if the type of the layer LayerIndex is wkbLineString, false otherwise.
208  */
209  bool isLineType(unsigned int LayerIndex = 0);
210 
211  /**
212  @brief Returns true if the VectorDataset is polygon type.
213  @param LayerIndex The index of the layer to compare the type, default 0.
214  @return True if the type of the layer LayerIndex is wkbPolygon, false otherwise.
215  */
216  bool isPolygonType(unsigned int LayerIndex = 0);
217 
218  /**
219  @brief Returns true if a field exists in the LayerIndex layer.
220  @param FieldName The name of the field to query.
221  @param LayerIndex The index of the layer to query, default 0.
222  @return True if the field FieldName exists, False otherwise.
223  */
224  bool containsField(const std::string& FieldName,
225  unsigned int LayerIndex = 0);
226 
227  /**
228  @brief Get the index of a field in the LayerIndex layer.
229  @param LayerIndex The index of the layer to query, default 0.
230  @param FieldName The name of the field to query.
231  @return The index of FieldName or -1 if field FieldName doesn't exist.
232  */
233  int getFieldIndex(const std::string& FieldName,
234  unsigned int LayerIndex = 0);
235 
236  /**
237  @brief Returns true if a field is of the type FieldType in the LayerIndex layer.
238  @param FieldName The name of the field to query.
239  @param FieldType The type of the field to query.
240  @param LayerIndex The index of the layer to query, default 0.
241  @return True if the field FieldName is type FieldType.
242  @throw openfluid::base::FrameworkException if the field doesn't exist.
243  */
244  bool isFieldOfType(const std::string& FieldName,
245  OGRFieldType FieldType,
246  unsigned int LayerIndex = 0);
247 
248  /**
249  @brief Returns true if a field has the value Value in the LayerIndex layer.
250  @param FieldName The name of the field to query.
251  @param Value The value to query.
252  @param LayerIndex The index of the layer to query, default 0.
253  @return True if the field has at least a feature containing the value Value, False otherwise.
254  */
255  bool isIntValueSet(const std::string& FieldName,
256  int Value,
257  unsigned int LayerIndex = 0);
258 
259  /**
260  @brief Gets the list of all features of a layer of this GeoVectorValue.
261  @param LayerIndex The index of the layer to query, default 0.
262  @return A list of OGRFeature and geos::geom::Geometry of this VectorDataset.
263  */
264  FeaturesList_t features(unsigned int LayerIndex = 0);
265 
266  /**
267  @brief Gets a geos::geom::Geometry representing a collection of all
268  the geometries of the layer LayerIndex of this GeoVectorValue.
269  @param LayerIndex The index of the layer to query, default 0.
270  @return A geos::geom::Geometry.
271  */
272  geos::geom::Geometry* geometries(unsigned int LayerIndex = 0);
273 
274  /**
275  @brief Returns true if the VectorDataset is point type.
276  @param LayerIndex The index of the layer to compare the type, default 0.
277  @return True if the type of the layer LayerIndex is wkbPoint, false otherwise.
278  */
279  bool isPointType(unsigned int LayerIndex = 0);
280 
281  /**
282  @brief Sets an integer Field with an index list (increment value one by one)
283  @param FieldName The name of the field to query (must exist).
284  @param BeginValue The begin value, default 1.
285  @param LayerIndex The index of the layer to query, default 0.
286  */
287  void setIndexIntField(const std::string& FieldName,
288  int BeginValue = 1,
289  unsigned int LayerIndex = 0);
290 
291  /**
292  @brief Returns the OGREnvelope associated to this VectorDataset.
293  */
294  OGREnvelope envelope();
295 
296  /**
297  @brief Snap the vertices of this VectorDataset.
298  Only for Polygon or Line Type;
299  @param Threshold The snapping threshold value.
300  @param LayerIndex The index of the layer to query, default 0.
301  */
302  void snapVertices(double Threshold,unsigned int LayerIndex=0);
303 
304  /**
305  @brief Check the topology of this VectorDataset.
306  Only for Polygon Type.
307  @param Threshold The maximum distance between polygon to be considered as gap.
308  @param LayerIndex The index of the layer to query, default 0.
309  @return An non-empty string if found errors, otherwise the string is empty.
310  */
311  std::string checkTopology(double Threshold, unsigned int LayerIndex=0);
312 
313  /**
314  @brief Find the overlapping polygons.
315  Only for Polygon Type;
316  @param LayerIndex The index of the layer to query, default 0.
317  @return A list of pair of OGRFeature* for each overlap between two polygons.
318  */
319  std::list<std::pair<OGRFeature*, OGRFeature*> > findOverlap(unsigned int LayerIndex=0);
320 
321  /**
322  @brief Find gap between polygons.
323  Only for Polygon Type;
324  @param Threshold The maximum distance between polygon to be considered as gap.
325  @param LayerIndex The index of the layer to query, default 0.
326  @return A list of pair of OGRFeature* for each overlap between two polygons.
327  */
328  std::list<std::pair<OGRFeature*, OGRFeature*> > findGap(double Threshold,unsigned int LayerIndex=0);
329 
330  /**
331  @brief Clean the overlapping polygons.
332  Only for Polygon Type;
333  @param Threshold The snapping threshold value.
334  @param LayerIndex The index of the layer to query, default 0.
335  */
336  void cleanOverlap(double Threshold, unsigned int LayerIndex=0);
337 
338  /**
339  @brief Identify the duplicate geometry of this VectorDataset.
340  @param LayerIndex The index of the layer to query, default 0.
341  @return A list of OGRFeature* which are duplicate.
342  */
343  std::list<OGRFeature*> hasDuplicateGeometry(unsigned int LayerIndex=0);
344 
345 };
346 
347 
348 } } // namespaces
349 
350 
351 #endif /* __OPENFLUID_LANDR_VECTORDATASET_HPP__ */
Interface for managing Vector Data format.
Definition: VectorDataset.hpp:64
Definition: LandREntity.hpp:53
Definition: ApplicationException.hpp:47
Definition: GeoVectorValue.hpp:55
#define OPENFLUID_API
Definition: dllexport.hpp:87
std::list< std::pair< OGRFeature *, geos::geom::Geometry * > > FeaturesList_t
A list of pair of OGRFeature and geos::geom::Geometry related to this VectorDataset entities...
Definition: VectorDataset.hpp:71
#define GDALDataset_COMPAT
Definition: GDALCompatibility.hpp:72