Validation.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/Validation.h
22 
23  \brief A list of Validation functions for the Geometry Module.
24 */
25 
26 #ifndef __TERRALIB_GEOMETRY_INTERNAL_VALIDATION_H
27 #define __TERRALIB_GEOMETRY_INTERNAL_VALIDATION_H
28 
29 // TerraLib
30 #include "Config.h"
31 
32 #include "Coord2D.h"
33 
34 // STL
35 #include <map>
36 #include <string>
37 #include <vector>
38 
39 namespace te
40 {
41  namespace gm
42  {
43 // Forward declarations
44  class Geometry;
45  class GeometryPtr;
46  class LineString;
47 
48  /*!
49  \struct TopologyValidationError
50 
51  \brief This struct contains informations about GEOS TopologyValidationError
52  */
54  {
55  public:
56 
58  std::string m_message;
59  };
60 
62  {
63  public:
64 
65  /*!
66  \brief It check geometry validity using GEOS.
67 
68  \param geom Geometry that will be verified.
69  \param error TopologyValidationError struct.
70 
71  \return True if geometry is valid.
72  */
73  static bool CheckValidity(const Geometry* geom, te::gm::TopologyValidationError& error);
74 
75  /*!
76  \brief Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon, self intersections /
77  inconsistencies are fixed. Otherwise the geometry is returned.
78 
79  \param geom
80  \return a geometry
81 
82  \note: https://stackoverflow.com/questions/31473553/is-there-a-way-to-convert-a-self-intersecting-polygon-to-a-multipolygon-in-jts
83  */
85 
86  /*! Fixes the given geometry. It ensures that all the coordinates will be kept. This algorithm is very conservative.
87  * It handles very well polygons containing self intersections like the 8 (eight number) or infinite symbol
88  * Important: This may result is some area addition in the cases in which the border of the polygon contains several self touching segments (in both directions) linkng the same area.
89  * This situation is usually resulted froma difference operation executed without first snapping both geometries
90  */
92 
93  /*! Fixes the given geometry. It ensures that all the coordinates will be kept. This algorithm is very conservative.
94  * It handles very well polygons containing self intersections like the 8 (eight number) or infinite symbol
95  * Important: This may result is some area addition in the cases in which the border of the polygon contains several self touching segments (in both directions) linkng the same area.
96  * This situation is usually resulted froma difference operation executed without first snapping both geometries
97  */
98  static te::gm::GeometryPtr MakeValid(const te::gm::Geometry* geometry, std::string& errorMessage);
99 
100  /*! Snaps the vertices in the component geom::LineStrings of the source geometry to the vertices of itself with a given snap tolerance and optionally cleaning the result. By GEOS
101  * Important: This function may discard the entire geometry and return null if it is not able to execute the operation.
102  * It does not handle well polygons containing self intersections like the 8 (eight number) or infinite symbol
103  * In this case, choose MakeValid
104  */
105  static te::gm::GeometryPtr SnapToSelf(const te::gm::Geometry* geometry, double tolerance, bool cleanResult);
106 
107  /*! Snaps the vertices in the component geom::LineStrings of the source geometry to the vertices of itself with a given snap tolerance and optionally cleaning the result. By GEOS
108  * Important: This function may discard the entire geometry and return null if it is not able to execute the operation.
109  * It does not handle well polygons containing self intersections like the 8 (eight number) or infinite symbol
110  * In this case, choose MakeValid
111  */
113 
114  /*! Tries to fix a geometry in a better way by combining two algorithms: SnapToSelf and MakeValid
115  * It fixes the geometry using the following steps: First try to apply the snapToSelf algorithm.
116  * If the result is empty or if the area of the resulting geometry has changed significantly, it assumes that the snapToSelf was not able to fix it appropriately.
117  * In this case, it then applies the makeValid algorithm (that is more conservative) and returns the result.
118  */
120 
121  private:
122 
123  /*!
124  \brief Apply several fixes to a geometry to ensure that it will be made valid. New implementations must call MakeValidGeos
125 
126  \description It applies the following fixes:
127  1 - Removes all the repeated consecutive coordidates
128  2 - Add perpendicular points that are within the tolerance to each analysed segment
129  3 - We snap the coordinates to ensure that any "similar" coordinate is now equal
130  4 - To avoid the discard of rings in geos polygonize function, we must ensure that we removed all the collapsed segments
131  5 - Finally we applies a polygonizer to recreate the geometry and ensure that it is now consistent
132 
133  \param geom Geometry that will be verified.
134 
135  \return True if geometry is valid.
136  */
137  static te::gm::GeometryPtr MakeValidTerralib(const te::gm::Geometry* geometry, std::string& errorMessage);
138 
139  //!< Executes the makeValid from Geos
140  static te::gm::GeometryPtr MakeValidGeos(const te::gm::Geometry* geometry, std::string& errorMessage);
141 
142 
143 
144  };
145 
146  } // end namespace gm
147 } // end namespace te
148 
149 #endif // __TERRALIB_GEOMETRY_INTERNAL_VALIDATION_H
An utility struct for representing 2D coordinates.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:78
static bool CheckValidity(const Geometry *geom, te::gm::TopologyValidationError &error)
It check geometry validity using GEOS.
static te::gm::GeometryPtr MakeValidTerralib(const te::gm::Geometry *geometry, std::string &errorMessage)
Apply several fixes to a geometry to ensure that it will be made valid. New implementations must call...
static te::gm::GeometryPtr MakeValid(const te::gm::Geometry *geometry)
static te::gm::GeometryPtr MakeValid(const te::gm::Geometry *geometry, std::string &errorMessage)
static te::gm::GeometryPtr SnapToSelf(const te::gm::Geometry *geometry)
static te::gm::GeometryPtr MakeValidGeos(const te::gm::Geometry *geometry, std::string &errorMessage)
static 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,...
static te::gm::GeometryPtr SnapToSelf(const te::gm::Geometry *geometry, double tolerance, bool cleanResult)
static te::gm::GeometryPtr MakeValidWithSnap(const te::gm::Geometry *geometry)
TerraLib.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:41
This struct contains informations about GEOS TopologyValidationError.
Definition: Validation.h:54
#define TEGEOMEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:76
Proxy configuration file for TerraView (see terraview_config.h).