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 "Config.h"
31 #include "Coord2D.h"
32 #include "Enums.h"
33 #include "Point.h"
34 
35 // STL
36 #include <memory>
37 #include <vector>
38 
39 namespace te
40 {
41  namespace gm
42  {
43 // Forward declarations
44  class Envelope;
45  class Geometry;
46  class Polygon;
47  class LineString;
48  class Line;
49 
50  /*!
51  \struct TopologyValidationError
52 
53  \brief This struct contains informations about GEOS TopologyValidationError
54  */
56  {
57  public:
58 
60  std::string m_message;
61  };
62 
63  /*!
64  \struct DistanceOrderFunctor
65 
66  \brief This struct is used sorts a vector of points by distance.
67  */
69  {
72  {
73  _reference = reference;
74  }
75 
76  bool operator()(const te::gm::Point& p1, const te::gm::Point& p2) const
77  {
78  double distance1 = p1.distance(&_reference);
79  double distance2 = p2.distance(&_reference);
80 
81  if (distance1 < distance2)
82  return true;
83 
84  return false;
85  }
86  };
87 
88  /*!
89  \brief It returns the number of measurements or axes needed to describe a position in a coordinate system.
90 
91  It returns:
92  <ul>
93  <li>2 for a coordinate with x, y;</li>
94  <li>3 for a coordinate with x, y and z or x, y and m;</li>
95  <li>4 for a coordinate with x, y, z and m.</li>
96  </ul>
97 
98  \param t The geomeytric type.
99 
100  \return The number of measurements or axes needed to describe a position in a coordinate system.
101  */
103  {
104  if(t & 0x100) // may be z (0x300), m (0x700) or zm (0x800)
105  {
106  if(t & 0x800) // it must be zm
107  return 4;
108 
109  return 3; // it can be z (gType & 0x300) or m (gType & 0x700)
110  }
111 
112  return 2;
113  }
114 
115  /*!
116  \brief It creates a Geometry (a polygon) from the given envelope.
117 
118  \param e The envelope to extract the coordinates. Don't call with a NULL envelope.
119  \param srid The Spatial Reference System ID to be associated to the polygon.
120 
121  \return A polygon (in counter-clock-wise) with rectangle coordinates: [(MINX, MINY), (MAXX, MINY), (MAXX, MAXY), (MINX, MAXY), (MINX, MINY)].
122 
123  \note The caller of this method will take the ownership of the returned geometry.
124  */
125  TEGEOMEXPORT Geometry* GetGeomFromEnvelope(const Envelope* const e, int srid);
126 
127  /*!
128  \brief It returns the union of a geometry vector.
129 
130  \param items Vector of itens that represents a group.
131 
132  \return Union of the geometry.
133  */
134  TEGEOMEXPORT std::auto_ptr<te::gm::Geometry> GetGeometryUnion(const std::vector<te::gm::Geometry*>& geomVec);
135 
136  /*!
137  \brief It returns if two geometries satisfy a given spatial relation.
138 
139  \param g1 The first geometry
140  \param g2 The second geometry
141  \param r A given spatial relation to be tested
142 
143  \return It returns true if the given two geometries satisfy the spatial relation. Otherwise, it returns false.
144 
145  \exception Exception It throws an exception if the spatial relation is not valid or if the test can not be evaluated.
146  */
147  TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry* g1, const Geometry* g2, SpatialRelation relation);
148 
149  /*!
150  \brief Finds the correspondent smallest box that allows a box to be cut in blocks of a given size
151 
152  \param env Reference envelope
153  \param bWidth Block width
154  \param bHeight Block height
155 
156  \return It returns a adjusted envelope
157  */
158  TEGEOMEXPORT Envelope AdjustToCut(const Envelope & env, double bWidth, double bHeight);
159 
160  /* \brief It checks if one object intersects another object. */
161  template<class T1, class T2> bool Intersects(const T1& o1, const T2& o2);
162 
163  /* \brief Specialized function that checks if point intersects envelope. */
164  template<> TEGEOMEXPORT bool Intersects(const te::gm::Point& point, const te::gm::Envelope& e);
165 
166  /*!
167  \brief Make the line interpolation to find a target
168 
169  \param line LineString to make the interpolation
170  \param initial Initial value
171  \param final Final value
172  \param target Target value
173 
174  \return It returns a target Coord2D in the line.
175  */
176  TEGEOMEXPORT Coord2D* locateAlong(const LineString* line, double initial, double final, double target);
177 
178  /*!
179  \brief It will get a GeometryCollection and distribute in a vector.
180 
181  \param g Input GeometryCollection.
182  \param geoms Output Geometry Vector.
183 
184  \note If the geomSource is not a te::gm::GeometryCollectionType it will return vector with the input geometry.
185  */
186  TEGEOMEXPORT void Multi2Single(te::gm::Geometry* g, std::vector<te::gm::Geometry*>& geoms);
187 
188  /*!
189  \brief It will fix geometries with self-intersection.
190 
191  \param g Input Geometry to fix.
192 
193  \return a vector of geometries.
194 
195  \note It will not check if the geometry is invalid.
196 
197  \exception Exception It throws an exception if the geometry is not Polygon or MultiPolygon.
198  */
199  TEGEOMEXPORT std::vector<te::gm::Geometry*> FixSelfIntersection(const te::gm::Geometry* g);
200 
201  /*!
202  \brief It will get the union of the input geometries.
203 
204  \param geom Input Geometry.
205 
206  \return a Geometry containing the union.
207 
208  \note If no input geometries were provided, a POINT EMPTY is returned.
209  */
211 
212  /*!
213  \brief Compute the the closest points of two geometries.
214 
215  \param geomA Input Geometry A.
216  \param geomB Input Geometry B.
217  \param coordA Output coord on Geometry A.
218  \param coordB Output coord on Geometry B.
219 
220  */
222  te::gm::Coord2D& coordA, te::gm::Coord2D& coordB);
223 
225 
226  TEGEOMEXPORT double GetAngle(te::gm::Coord2D coordA, te::gm::Coord2D coordB);
227 
228  TEGEOMEXPORT bool Rotate(te::gm::Coord2D pr, te::gm::LineString* l, double angle, te::gm::LineString* lOut);
229 
230  TEGEOMEXPORT bool AdjustSegment(te::gm::Point* P0, te::gm::Point* P1, double d0, te::gm::Coord2D& P0out, te::gm::Coord2D& P1out);
231 
232  /*!
233  \brief It will get a list of polygons formed by the polygonization.
234 
235  \param g Input Polygon.
236  \param pols Output Polygon Vector.
237  */
238  TEGEOMEXPORT void Polygonizer(te::gm::Geometry* g, std::vector<te::gm::Polygon*>& pols);
239 
240  /*!
241  \brief It check geometry validity using GEOS.
242 
243  \param geom Geometry that will be verified.
244  \param error TopologyValidationError struct.
245 
246  \return True if geometry is valid.
247  */
249 
250  /*!
251  \brief Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon, self intersections /
252  inconsistencies are fixed. Otherwise the geometry is returned.
253 
254  \param geom
255  \return a geometry
256  */
258 
259  /*!
260  \brief Add all line strings from the polygon given to the vector given.
261 
262  \param polygon polygon from which to extract line strings
263  \param pAdd A reference to a vector of geometries.
264  */
265  TEGEOMEXPORT void AddPolygon(te::gm::Polygon* polygon, std::vector<te::gm::Geometry*>& pAdd);
266 
267  /*!
268  \brief Add the linestring given to the vector.
269 
270  \param linestring line string
271  \param pAdd A reference to a vector of geometries.
272  */
273  TEGEOMEXPORT void AddLineString(te::gm::LineString* lineString, std::vector<te::gm::Geometry*>& pAdd);
274 
275  /*!
276  \brief Calculate the perpendicular distance of a point in a segment.
277 
278  \param first The first segment point.
279  \param last The last segment point.
280  \param pin The point to be calculated.
281  \param pinter A intern point calculated based on pin projected intersection with the segment.
282 
283  \return The perpendicular distance.
284  */
285  TEGEOMEXPORT double PerpendicularDistance(const te::gm::Point& first, const te::gm::Point& last, te::gm::Point& pin, Point& pinter);
286 
287  /*!
288  \brief
289 
290  \param geom_A Geometry used to be inserted new points of intersection.
291  \param geom_B Geometry used to intersects with Geometry A.
292 
293  \return A new geometry based on geometry A with intersection points in geometry B.
294  */
296 
297  /*!
298  \brief
299 
300  \param pol_A Polygon used to be inserted new points of intersection.
301  \param pol_B Polygon used to intersects with Polygon A.
302 
303  \return A new polygon based on polygon A with intersection points in polygon B.
304  */
306 
307  /*!
308  \brief
309 
310  \param ls_A LineString used to be inserted new points of intersection.
311  \param ls_B LineString used to intersects with LineString A.
312 
313  \return A new LineString based on LineString A with intersection points in LineString B.
314  */
316 
317  /*!
318  \brief
319 
320  \param lsReference LineString used as reference.
321  \param candidates A Vector of LineString used to intersects the lsReference.
322 
323  \return A vector of lsReference points including intersection points.
324  */
325  TEGEOMEXPORT std::vector<te::gm::Point> GetIntersectionPoints(const te::gm::LineString& lsReference, const std::vector<te::gm::LineString>& candidates);
326 
327  } // end namespace gm
328 } // end namespace te
329 
330 #endif // __TERRALIB_GEOMETRY_INTERNAL_GEOMUTILS_H
331 
DistanceOrderFunctor(const te::gm::Point &reference)
Definition: Utils.h:71
TEGEOMEXPORT bool CheckValidity(const te::gm::Geometry *geom, te::gm::TopologyValidationError &error)
It check geometry validity using GEOS.
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
int GetCoordDimension(GeomType t)
It returns the number of measurements or axes needed to describe a position in a coordinate system...
Definition: Utils.h:102
This struct is used sorts a vector of points by distance.
Definition: Utils.h:68
#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...
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 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
bool Intersects(const T1 &o1, const T2 &o2)
bool operator()(const te::gm::Point &p1, const te::gm::Point &p2) const
Definition: Utils.h:76
TEGEOMEXPORT te::gm::Geometry * PrepareGeometriesToIntersection(te::gm::Geometry *geom_A, te::gm::Geometry *geom_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:70
TEGEOMEXPORT double GetAngle(te::gm::Coord2D coordA, te::gm::Coord2D coordB)
TEGEOMEXPORT std::auto_ptr< te::gm::Geometry > GetGeometryUnion(const std::vector< te::gm::Geometry * > &geomVec)
It returns the union of a geometry vector.
TEGEOMEXPORT std::vector< te::gm::Point > GetIntersectionPoints(const te::gm::LineString &lsReference, const std::vector< te::gm::LineString > &candidates)
TEGEOMEXPORT bool AdjustSegment(te::gm::Point *P0, te::gm::Point *P1, double d0, te::gm::Coord2D &P0out, te::gm::Coord2D &P1out)
TEGEOMEXPORT void Multi2Single(te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
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:55
An utility struct for representing 2D coordinates.
TEGEOMEXPORT te::gm::Line * GetIntersectionLine(te::gm::Geometry *geom, te::gm::Coord2D coord)