Utils.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/geometry/Utils.h
22 
23  \brief Utility functions for the Geometry Module.
24 */
25 
26 #ifndef __TERRALIB_GEOMETRY_INTERNAL_GEOMUTILS_H
27 #define __TERRALIB_GEOMETRY_INTERNAL_GEOMUTILS_H
28 
29 // TerraLib
30 #include "../sam/rtree.h"
31 #include "Config.h"
32 #include "Coord2D.h"
33 #include "Enums.h"
34 #include "Point.h"
35 
36 // STL
37 #include <memory>
38 #include <vector>
39 
40 namespace te
41 {
42  namespace gm
43  {
44 // Forward declarations
45  class Envelope;
46  class Geometry;
47  class Polygon;
48  class LineString;
49  class Line;
50 
51  /*!
52  \struct TopologyValidationError
53 
54  \brief This struct contains informations about GEOS TopologyValidationError
55  */
57  {
58  public:
59 
61  std::string m_message;
62  };
63 
64  /*!
65  \struct DistanceOrderFunctor
66 
67  \brief This struct is used sorts a vector of points by distance.
68  */
70  {
73  {
74  _reference = reference;
75  }
76 
77  bool operator()(const te::gm::Point& p1, const te::gm::Point& p2) const
78  {
79  double distance1 = p1.distance(&_reference);
80  double distance2 = p2.distance(&_reference);
81 
82  if (distance1 < distance2)
83  return true;
84 
85  return false;
86  }
87  };
88 
89  /*!
90  \brief It returns the number of measurements or axes needed to describe a position in a coordinate system.
91 
92  It returns:
93  <ul>
94  <li>2 for a coordinate with x, y;</li>
95  <li>3 for a coordinate with x, y and z or x, y and m;</li>
96  <li>4 for a coordinate with x, y, z and m.</li>
97  </ul>
98 
99  \param t The geomeytric type.
100 
101  \return The number of measurements or axes needed to describe a position in a coordinate system.
102  */
104  {
105  if(t & 0x100) // may be z (0x300), m (0x700) or zm (0x800)
106  {
107  if(t & 0x800) // it must be zm
108  return 4;
109 
110  return 3; // it can be z (gType & 0x300) or m (gType & 0x700)
111  }
112 
113  return 2;
114  }
115 
116  /*!
117  \brief It creates a Geometry (a polygon) from the given envelope.
118 
119  \param e The envelope to extract the coordinates. Don't call with a NULL envelope.
120  \param srid The Spatial Reference System ID to be associated to the polygon.
121 
122  \return A polygon (in counter-clock-wise) with rectangle coordinates: [(MINX, MINY), (MAXX, MINY), (MAXX, MAXY), (MINX, MAXY), (MINX, MINY)].
123 
124  \note The caller of this method will take the ownership of the returned geometry.
125  */
126  TEGEOMEXPORT Geometry* GetGeomFromEnvelope(const Envelope* const e, int srid);
127 
128  /*!
129  \brief It returns the union of a geometry vector.
130 
131  \param items Vector of itens that represents a group.
132 
133  \return Union of the geometry.
134  */
135  TEGEOMEXPORT std::unique_ptr<te::gm::Geometry> GetGeometryUnion(const std::vector<te::gm::Geometry*>& geomVec);
136 
137  /*!
138  \brief It returns if two geometries satisfy a given spatial relation.
139 
140  \param g1 The first geometry
141  \param g2 The second geometry
142  \param r A given spatial relation to be tested
143 
144  \return It returns true if the given two geometries satisfy the spatial relation. Otherwise, it returns false.
145 
146  \exception Exception It throws an exception if the spatial relation is not valid or if the test can not be evaluated.
147  */
148  TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry* g1, const Geometry* g2, SpatialRelation relation);
149 
150  /*!
151  \brief Finds the correspondent smallest box that allows a box to be cut in blocks of a given size
152 
153  \param env Reference envelope
154  \param bWidth Block width
155  \param bHeight Block height
156 
157  \return It returns a adjusted envelope
158  */
159  TEGEOMEXPORT Envelope AdjustToCut(const Envelope & env, double bWidth, double bHeight);
160 
161  /* \brief It checks if one object intersects another object. */
162  template<class T1, class T2> bool Intersects(const T1& o1, const T2& o2);
163 
164  /* \brief Specialized function that checks if point intersects envelope. */
165  template<> TEGEOMEXPORT bool Intersects(const te::gm::Point& point, const te::gm::Envelope& e);
166 
167  /*!
168  \brief Make the line interpolation to find a target
169 
170  \param line LineString to make the interpolation
171  \param initial Initial value
172  \param final Final value
173  \param target Target value
174 
175  \return It returns a target Coord2D in the line.
176  */
177  TEGEOMEXPORT Coord2D* locateAlong(const LineString* line, double initial, double final, double target);
178 
179  /*!
180  \brief It will get a GeometryCollection and distribute in a vector.
181 
182  \param g Input GeometryCollection.
183  \param geoms Output Geometry Vector.
184 
185  \note If the geomSource is not a te::gm::GeometryCollectionType it will return vector with the input geometry.
186  */
187  TEGEOMEXPORT void Multi2Single(const te::gm::Geometry* g, std::vector<te::gm::Geometry*>& geoms);
188 
189  /*!
190  \brief It will fix geometries with self-intersection.
191 
192  \param g Input Geometry to fix.
193 
194  \return a vector of geometries.
195 
196  \note It will not check if the geometry is invalid.
197 
198  \exception Exception It throws an exception if the geometry is not Polygon or MultiPolygon.
199  */
200  TEGEOMEXPORT std::vector<te::gm::Geometry*> FixSelfIntersection(const te::gm::Geometry* g);
201 
202  /*!
203  \brief Provides an efficient method of unioning a collection of Polygonal geometries.
204  This algorithm is faster and likely more robust than the simple iterated approach of repeatedly unioning each polygon to a result geometry.
205 
206  \param polygonVector A vector of polygons.
207 
208  \return a Geometry containing the union.
209  */
210  TEGEOMEXPORT te::gm::Geometry* CascadedPolygonUnion(const std::vector<te::gm::Polygon*>& polygonVector);
211 
212  /*!
213  \brief It will get the union of the input geometries.
214 
215  \param geom Input Geometry.
216 
217  \return a Geometry containing the union.
218 
219  \note If no input geometries were provided, an EMPTY POINTER is returned.
220  */
222 
223  /*!
224  \brief Compute the the closest points of two geometries.
225 
226  \param geomA Input Geometry A.
227  \param geomB Input Geometry B.
228  \param coordA Output coord on Geometry A.
229  \param coordB Output coord on Geometry B.
230 
231  */
233  te::gm::Coord2D& coordA, te::gm::Coord2D& coordB);
234 
236 
237  TEGEOMEXPORT double GetAngle(te::gm::Coord2D coordA, te::gm::Coord2D coordB);
238 
239  TEGEOMEXPORT bool Rotate(te::gm::Coord2D pr, te::gm::LineString* l, double angle, te::gm::LineString* lOut);
240 
241  TEGEOMEXPORT bool AdjustSegment(te::gm::Point* P0, te::gm::Point* P1, double d0, te::gm::Coord2D& P0out, te::gm::Coord2D& P1out);
242 
243  /*!
244  \brief It will get a list of polygons formed by the polygonization.
245 
246  \param g Input Polygon.
247  \param pols Output Polygon Vector.
248  */
249  TEGEOMEXPORT void Polygonizer(te::gm::Geometry* g, std::vector<te::gm::Polygon*>& pols);
250 
251  /*!
252  \brief It check geometry validity using GEOS.
253 
254  \param geom Geometry that will be verified.
255  \param error TopologyValidationError struct.
256 
257  \return True if geometry is valid.
258  */
260 
261  /*!
262  \brief Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon, self intersections /
263  inconsistencies are fixed. Otherwise the geometry is returned.
264 
265  \param geom
266  \return a geometry
267  */
269 
270  /*!
271  \brief Add all line strings from the polygon given to the vector given.
272 
273  \param polygon polygon from which to extract line strings
274  \param pAdd A reference to a vector of geometries.
275  */
276  TEGEOMEXPORT void AddPolygon(te::gm::Polygon* polygon, std::vector<te::gm::Geometry*>& pAdd);
277 
278  /*!
279  \brief Add the linestring given to the vector.
280 
281  \param linestring line string
282  \param pAdd A reference to a vector of geometries.
283  */
284  TEGEOMEXPORT void AddLineString(te::gm::LineString* lineString, std::vector<te::gm::Geometry*>& pAdd);
285 
286  /*!
287  \brief Calculate the perpendicular distance of a point in a segment.
288 
289  \param first The first segment point.
290  \param last The last segment point.
291  \param pin The point to be calculated.
292  \param pinter A intern point calculated based on pin projected intersection with the segment.
293 
294  \return The perpendicular distance.
295  */
296  TEGEOMEXPORT double PerpendicularDistance(const te::gm::Point& first, const te::gm::Point& last, te::gm::Point& pin, Point& pinter);
297 
298  /*!
299  \brief Prepares the geometry A with intersection points in geometry B.
300 
301  \param geom_A Geometry used to be inserted new points of intersection.
302  \param geom_B Geometry used to intersects with Geometry A.
303 
304  \return A new geometry based on geometry A with intersection points in geometry B.
305  */
307 
308  /*!
309  \brief Prepares the Polygon A with intersection points in Polygon B.
310 
311  \param mpol_A MultiPolygon used to be inserted new points of intersection.
312  \param mpol_B MultiPolygon used to intersects with Polygon A.
313 
314  \return A new geometry based on MultiPolygon A with intersection points in MultiPolygon B.
315  */
317 
318  /*!
319  \brief Prepares the Polygon A with intersection points in Polygon B.
320 
321  \param pol_A Polygon used to be inserted new points of intersection.
322  \param pol_B Polygon used to intersects with Polygon A.
323 
324  \return A new geometry based on polygon A with intersection points in polygon B.
325  */
327 
328  /*!
329  \brief Prepares the LineString A with intersection points in LineString B.
330 
331  \param ls_A LineString used to be inserted new points of intersection.
332  \param ls_B LineString used to intersects with LineString A.
333 
334  \return A new LineString based on LineString A with intersection points in LineString B.
335  */
337 
338  /*!
339  \brief Snaps a LinearRing based on a set of LineString.
340 
341  \param rtree The R-tree contains the candidates envelopes as reference to snap.
342  \param referenceSegments A vector of LineString that is used as reference to snap.
343  \param linearRingToSnap A LinearRing that will be snapped.
344 
345  \return A new snapped LinearRing.
346  */
348  const std::vector<te::gm::LineString>& referenceSegments,
349  const te::gm::LinearRing& linearRingToSnap);
350 
351  /*!
352  \brief Add intersection points in a LinearRing based on a Vector of LineString as reference.
353 
354  \param rtree The R-tree contains the candidates envelopes as reference to snap.
355  \param candidateSegments A vector of LineString that is used as reference to snap.
356  \param lr_Reference A LinearRing that will be added new points of intersection.
357  \param usePerpendicularDistance True if the new points are added by perpendicular distance algorithm.
358 
359  \return
360  */
362  const std::vector<te::gm::LineString>& candidateSegments,
363  const te::gm::LinearRing& lr_Reference,
364  const bool& usePerpendicularDistance = false);
365 
366  /*!
367  \brief Gets a vector of LineString reference points including intersection points using GEOS operators.
368 
369  \param lsReference LineString used as reference.
370  \param candidates A Vector of LineString used to intersects the lsReference.
371 
372  \return A vector of LineString reference points including intersection points using GEOS operators.
373  */
374  TEGEOMEXPORT std::vector<te::gm::Point> GetIntersectionPointsByOperators(const te::gm::LineString& lsReference, const std::vector<te::gm::LineString>& candidates);
375 
376  /*!
377  \brief Gets a vector of LineString reference points including intersection points using perpendicular distance.
378 
379  \param lsReference LineString used as reference.
380  \param candidates A Vector of LineString used to intersects the lsReference.
381 
382  \return A vector of LineString reference points including intersection points using perpendicular distance.
383  */
384  TEGEOMEXPORT std::vector<te::gm::Point> GetIntersectionPointsByPerpendicularDistance(const te::gm::LineString& lsReference, const std::vector<te::gm::LineString>& candidates);
385 
386  /*!
387  \brief Creates a LineString between the given startCoord and endCoord with the given number of intermediate coordinates.
388 
389  \param startCoord The first coordinate of the line
390  \param endCoord The last coordinate of the line
391  \param srid The srid of the line
392  \param numberOfIntermediateCoords The number of intermediate coordinates of the line
393 
394  \return A LineString between startCoord and endCoord with the given number of intermediate coordinates
395  */
396  TEGEOMEXPORT te::gm::LineString CreateLine(const te::gm::Coord2D& startCoord, const te::gm::Coord2D& endCoord, const int& srid, const int& numberOfIntermediateCoords);
397 
398  /*!
399  \brief Creates a Polygon from the given envelope and with the given number of intermediate coordinates in each of its four borders lines.
400 
401  \param box The envelope from which the polygon will be created
402  \param srid The srid of the polygon
403  \param numberOfIntermediateCoords The number of intermediate coordinates of each border line
404 
405  \return A LineString between startCoord and endCoord with the given number of intermediate coordinates in each border line
406  */
407  TEGEOMEXPORT te::gm::Polygon CreatePolygon(const te::gm::Envelope& box, const int& srid, const int& numberOfIntermediateCoords);
408 
409  } // end namespace gm
410 } // end namespace te
411 
412 #endif // __TERRALIB_GEOMETRY_INTERNAL_GEOMUTILS_H
413 
DistanceOrderFunctor(const te::gm::Point &reference)
Definition: Utils.h:72
TEGEOMEXPORT bool CheckValidity(const te::gm::Geometry *geom, te::gm::TopologyValidationError &error)
It check geometry validity using GEOS.
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
A Line is LineString with 2 points.
Definition: Line.h:50
TEGEOMEXPORT double PerpendicularDistance(const te::gm::Point &first, const te::gm::Point &last, te::gm::Point &pin, Point &pinter)
Calculate the perpendicular distance of a point in a segment.
TEGEOMEXPORT void AddPolygon(te::gm::Polygon *polygon, std::vector< te::gm::Geometry * > &pAdd)
Add all line strings from the polygon given to the vector given.
TEGEOMEXPORT void ClosestPoints(te::gm::Geometry *geomA, te::gm::Geometry *geomB, te::gm::Coord2D &coordA, te::gm::Coord2D &coordB)
Compute the the closest points of two geometries.
TEGEOMEXPORT bool Rotate(te::gm::Coord2D pr, te::gm::LineString *l, double angle, te::gm::LineString *lOut)
A point with x and y coordinate values.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:127
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
TEGEOMEXPORT te::gm::Polygon CreatePolygon(const te::gm::Envelope &box, const int &srid, const int &numberOfIntermediateCoords)
Creates a Polygon from the given envelope and with the given number of intermediate coordinates in ea...
int GetCoordDimension(GeomType t)
It returns the number of measurements or axes needed to describe a position in a coordinate system...
Definition: Utils.h:103
This struct is used sorts a vector of points by distance.
Definition: Utils.h:69
TEGEOMEXPORT te::gm::Geometry * CascadedPolygonUnion(const std::vector< te::gm::Polygon * > &polygonVector)
Provides an efficient method of unioning a collection of Polygonal geometries. This algorithm is fast...
#define TEGEOMEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:76
TEGEOMEXPORT Envelope AdjustToCut(const Envelope &env, double bWidth, double bHeight)
Finds the correspondent smallest box that allows a box to be cut in blocks of a given size...
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
TEGEOMEXPORT te::gm::LinearRing * SnapLineToPoints(const te::sam::rtree::Index< std::size_t, 8 > &rtree, const std::vector< te::gm::LineString > &referenceSegments, const te::gm::LinearRing &linearRingToSnap)
Snaps a LinearRing based on a set of LineString.
Enumerations related to Geometry module.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
URI C++ Library.
TEGEOMEXPORT te::gm::Geometry * UnaryUnion(te::gm::Geometry *geom)
It will get the union of the input geometries.
virtual double distance(const Geometry *const rhs) const
It returns the shortest distance between any two points in the two geometry objects.
TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry *g1, const Geometry *g2, SpatialRelation relation)
It returns if two geometries satisfy a given spatial relation.
TEGEOMEXPORT std::vector< te::gm::Geometry * > FixSelfIntersection(const te::gm::Geometry *g)
It will fix geometries with self-intersection.
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
TEGEOMEXPORT te::gm::Geometry * Validate(te::gm::Geometry *geom)
Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon...
TEGEOMEXPORT void AddLineString(te::gm::LineString *lineString, std::vector< te::gm::Geometry * > &pAdd)
Add the linestring given to the vector.
TEGEOMEXPORT void Polygonizer(te::gm::Geometry *g, std::vector< te::gm::Polygon * > &pols)
It will get a list of polygons formed by the polygonization.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:74
TEGEOMEXPORT std::vector< te::gm::Point > GetIntersectionPointsByPerpendicularDistance(const te::gm::LineString &lsReference, const std::vector< te::gm::LineString > &candidates)
Gets a vector of LineString reference points including intersection points using perpendicular distan...
TEGEOMEXPORT std::vector< te::gm::Point > GetIntersectionPointsByOperators(const te::gm::LineString &lsReference, const std::vector< te::gm::LineString > &candidates)
Gets a vector of LineString reference points including intersection points using GEOS operators...
bool Intersects(const T1 &o1, const T2 &o2)
bool operator()(const te::gm::Point &p1, const te::gm::Point &p2) const
Definition: Utils.h:77
TEGEOMEXPORT te::gm::Geometry * PrepareGeometriesToIntersection(te::gm::Geometry *geom_A, te::gm::Geometry *geom_B)
Prepares the geometry A with intersection points in geometry B.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
te::gm::Point _reference
Definition: Utils.h:71
TEGEOMEXPORT std::unique_ptr< te::gm::Geometry > GetGeometryUnion(const std::vector< te::gm::Geometry * > &geomVec)
It returns the union of a geometry vector.
TEGEOMEXPORT double GetAngle(te::gm::Coord2D coordA, te::gm::Coord2D coordB)
TEGEOMEXPORT bool AdjustSegment(te::gm::Point *P0, te::gm::Point *P1, double d0, te::gm::Coord2D &P0out, te::gm::Coord2D &P1out)
TEGEOMEXPORT te::gm::LinearRing * AddIntersectionPoints(const te::sam::rtree::Index< std::size_t, 8 > &rtree, const std::vector< te::gm::LineString > &candidateSegments, const te::gm::LinearRing &lr_Reference, const bool &usePerpendicularDistance=false)
Add intersection points in a LinearRing based on a Vector of LineString as reference.
TEGEOMEXPORT te::gm::LineString CreateLine(const te::gm::Coord2D &startCoord, const te::gm::Coord2D &endCoord, const int &srid, const int &numberOfIntermediateCoords)
Creates a LineString between the given startCoord and endCoord with the given number of intermediate ...
TEGEOMEXPORT Coord2D * locateAlong(const LineString *line, double initial, double final, double target)
Make the line interpolation to find a target.
Configuration flags for the Vector Geometry Model of TerraLib.
This struct contains informations about GEOS TopologyValidationError.
Definition: Utils.h:56
An utility struct for representing 2D coordinates.
TEGEOMEXPORT te::gm::Line * GetIntersectionLine(te::gm::Geometry *geom, te::gm::Coord2D coord)