QGIS API Documentation  3.0.2-Girona (307d082)
qgsgeos.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeos.h
3  -------------------------------------------------------------------
4 Date : 22 Sept 2014
5 Copyright : (C) 2014 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
7  ***************************************************************************
8  * *
9  * This program 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 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSGEOS_H
17 #define QGSGEOS_H
18 
19 #define SIP_NO_FILE
20 
21 #include "qgis_core.h"
22 #include "qgsgeometryengine.h"
23 #include "qgsgeometry.h"
24 #include <geos_c.h>
25 
26 class QgsLineString;
27 class QgsPolygon;
28 class QgsGeometry;
30 
36 namespace geos
37 {
38 
43  struct GeosDeleter
44  {
45 
50  void CORE_EXPORT operator()( GEOSGeometry *geom );
51 
56  void CORE_EXPORT operator()( const GEOSPreparedGeometry *geom );
57 
62  void CORE_EXPORT operator()( GEOSBufferParams *params );
63 
68  void CORE_EXPORT operator()( GEOSCoordSequence *sequence );
69  };
70 
74  using unique_ptr = std::unique_ptr< GEOSGeometry, GeosDeleter>;
75 
79  using prepared_unique_ptr = std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter>;
80 
84  using buffer_params_unique_ptr = std::unique_ptr< GEOSBufferParams, GeosDeleter>;
85 
89  using coord_sequence_unique_ptr = std::unique_ptr< GEOSCoordSequence, GeosDeleter>;
90 
91 }
92 
98 class CORE_EXPORT QgsGeos: public QgsGeometryEngine
99 {
100  public:
101 
107  QgsGeos( const QgsAbstractGeometry *geometry, double precision = 0 );
108 
109  void geometryChanged() override;
110  void prepareGeometry() override;
111 
112  QgsAbstractGeometry *intersection( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
113  QgsAbstractGeometry *difference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
114 
119  std::unique_ptr< QgsAbstractGeometry > clip( const QgsRectangle &rectangle, QString *errorMsg = nullptr ) const;
120 
135  std::unique_ptr< QgsAbstractGeometry > subdivide( int maxNodes, QString *errorMsg = nullptr ) const;
136 
137  QgsAbstractGeometry *combine( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
138  QgsAbstractGeometry *combine( const QVector<QgsAbstractGeometry *> &geomList, QString *errorMsg ) const override;
139  QgsAbstractGeometry *combine( const QVector< QgsGeometry > &, QString *errorMsg = nullptr ) const override;
140  QgsAbstractGeometry *symDifference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
141  QgsAbstractGeometry *buffer( double distance, int segments, QString *errorMsg = nullptr ) const override;
142  QgsAbstractGeometry *buffer( double distance, int segments, int endCapStyle, int joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
143  QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override;
144  QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override;
145  QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override;
146  QgsPoint *centroid( QString *errorMsg = nullptr ) const override;
147  QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override;
148  QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override;
149  double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
150 
166  double hausdorffDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
167 
184  double hausdorffDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
185 
186  bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
187  bool touches( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
188  bool crosses( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
189  bool within( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
190  bool overlaps( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
191  bool contains( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
192  bool disjoint( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
193  QString relate( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
194  bool relatePattern( const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg = nullptr ) const override;
195  double area( QString *errorMsg = nullptr ) const override;
196  double length( QString *errorMsg = nullptr ) const override;
197  bool isValid( QString *errorMsg = nullptr ) const override;
198  bool isEqual( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
199  bool isEmpty( QString *errorMsg = nullptr ) const override;
200  bool isSimple( QString *errorMsg = nullptr ) const override;
201 
202  EngineOperationResult splitGeometry( const QgsLineString &splitLine,
203  QVector<QgsGeometry> &newGeometries,
204  bool topological,
205  QgsPointSequence &topologyTestPoints,
206  QString *errorMsg = nullptr ) const override;
207 
208  QgsAbstractGeometry *offsetCurve( double distance, int segments, int joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
209 
223  std::unique_ptr< QgsAbstractGeometry > singleSidedBuffer( double distance, int segments, int side,
224  int joinStyle, double miterLimit,
225  QString *errorMsg = nullptr ) const;
226 
234  std::unique_ptr< QgsAbstractGeometry > reshapeGeometry( const QgsLineString &reshapeWithLine, EngineOperationResult *errorCode, QString *errorMsg = nullptr ) const;
235 
245  QgsGeometry mergeLines( QString *errorMsg = nullptr ) const;
246 
252  QgsGeometry closestPoint( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
253 
259  QgsGeometry shortestLine( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
260 
271  double lineLocatePoint( const QgsPoint &point, QString *errorMsg = nullptr ) const;
272 
282  static QgsGeometry polygonize( const QVector<const QgsAbstractGeometry *> &geometries, QString *errorMsg = nullptr );
283 
299  QgsGeometry voronoiDiagram( const QgsAbstractGeometry *extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
300 
310  QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
311 
316  static std::unique_ptr< QgsAbstractGeometry > fromGeos( const GEOSGeometry *geos );
317  static std::unique_ptr< QgsPolygon > fromGeosPolygon( const GEOSGeometry *geos );
318  static geos::unique_ptr asGeos( const QgsAbstractGeometry *geom, double precision = 0 );
319  static QgsPoint coordSeqPoint( const GEOSCoordSequence *cs, int i, bool hasZ, bool hasM );
320 
321  static GEOSContextHandle_t getGEOSHandler();
322 
323 
324  private:
325  mutable geos::unique_ptr mGeos;
326  geos::prepared_unique_ptr mGeosPrepared;
327  double mPrecision = 0.0;
328 
329  enum Overlay
330  {
331  OverlayIntersection,
332  OverlayDifference,
333  OverlayUnion,
334  OverlaySymDifference
335  };
336 
337  enum Relation
338  {
339  RelationIntersects,
340  RelationTouches,
341  RelationCrosses,
342  RelationWithin,
343  RelationOverlaps,
344  RelationContains,
345  RelationDisjoint
346  };
347 
348  //geos util functions
349  void cacheGeos() const;
350  std::unique_ptr< QgsAbstractGeometry > overlay( const QgsAbstractGeometry *geom, Overlay op, QString *errorMsg = nullptr ) const;
351  bool relation( const QgsAbstractGeometry *geom, Relation r, QString *errorMsg = nullptr ) const;
352  static GEOSCoordSequence *createCoordinateSequence( const QgsCurve *curve, double precision, bool forceClose = false );
353  static std::unique_ptr< QgsLineString > sequenceToLinestring( const GEOSGeometry *geos, bool hasZ, bool hasM );
354  static int numberOfGeometries( GEOSGeometry *g );
355  static geos::unique_ptr nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
356  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry *> &splitResult ) const;
357 
361  static geos::unique_ptr createGeosCollection( int typeId, const QVector<GEOSGeometry *> &geoms );
362 
363  static geos::unique_ptr createGeosPointXY( double x, double y, bool hasZ, double z, bool hasM, double m, int coordDims, double precision );
364  static geos::unique_ptr createGeosPoint( const QgsAbstractGeometry *point, int coordDims, double precision );
365  static geos::unique_ptr createGeosLinestring( const QgsAbstractGeometry *curve, double precision );
366  static geos::unique_ptr createGeosPolygon( const QgsAbstractGeometry *poly, double precision );
367 
368  //utils for geometry split
369  bool topologicalTestPointsSplit( const GEOSGeometry *splitLine, QgsPointSequence &testPoints, QString *errorMsg = nullptr ) const;
370  geos::unique_ptr linePointDifference( GEOSGeometry *GEOSsplitPoint ) const;
371  EngineOperationResult splitLinearGeometry( GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries ) const;
372  EngineOperationResult splitPolygonGeometry( GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries ) const;
373 
374  //utils for reshape
375  static geos::unique_ptr reshapeLine( const GEOSGeometry *line, const GEOSGeometry *reshapeLineGeos, double precision );
376  static geos::unique_ptr reshapePolygon( const GEOSGeometry *polygon, const GEOSGeometry *reshapeLineGeos, double precision );
377  static int lineContainedInLine( const GEOSGeometry *line1, const GEOSGeometry *line2 );
378  static int pointContainedInLine( const GEOSGeometry *point, const GEOSGeometry *line );
379  static int geomDigits( const GEOSGeometry *geom );
380  void subdivideRecursive( const GEOSGeometry *currentPart, int maxNodes, int depth, QgsGeometryCollection *parts, const QgsRectangle &clipRect ) const;
381 };
382 
384 
385 
386 class GEOSException : public std::runtime_error
387 {
388  public:
389  explicit GEOSException( const QString &message )
390  : std::runtime_error( message.toUtf8().constData() )
391  {
392  }
393 };
394 
396 
397 #endif // QGSGEOS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:39
std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter > prepared_unique_ptr
Scoped GEOS prepared geometry pointer.
Definition: qgsgeos.h:79
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
void CORE_EXPORT operator()(GEOSGeometry *geom)
Destroys the GEOS geometry geom, using the static QGIS geos context.
Geometry collection.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:74
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
Abstract base class for all geometries.
Does vector analysis using the geos library and handles import, export, exception handling*...
Definition: qgsgeos.h:98
std::unique_ptr< GEOSBufferParams, GeosDeleter > buffer_params_unique_ptr
Scoped GEOS buffer params pointer.
Definition: qgsgeos.h:84
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
Contains geos related utilities and functions.
Definition: qgsgeos.h:36
QVector< QgsPoint > QgsPointSequence
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:41
Contains geometry relation and modification algorithms.
std::unique_ptr< GEOSCoordSequence, GeosDeleter > coord_sequence_unique_ptr
Scoped GEOS coordinate sequence pointer.
Definition: qgsgeos.h:89
EngineOperationResult
Success or failure of a geometry operation.
Polygon geometry type.
Definition: qgspolygon.h:31
Destroys the GEOS geometry geom, using the static QGIS geos context.
Definition: qgsgeos.h:43