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 
34 // STL
35 #include <vector>
36 
37 namespace te
38 {
39  namespace gm
40  {
41 // Forward declarations
42  class Envelope;
43  class Geometry;
44  class Point;
45  class Polygon;
46  class LineString;
47  class Line;
48 
49  /*!
50  \struct TopologyValidationError
51 
52  \brief This struct contains informations about GEOS TopologyValidationError
53  */
55  {
56  public:
57 
59  std::string m_message;
60  };
61 
62  /*!
63  \brief It returns the number of measurements or axes needed to describe a position in a coordinate system.
64 
65  It returns:
66  <ul>
67  <li>2 for a coordinate with x, y;</li>
68  <li>3 for a coordinate with x, y and z or x, y and m;</li>
69  <li>4 for a coordinate with x, y, z and m.</li>
70  </ul>
71 
72  \param t The geomeytric type.
73 
74  \return The number of measurements or axes needed to describe a position in a coordinate system.
75  */
77  {
78  if(t & 0x100) // may be z (0x300), m (0x700) or zm (0x800)
79  {
80  if(t & 0x800) // it must be zm
81  return 4;
82 
83  return 3; // it can be z (gType & 0x300) or m (gType & 0x700)
84  }
85 
86  return 2;
87  }
88 
89  /*!
90  \brief It creates a Geometry (a polygon) from the given envelope.
91 
92  \param e The envelope to extract the coordinates. Don't call with a NULL envelope.
93  \param srid The Spatial Reference System ID to be associated to the polygon.
94 
95  \return A polygon (in counter-clock-wise) with rectangle coordinates: [(MINX, MINY), (MAXX, MINY), (MAXX, MAXY), (MINX, MAXY), (MINX, MINY)].
96 
97  \note The caller of this method will take the ownership of the returned geometry.
98  */
99  TEGEOMEXPORT Geometry* GetGeomFromEnvelope(const Envelope* const e, int srid);
100 
101  /*!
102  \brief It returns if two geometries satisfy a given spatial relation.
103 
104  \param g1 The first geometry
105  \param g2 The second geometry
106  \param r A given spatial relation to be tested
107 
108  \return It returns true if the given two geometries satisfy the spatial relation. Otherwise, it returns false.
109 
110  \exception Exception It throws an exception if the spatial relation is not valid or if the test can not be evaluated.
111  */
112  TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry* g1, const Geometry* g2, SpatialRelation relation);
113 
114  /*!
115  \brief Finds the correspondent smallest box that allows a box to be cut in blocks of a given size
116 
117  \param env Reference envelope
118  \param bWidth Block width
119  \param bHeight Block height
120 
121  \return It returns a adjusted envelope
122  */
123  TEGEOMEXPORT Envelope AdjustToCut(const Envelope & env, double bWidth, double bHeight);
124 
125  /* \brief It checks if one object intersects another object. */
126  template<class T1, class T2> bool Intersects(const T1& o1, const T2& o2);
127 
128  /* \brief Specialized function that checks if point intersects envelope. */
129  template<> TEGEOMEXPORT bool Intersects(const te::gm::Point& point, const te::gm::Envelope& e);
130 
131  /*!
132  \brief Make the line interpolation to find a target
133 
134  \param line LineString to make the interpolation
135  \param initial Initial value
136  \param final Final value
137  \param target Target value
138 
139  \return It returns a target Coord2D in the line.
140  */
141  TEGEOMEXPORT Coord2D* locateAlong(const LineString* line, double initial, double final, double target);
142 
143  /*!
144  \brief It will get a GeometryCollection and distribute in a vector.
145 
146  \param g Input GeometryCollection.
147  \param geoms Output Geometry Vector.
148 
149  \note If the geomSource is not a te::gm::GeometryCollectionType it will return vector with the input geometry.
150  */
151  TEGEOMEXPORT void Multi2Single(te::gm::Geometry* g, std::vector<te::gm::Geometry*>& geoms);
152 
153  /*!
154  \brief It will fix geometries with self-intersection.
155 
156  \param g Input Geometry to fix.
157 
158  \return a vector of geometries.
159 
160  \note It will not check if the geometry is invalid.
161 
162  \exception Exception It throws an exception if the geometry is not Polygon or MultiPolygon.
163  */
164  TEGEOMEXPORT std::vector<te::gm::Geometry*> FixSelfIntersection(const te::gm::Geometry* g);
165 
166  /*!
167  \brief It will get the union of the input geometries.
168 
169  \param geom Input Geometry.
170 
171  \return a Geometry containing the union.
172 
173  \note If no input geometries were provided, a POINT EMPTY is returned.
174  */
176 
177  /*!
178  \brief Compute the the closest points of two geometries.
179 
180  \param geomA Input Geometry A.
181  \param geomB Input Geometry B.
182  \param coordA Output coord on Geometry A.
183  \param coordB Output coord on Geometry B.
184 
185  */
187  te::gm::Coord2D& coordA, te::gm::Coord2D& coordB);
188 
190 
191  TEGEOMEXPORT double GetAngle(te::gm::Coord2D coordA, te::gm::Coord2D coordB);
192 
193  TEGEOMEXPORT bool Rotate(te::gm::Coord2D pr, te::gm::LineString* l, double angle, te::gm::LineString* lOut);
194 
195  TEGEOMEXPORT bool AdjustSegment(te::gm::Point* P0, te::gm::Point* P1, double d0, te::gm::Coord2D& P0out, te::gm::Coord2D& P1out);
196 
197  /*!
198  \brief It will get a list of polygons formed by the polygonization.
199 
200  \param g Input Polygon.
201  \param pols Output Polygon Vector.
202  */
203  TEGEOMEXPORT void Polygonizer(te::gm::Geometry* g, std::vector<te::gm::Polygon*>& pols);
204 
205  /*!
206  \brief It check geometry validity using GEOS.
207 
208  \param geom Geometry that will be verified.
209  \param error TopologyValidationError struct.
210 
211  \return True if geometry is valid.
212  */
214 
215  /*!
216  \brief Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon, self intersections /
217  inconsistencies are fixed. Otherwise the geometry is returned.
218 
219  \param geom
220  \return a geometry
221  */
223 
224  /*!
225  \brief Add all line strings from the polygon given to the vector given.
226 
227  \param polygon polygon from which to extract line strings
228  \param pAdd A reference to a vector of geometries.
229  */
230  TEGEOMEXPORT void AddPolygon(te::gm::Polygon* polygon, std::vector<te::gm::Geometry*>& pAdd);
231 
232  /*!
233  \brief Add the linestring given to the vector.
234 
235  \param linestring line string
236  \param pAdd A reference to a vector of geometries.
237  */
238  TEGEOMEXPORT void AddLineString(te::gm::LineString* lineString, std::vector<te::gm::Geometry*>& pAdd);
239 
240  /*!
241  * \brief snapToSelf
242  * \return
243  */
244  TEGEOMEXPORT te::gm::Geometry* SnapToSelf(const te::gm::Geometry* g, const double& snapTolerance, const bool& cleanResult);
245 
246  } // end namespace gm
247 } // end namespace te
248 
249 #endif // __TERRALIB_GEOMETRY_INTERNAL_GEOMUTILS_H
250 
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 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)
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:76
#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.
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)
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
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 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.
TEGEOMEXPORT te::gm::Geometry * SnapToSelf(const te::gm::Geometry *g, const double &snapTolerance, const bool &cleanResult)
snapToSelf
Configuration flags for the Vector Geometry Model of TerraLib.
This struct contains informations about GEOS TopologyValidationError.
Definition: Utils.h:54
An utility struct for representing 2D coordinates.
TEGEOMEXPORT te::gm::Line * GetIntersectionLine(te::gm::Geometry *geom, te::gm::Coord2D coord)