QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #if defined(GEOS_VERSION_MAJOR) && (GEOS_VERSION_MAJOR<3)
27 #define GEOSGeometry struct GEOSGeom_t
28 #define GEOSCoordSequence struct GEOSCoordSeq_t
29 #endif
30 
31 class QgsLineString;
32 class QgsPolygon;
33 class QgsGeometry;
35 
41 namespace geos
42 {
43 
48  struct GeosDeleter
49  {
50 
55  void CORE_EXPORT operator()( GEOSGeometry *geom ) const;
56 
61  void CORE_EXPORT operator()( const GEOSPreparedGeometry *geom ) const;
62 
67  void CORE_EXPORT operator()( GEOSBufferParams *params ) const;
68 
73  void CORE_EXPORT operator()( GEOSCoordSequence *sequence ) const;
74  };
75 
79  using unique_ptr = std::unique_ptr< GEOSGeometry, GeosDeleter>;
80 
84  using prepared_unique_ptr = std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter>;
85 
89  using buffer_params_unique_ptr = std::unique_ptr< GEOSBufferParams, GeosDeleter>;
90 
94  using coord_sequence_unique_ptr = std::unique_ptr< GEOSCoordSequence, GeosDeleter>;
95 
96 }
97 
103 class CORE_EXPORT QgsGeos: public QgsGeometryEngine
104 {
105  public:
106 
112  QgsGeos( const QgsAbstractGeometry *geometry, double precision = 0 );
113 
118  static QgsGeometry geometryFromGeos( GEOSGeometry *geos );
119 
123  static QgsGeometry geometryFromGeos( const geos::unique_ptr &geos );
124 
125 #if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=8 )
126 
131  std::unique_ptr< QgsAbstractGeometry > makeValid( QString *errorMsg = nullptr ) const;
132 #endif
133 
140  static Qgis::GeometryOperationResult addPart( QgsGeometry &geometry, GEOSGeometry *newPart );
141 
142  void geometryChanged() override;
143  void prepareGeometry() override;
144 
145  QgsAbstractGeometry *intersection( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
146  QgsAbstractGeometry *difference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
147 
152  std::unique_ptr< QgsAbstractGeometry > clip( const QgsRectangle &rectangle, QString *errorMsg = nullptr ) const;
153 
168  std::unique_ptr< QgsAbstractGeometry > subdivide( int maxNodes, QString *errorMsg = nullptr ) const;
169 
170  QgsAbstractGeometry *combine( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
171  QgsAbstractGeometry *combine( const QVector<QgsAbstractGeometry *> &geomList, QString *errorMsg ) const override;
172  QgsAbstractGeometry *combine( const QVector< QgsGeometry > &, QString *errorMsg = nullptr ) const override;
173  QgsAbstractGeometry *symDifference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
174  QgsAbstractGeometry *buffer( double distance, int segments, QString *errorMsg = nullptr ) const override;
175  QgsAbstractGeometry *buffer( double distance, int segments, Qgis::EndCapStyle endCapStyle, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
176  QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override;
177  QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override;
178  QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override;
179  QgsPoint *centroid( QString *errorMsg = nullptr ) const override;
180  QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override;
181  QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override;
182  double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
183  bool distanceWithin( const QgsAbstractGeometry *geom, double maxdistance, QString *errorMsg = nullptr ) const override;
184 
192  bool contains( double x, double y, QString *errorMsg = nullptr ) const;
193 
201  double distance( double x, double y, QString *errorMsg = nullptr ) const;
202 
218  double hausdorffDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
219 
236  double hausdorffDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
237 
250  double frechetDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
251 
274  double frechetDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
275 
276  bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
277  bool touches( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
278  bool crosses( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
279  bool within( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
280  bool overlaps( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
281  bool contains( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
282  bool disjoint( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
283  QString relate( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
284  bool relatePattern( const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg = nullptr ) const override;
285  double area( QString *errorMsg = nullptr ) const override;
286  double length( QString *errorMsg = nullptr ) const override;
287  bool isValid( QString *errorMsg = nullptr, bool allowSelfTouchingHoles = false, QgsGeometry *errorLoc = nullptr ) const override;
288  bool isEqual( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
289  bool isEmpty( QString *errorMsg = nullptr ) const override;
290  bool isSimple( QString *errorMsg = nullptr ) const override;
291 
293  QVector<QgsGeometry> &newGeometries,
294  bool topological,
295  QgsPointSequence &topologyTestPoints,
296  QString *errorMsg = nullptr, bool skipIntersectionCheck = false ) const override;
297 
298  QgsAbstractGeometry *offsetCurve( double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
299 
313  std::unique_ptr< QgsAbstractGeometry > singleSidedBuffer( double distance, int segments, Qgis::BufferSide side,
314  Qgis::JoinStyle joinStyle, double miterLimit,
315  QString *errorMsg = nullptr ) const;
316 
341  std::unique_ptr< QgsAbstractGeometry > maximumInscribedCircle( double tolerance, QString *errorMsg = nullptr ) const;
342 
366  std::unique_ptr< QgsAbstractGeometry > largestEmptyCircle( double tolerance, const QgsAbstractGeometry *boundary = nullptr, QString *errorMsg = nullptr ) const;
367 
382  std::unique_ptr< QgsAbstractGeometry > minimumWidth( QString *errorMsg = nullptr ) const;
383 
405  double minimumClearance( QString *errorMsg = nullptr ) const;
406 
418  std::unique_ptr< QgsAbstractGeometry > minimumClearanceLine( QString *errorMsg = nullptr ) const;
419 
430  std::unique_ptr< QgsAbstractGeometry > node( QString *errorMsg = nullptr ) const;
431 
446  std::unique_ptr< QgsAbstractGeometry > sharedPaths( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
447 
455  std::unique_ptr< QgsAbstractGeometry > reshapeGeometry( const QgsLineString &reshapeWithLine, EngineOperationResult *errorCode, QString *errorMsg = nullptr ) const;
456 
466  QgsGeometry mergeLines( QString *errorMsg = nullptr ) const;
467 
473  QgsGeometry closestPoint( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
474 
480  QgsGeometry shortestLine( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
481 
487  QgsGeometry shortestLine( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
488 
499  double lineLocatePoint( const QgsPoint &point, QString *errorMsg = nullptr ) const;
500 
513  double lineLocatePoint( double x, double y, QString *errorMsg = nullptr ) const;
514 
524  static QgsGeometry polygonize( const QVector<const QgsAbstractGeometry *> &geometries, QString *errorMsg = nullptr );
525 
541  QgsGeometry voronoiDiagram( const QgsAbstractGeometry *extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
542 
552  QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
553 
558  static std::unique_ptr< QgsAbstractGeometry > fromGeos( const GEOSGeometry *geos );
559  static std::unique_ptr< QgsPolygon > fromGeosPolygon( const GEOSGeometry *geos );
560 
561 
567  static geos::unique_ptr asGeos( const QgsGeometry &geometry, double precision = 0 );
568 
574  static geos::unique_ptr asGeos( const QgsAbstractGeometry *geometry, double precision = 0 );
575  static QgsPoint coordSeqPoint( const GEOSCoordSequence *cs, int i, bool hasZ, bool hasM );
576 
577  static GEOSContextHandle_t getGEOSHandler();
578 
579 
580  private:
581  mutable geos::unique_ptr mGeos;
582  geos::prepared_unique_ptr mGeosPrepared;
583  double mPrecision = 0.0;
584 
585  enum Overlay
586  {
587  OverlayIntersection,
588  OverlayDifference,
589  OverlayUnion,
590  OverlaySymDifference
591  };
592 
593  enum Relation
594  {
595  RelationIntersects,
596  RelationTouches,
597  RelationCrosses,
598  RelationWithin,
599  RelationOverlaps,
600  RelationContains,
601  RelationDisjoint
602  };
603 
604  //geos util functions
605  void cacheGeos() const;
606  std::unique_ptr< QgsAbstractGeometry > overlay( const QgsAbstractGeometry *geom, Overlay op, QString *errorMsg = nullptr ) const;
607  bool relation( const QgsAbstractGeometry *geom, Relation r, QString *errorMsg = nullptr ) const;
608  static GEOSCoordSequence *createCoordinateSequence( const QgsCurve *curve, double precision, bool forceClose = false );
609  static std::unique_ptr< QgsLineString > sequenceToLinestring( const GEOSGeometry *geos, bool hasZ, bool hasM );
610  static int numberOfGeometries( GEOSGeometry *g );
611  static geos::unique_ptr nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
612  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry *> &splitResult ) const;
613 
617  static geos::unique_ptr createGeosCollection( int typeId, const QVector<GEOSGeometry *> &geoms );
618 
619  static geos::unique_ptr createGeosPointXY( double x, double y, bool hasZ, double z, bool hasM, double m, int coordDims, double precision );
620  static geos::unique_ptr createGeosPoint( const QgsAbstractGeometry *point, int coordDims, double precision );
621  static geos::unique_ptr createGeosLinestring( const QgsAbstractGeometry *curve, double precision );
622  static geos::unique_ptr createGeosPolygon( const QgsAbstractGeometry *poly, double precision );
623 
624  //utils for geometry split
625  bool topologicalTestPointsSplit( const GEOSGeometry *splitLine, QgsPointSequence &testPoints, QString *errorMsg = nullptr ) const;
626  geos::unique_ptr linePointDifference( GEOSGeometry *GEOSsplitPoint ) const;
627  EngineOperationResult splitLinearGeometry( GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
628  EngineOperationResult splitPolygonGeometry( GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
629 
630  //utils for reshape
631  static geos::unique_ptr reshapeLine( const GEOSGeometry *line, const GEOSGeometry *reshapeLineGeos, double precision );
632  static geos::unique_ptr reshapePolygon( const GEOSGeometry *polygon, const GEOSGeometry *reshapeLineGeos, double precision );
633  static int lineContainedInLine( const GEOSGeometry *line1, const GEOSGeometry *line2 );
634  static int pointContainedInLine( const GEOSGeometry *point, const GEOSGeometry *line );
635  static int geomDigits( const GEOSGeometry *geom );
636  void subdivideRecursive( const GEOSGeometry *currentPart, int maxNodes, int depth, QgsGeometryCollection *parts, const QgsRectangle &clipRect ) const;
637 };
638 
640 
641 
642 class GEOSException : public std::runtime_error
643 {
644  public:
645  explicit GEOSException( const QString &message )
646  : std::runtime_error( message.toUtf8().constData() )
647  {
648  }
649 };
650 
652 
653 #endif // QGSGEOS_H
QgsCurve
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
QgsGeometryEngine::isValid
virtual bool isValid(QString *errorMsg=nullptr, bool allowSelfTouchingHoles=false, QgsGeometry *errorLoc=nullptr) const =0
Returns true if the geometry is valid.
QgsGeometryEngine::touches
virtual bool touches(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom touches this.
geos::buffer_params_unique_ptr
std::unique_ptr< GEOSBufferParams, GeosDeleter > buffer_params_unique_ptr
Scoped GEOS buffer params pointer.
Definition: qgsgeos.h:89
QgsPolygon
Polygon geometry type.
Definition: qgspolygon.h:33
QgsGeometryEngine::interpolate
virtual QgsAbstractGeometry * interpolate(double distance, QString *errorMsg=nullptr) const =0
QgsGeometryEngine::distanceWithin
virtual bool distanceWithin(const QgsAbstractGeometry *geom, double maxdistance, QString *errorMsg=nullptr) const =0
Checks if geom is within maxdistance distance from this geometry.
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
geos::GeosDeleter
Destroys the GEOS geometry geom, using the static QGIS geos context.
Definition: qgsgeos.h:48
Qgis::EndCapStyle
EndCapStyle
End cap styles for buffers.
Definition: qgis.h:966
QgsGeometryEngine::relatePattern
virtual bool relatePattern(const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg=nullptr) const =0
Tests whether two geometries are related by a specified Dimensional Extended 9 Intersection Model (DE...
geos
Contains geos related utilities and functions.
Definition: qgsgeos.h:41
QgsGeometryEngine::convexHull
virtual QgsAbstractGeometry * convexHull(QString *errorMsg=nullptr) const =0
Calculate the convex hull of this.
QgsGeometryEngine::length
virtual double length(QString *errorMsg=nullptr) const =0
QgsGeometryEngine::intersection
virtual QgsAbstractGeometry * intersection(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Calculate the intersection of this and geom.
QgsGeometryEngine::relate
virtual QString relate(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Returns the Dimensional Extended 9 Intersection Model (DE-9IM) representation of the relationship bet...
QgsLineString
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
QgsGeometryEngine::buffer
virtual QgsAbstractGeometry * buffer(double distance, int segments, QString *errorMsg=nullptr) const =0
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsGeometryEngine::crosses
virtual bool crosses(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom crosses this.
QgsGeometryEngine::disjoint
virtual bool disjoint(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom is disjoint from this.
QgsGeometryEngine::pointOnSurface
virtual QgsPoint * pointOnSurface(QString *errorMsg=nullptr) const =0
Calculate a point that is guaranteed to be on the surface of this.
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsGeometryCollection
Geometry collection.
Definition: qgsgeometrycollection.h:36
QgsGeometryEngine::isEmpty
virtual bool isEmpty(QString *errorMsg) const =0
qgsgeometryengine.h
QgsGeometryEngine::geometryChanged
virtual void geometryChanged()=0
Should be called whenever the geometry associated with the engine has been modified and the engine mu...
Qgis::BufferSide
BufferSide
Side of line to buffer.
Definition: qgis.h:954
QgsGeos
Does vector analysis using the geos library and handles import, export, exception handling*.
Definition: qgsgeos.h:103
Qgis::GeometryOperationResult
GeometryOperationResult
Success or failure of a geometry operation.
Definition: qgis.h:901
QgsGeometryEngine::intersects
virtual bool intersects(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom intersects this.
Qgis::JoinStyle
JoinStyle
Join styles for buffers.
Definition: qgis.h:979
QgsGeometryEngine::envelope
virtual QgsAbstractGeometry * envelope(QString *errorMsg=nullptr) const =0
QgsGeometryEngine::combine
virtual QgsAbstractGeometry * combine(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Calculate the combination of this and geom.
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:79
QgsGeometryEngine::symDifference
virtual QgsAbstractGeometry * symDifference(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Calculate the symmetric difference of this and geom.
QgsGeometryEngine::isSimple
virtual bool isSimple(QString *errorMsg=nullptr) const =0
Determines whether the geometry is simple (according to OGC definition).
QgsGeometryEngine::distance
virtual double distance(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Calculates the distance between this and geom.
QgsGeometryEngine::isEqual
virtual bool isEqual(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if this is equal to geom.
QgsGeometryEngine::simplify
virtual QgsAbstractGeometry * simplify(double tolerance, QString *errorMsg=nullptr) const =0
QgsGeometryEngine::area
virtual double area(QString *errorMsg=nullptr) const =0
QgsGeometryEngine::prepareGeometry
virtual void prepareGeometry()=0
Prepares the geometry, so that subsequent calls to spatial relation methods are much faster.
qgsgeometry.h
QgsPointSequence
QVector< QgsPoint > QgsPointSequence
Definition: qgsabstractgeometry.h:52
QgsGeometryEngine::within
virtual bool within(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom is within this.
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
geos::GeosDeleter::operator()
void CORE_EXPORT operator()(GEOSGeometry *geom) const
Destroys the GEOS geometry geom, using the static QGIS geos context.
QgsGeometryEngine::splitGeometry
virtual QgsGeometryEngine::EngineOperationResult splitGeometry(const QgsLineString &splitLine, QVector< QgsGeometry > &newGeometries, bool topological, QgsPointSequence &topologyTestPoints, QString *errorMsg=nullptr, bool skipIntersectionCheck=false) const
Splits this geometry according to a given line.
Definition: qgsgeometryengine.h:306
geos::coord_sequence_unique_ptr
std::unique_ptr< GEOSCoordSequence, GeosDeleter > coord_sequence_unique_ptr
Scoped GEOS coordinate sequence pointer.
Definition: qgsgeos.h:94
geos::prepared_unique_ptr
std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter > prepared_unique_ptr
Scoped GEOS prepared geometry pointer.
Definition: qgsgeos.h:84
QgsGeometryEngine::EngineOperationResult
EngineOperationResult
Success or failure of a geometry operation.
Definition: qgsgeometryengine.h:76
QgsGeometryEngine::difference
virtual QgsAbstractGeometry * difference(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Calculate the difference of this and geom.
QgsGeometryEngine::offsetCurve
virtual QgsAbstractGeometry * offsetCurve(double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg=nullptr) const =0
Offsets a curve.
QgsGeometryEngine::overlaps
virtual bool overlaps(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom overlaps this.
QgsGeometryEngine::centroid
virtual QgsPoint * centroid(QString *errorMsg=nullptr) const =0
Calculates the centroid of this.
QgsGeometryEngine
A geometry engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
Definition: qgsgeometryengine.h:68
QgsGeometryEngine::contains
virtual bool contains(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom contains this.