QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsgeos.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeos.h
3 -------------------------------------------------------------------
4Date : 22 Sept 2014
5Copyright : (C) 2014 by Marco Hugentobler
6email : 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
26class QgsLineString;
27class QgsPolygon;
28class QgsGeometry;
30
35namespace geos
36{
37
43 {
44
49 void CORE_EXPORT operator()( GEOSGeometry *geom ) const;
50
55 void CORE_EXPORT operator()( const GEOSPreparedGeometry *geom ) const;
56
61 void CORE_EXPORT operator()( GEOSBufferParams *params ) const;
62
67 void CORE_EXPORT operator()( GEOSCoordSequence *sequence ) const;
68 };
69
73 using unique_ptr = std::unique_ptr< GEOSGeometry, GeosDeleter>;
74
78 using prepared_unique_ptr = std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter>;
79
83 using buffer_params_unique_ptr = std::unique_ptr< GEOSBufferParams, GeosDeleter>;
84
88 using coord_sequence_unique_ptr = std::unique_ptr< GEOSCoordSequence, GeosDeleter>;
89
90}
91
97class CORE_EXPORT QgsGeos: public QgsGeometryEngine
98{
99 public:
100
107 QgsGeos( const QgsAbstractGeometry *geometry, double precision = 0, bool allowInvalidSubGeom = true );
108
113 static QgsGeometry geometryFromGeos( GEOSGeometry *geos );
114
118 static QgsGeometry geometryFromGeos( const geos::unique_ptr &geos );
119
128 std::unique_ptr< QgsAbstractGeometry > makeValid( Qgis::MakeValidMethod method = Qgis::MakeValidMethod::Linework, bool keepCollapsed = false, QString *errorMsg = nullptr ) const;
129
136 static Qgis::GeometryOperationResult addPart( QgsGeometry &geometry, GEOSGeometry *newPart );
137
138 void geometryChanged() override;
139 void prepareGeometry() override;
140
141 QgsAbstractGeometry *intersection( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
142 QgsAbstractGeometry *difference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
143
148 std::unique_ptr< QgsAbstractGeometry > clip( const QgsRectangle &rectangle, QString *errorMsg = nullptr ) const;
149
167 std::unique_ptr< QgsAbstractGeometry > subdivide( int maxNodes, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
168
169 QgsAbstractGeometry *combine( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
170 QgsAbstractGeometry *combine( const QVector<QgsAbstractGeometry *> &geomList, QString *errorMsg, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
171 QgsAbstractGeometry *combine( const QVector< QgsGeometry > &, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
172 QgsAbstractGeometry *symDifference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
173 QgsAbstractGeometry *buffer( double distance, int segments, QString *errorMsg = nullptr ) const override;
174 QgsAbstractGeometry *buffer( double distance, int segments, Qgis::EndCapStyle endCapStyle, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
175 QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override;
176 QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override;
177 QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override;
178 QgsPoint *centroid( QString *errorMsg = nullptr ) const override;
179 QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override;
180 QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override;
181 double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
182 bool distanceWithin( const QgsAbstractGeometry *geom, double maxdistance, QString *errorMsg = nullptr ) const override;
183
191 bool contains( double x, double y, QString *errorMsg = nullptr ) const;
192
200 double distance( double x, double y, QString *errorMsg = nullptr ) const;
201
216 double hausdorffDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
217
233 double hausdorffDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
234
247 double frechetDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
248
271 double frechetDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
272
273 bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
274 bool touches( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
275 bool crosses( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
276 bool within( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
277 bool overlaps( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
278 bool contains( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
279 bool disjoint( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
280 QString relate( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
281 bool relatePattern( const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg = nullptr ) const override;
282 double area( QString *errorMsg = nullptr ) const override;
283 double length( QString *errorMsg = nullptr ) const override;
284 bool isValid( QString *errorMsg = nullptr, bool allowSelfTouchingHoles = false, QgsGeometry *errorLoc = nullptr ) const override;
285 bool isEqual( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
286 bool isEmpty( QString *errorMsg = nullptr ) const override;
287 bool isSimple( QString *errorMsg = nullptr ) const override;
288
290 QVector<QgsGeometry> &newGeometries,
291 bool topological,
292 QgsPointSequence &topologyTestPoints,
293 QString *errorMsg = nullptr, bool skipIntersectionCheck = false ) const override;
294
295 QgsAbstractGeometry *offsetCurve( double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
296
309 std::unique_ptr< QgsAbstractGeometry > singleSidedBuffer( double distance, int segments, Qgis::BufferSide side,
310 Qgis::JoinStyle joinStyle, double miterLimit,
311 QString *errorMsg = nullptr ) const;
312
337 std::unique_ptr< QgsAbstractGeometry > maximumInscribedCircle( double tolerance, QString *errorMsg = nullptr ) const;
338
362 std::unique_ptr< QgsAbstractGeometry > largestEmptyCircle( double tolerance, const QgsAbstractGeometry *boundary = nullptr, QString *errorMsg = nullptr ) const;
363
378 std::unique_ptr< QgsAbstractGeometry > minimumWidth( QString *errorMsg = nullptr ) const;
379
401 double minimumClearance( QString *errorMsg = nullptr ) const;
402
414 std::unique_ptr< QgsAbstractGeometry > minimumClearanceLine( QString *errorMsg = nullptr ) const;
415
426 std::unique_ptr< QgsAbstractGeometry > node( QString *errorMsg = nullptr ) const;
427
442 std::unique_ptr< QgsAbstractGeometry > sharedPaths( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
443
451 std::unique_ptr< QgsAbstractGeometry > reshapeGeometry( const QgsLineString &reshapeWithLine, EngineOperationResult *errorCode, QString *errorMsg = nullptr ) const;
452
461 QgsGeometry mergeLines( QString *errorMsg = nullptr ) const;
462
467 QgsGeometry closestPoint( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
468
473 QgsGeometry shortestLine( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
474
480 QgsGeometry shortestLine( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
481
492 double lineLocatePoint( const QgsPoint &point, QString *errorMsg = nullptr ) const;
493
506 double lineLocatePoint( double x, double y, QString *errorMsg = nullptr ) const;
507
516 static QgsGeometry polygonize( const QVector<const QgsAbstractGeometry *> &geometries, QString *errorMsg = nullptr );
517
532 QgsGeometry voronoiDiagram( const QgsAbstractGeometry *extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
533
543 QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
544
557 std::unique_ptr< QgsAbstractGeometry > constrainedDelaunayTriangulation( QString *errorMsg = nullptr ) const;
558
580 QgsAbstractGeometry *concaveHull( double targetPercent, bool allowHoles = false, QString *errorMsg = nullptr ) const;
581
601 Qgis::CoverageValidityResult validateCoverage( double gapWidth, std::unique_ptr< QgsAbstractGeometry > *invalidEdges, QString *errorMsg = nullptr ) const;
602
624 std::unique_ptr< QgsAbstractGeometry > simplifyCoverageVW( double tolerance, bool preserveBoundary, QString *errorMsg = nullptr ) const;
625
637 std::unique_ptr< QgsAbstractGeometry > unionCoverage( QString *errorMsg = nullptr ) const;
638
643 static std::unique_ptr< QgsAbstractGeometry > fromGeos( const GEOSGeometry *geos );
644 static std::unique_ptr< QgsPolygon > fromGeosPolygon( const GEOSGeometry *geos );
645
646
652 static geos::unique_ptr asGeos( const QgsGeometry &geometry, double precision = 0 );
653
660 static geos::unique_ptr asGeos( const QgsAbstractGeometry *geometry, double precision = 0, bool allowInvalidSubGeom = true );
661 static QgsPoint coordSeqPoint( const GEOSCoordSequence *cs, int i, bool hasZ, bool hasM );
662
663 static GEOSContextHandle_t getGEOSHandler();
664
665
666 private:
667 mutable geos::unique_ptr mGeos;
668 geos::prepared_unique_ptr mGeosPrepared;
669 double mPrecision = 0.0;
670
671 enum Overlay
672 {
673 OverlayIntersection,
674 OverlayDifference,
675 OverlayUnion,
676 OverlaySymDifference
677 };
678
679 enum Relation
680 {
681 RelationIntersects,
682 RelationTouches,
683 RelationCrosses,
684 RelationWithin,
685 RelationOverlaps,
686 RelationContains,
687 RelationDisjoint
688 };
689
690 //geos util functions
691 void cacheGeos( bool allowInvalidSubGeom ) const;
692
700 std::unique_ptr< QgsAbstractGeometry > overlay( const QgsAbstractGeometry *geom, Overlay op, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
701 bool relation( const QgsAbstractGeometry *geom, Relation r, QString *errorMsg = nullptr ) const;
702 static GEOSCoordSequence *createCoordinateSequence( const QgsCurve *curve, double precision, bool forceClose = false );
703 static std::unique_ptr< QgsLineString > sequenceToLinestring( const GEOSGeometry *geos, bool hasZ, bool hasM );
704 static int numberOfGeometries( GEOSGeometry *g );
705 static geos::unique_ptr nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
706 int mergeGeometriesMultiTypeSplit( std::vector<geos::unique_ptr> &splitResult ) const;
707
711 static geos::unique_ptr createGeosCollection( int typeId, std::vector<geos::unique_ptr> &geoms );
712
713 static geos::unique_ptr createGeosPointXY( double x, double y, bool hasZ, double z, bool hasM, double m, int coordDims, double precision );
714 static geos::unique_ptr createGeosPoint( const QgsAbstractGeometry *point, int coordDims, double precision );
715 static geos::unique_ptr createGeosLinestring( const QgsAbstractGeometry *curve, double precision );
716 static geos::unique_ptr createGeosPolygon( const QgsAbstractGeometry *poly, double precision );
717
718 //utils for geometry split
719 bool topologicalTestPointsSplit( const GEOSGeometry *splitLine, QgsPointSequence &testPoints, QString *errorMsg = nullptr ) const;
720 geos::unique_ptr linePointDifference( GEOSGeometry *GEOSsplitPoint ) const;
721 EngineOperationResult splitLinearGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
722 EngineOperationResult splitPolygonGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
723
724 //utils for reshape
725 static geos::unique_ptr reshapeLine( const GEOSGeometry *line, const GEOSGeometry *reshapeLineGeos, double precision );
726 static geos::unique_ptr reshapePolygon( const GEOSGeometry *polygon, const GEOSGeometry *reshapeLineGeos, double precision );
727 static int lineContainedInLine( const GEOSGeometry *line1, const GEOSGeometry *line2 );
728 static int pointContainedInLine( const GEOSGeometry *point, const GEOSGeometry *line );
729 static int geomDigits( const GEOSGeometry *geom );
730 void subdivideRecursive( const GEOSGeometry *currentPart, int maxNodes, int depth, QgsGeometryCollection *parts, const QgsRectangle &clipRect, double gridSize = -1 ) const;
731};
732
734
735
736class GEOSException : public std::runtime_error
737{
738 public:
739 explicit GEOSException( const QString &message )
740 : std::runtime_error( message.toUtf8().constData() )
741 {
742 }
743};
744
746
747#endif // QGSGEOS_H
BufferSide
Side of line to buffer.
Definition: qgis.h:1669
GeometryOperationResult
Success or failure of a geometry operation.
Definition: qgis.h:1615
JoinStyle
Join styles for buffers.
Definition: qgis.h:1694
EndCapStyle
End cap styles for buffers.
Definition: qgis.h:1681
CoverageValidityResult
Coverage validity results.
Definition: qgis.h:1707
MakeValidMethod
Algorithms to use when repairing invalid geometries.
Definition: qgis.h:1720
@ Linework
Combines all rings into a set of noded lines and then extracts valid polygons from that linework.
Abstract base class for all geometries.
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
Geometry collection.
A geometry engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
virtual bool isEqual(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if this is equal to geom.
virtual double area(QString *errorMsg=nullptr) const =0
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.
virtual void geometryChanged()=0
Should be called whenever the geometry associated with the engine has been modified and the engine mu...
virtual bool intersects(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom intersects this.
virtual QgsAbstractGeometry * symDifference(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr, const QgsGeometryParameters &parameters=QgsGeometryParameters()) const =0
Calculate the symmetric difference of this and geom.
virtual QgsAbstractGeometry * buffer(double distance, int segments, QString *errorMsg=nullptr) const =0
virtual QgsAbstractGeometry * offsetCurve(double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg=nullptr) const =0
Offsets a curve.
virtual double distance(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Calculates the distance between this and geom.
virtual QgsAbstractGeometry * intersection(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr, const QgsGeometryParameters &parameters=QgsGeometryParameters()) const =0
Calculate the intersection of this and geom.
virtual bool distanceWithin(const QgsAbstractGeometry *geom, double maxdistance, QString *errorMsg=nullptr) const =0
Checks if geom is within maxdistance distance from this geometry.
virtual QgsAbstractGeometry * interpolate(double distance, QString *errorMsg=nullptr) const =0
virtual QgsAbstractGeometry * simplify(double tolerance, QString *errorMsg=nullptr) const =0
virtual bool isValid(QString *errorMsg=nullptr, bool allowSelfTouchingHoles=false, QgsGeometry *errorLoc=nullptr) const =0
Returns true if the geometry is valid.
virtual QgsPoint * pointOnSurface(QString *errorMsg=nullptr) const =0
Calculate a point that is guaranteed to be on the surface of this.
virtual bool touches(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom touches this.
virtual QgsAbstractGeometry * envelope(QString *errorMsg=nullptr) const =0
EngineOperationResult
Success or failure of a geometry operation.
virtual bool isEmpty(QString *errorMsg) const =0
virtual bool crosses(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom crosses this.
virtual QgsAbstractGeometry * difference(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr, const QgsGeometryParameters &parameters=QgsGeometryParameters()) const =0
Calculate the difference of this and geom.
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...
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...
virtual bool isSimple(QString *errorMsg=nullptr) const =0
Determines whether the geometry is simple (according to OGC definition).
virtual QgsAbstractGeometry * convexHull(QString *errorMsg=nullptr) const =0
Calculate the convex hull of this.
virtual QgsAbstractGeometry * combine(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr, const QgsGeometryParameters &parameters=QgsGeometryParameters()) const =0
Calculate the combination of this and geom.
virtual bool overlaps(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom overlaps this.
virtual bool within(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom is within this.
virtual void prepareGeometry()=0
Prepares the geometry, so that subsequent calls to spatial relation methods are much faster.
virtual bool contains(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom contains this.
virtual double length(QString *errorMsg=nullptr) const =0
virtual QgsPoint * centroid(QString *errorMsg=nullptr) const =0
Calculates the centroid of this.
virtual bool disjoint(const QgsAbstractGeometry *geom, QString *errorMsg=nullptr) const =0
Checks if geom is disjoint from this.
Encapsulates parameters under which a geometry operation is performed.
Definition: qgsgeometry.h:109
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
Does vector analysis using the geos library and handles import, export, exception handling*.
Definition: qgsgeos.h:98
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:45
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Polygon geometry type.
Definition: qgspolygon.h:33
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains geos related utilities and functions.
Definition: qgsgeos.h:36
std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter > prepared_unique_ptr
Scoped GEOS prepared geometry pointer.
Definition: qgsgeos.h:78
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:73
std::unique_ptr< GEOSCoordSequence, GeosDeleter > coord_sequence_unique_ptr
Scoped GEOS coordinate sequence pointer.
Definition: qgsgeos.h:88
std::unique_ptr< GEOSBufferParams, GeosDeleter > buffer_params_unique_ptr
Scoped GEOS buffer params pointer.
Definition: qgsgeos.h:83
QVector< QgsPoint > QgsPointSequence
int precision
Destroys the GEOS geometry geom, using the static QGIS geos context.
Definition: qgsgeos.h:43
void CORE_EXPORT operator()(GEOSGeometry *geom) const
Destroys the GEOS geometry geom, using the static QGIS geos context.
void CORE_EXPORT operator()(GEOSBufferParams *params) const
Destroys the GEOS buffer params params, using the static QGIS geos context.
void CORE_EXPORT operator()(GEOSCoordSequence *sequence) const
Destroys the GEOS coordinate sequence sequence, using the static QGIS geos context.
void CORE_EXPORT operator()(const GEOSPreparedGeometry *geom) const
Destroys the GEOS prepared geometry geom, using the static QGIS geos context.
struct GEOSGeom_t GEOSGeometry
Definition: util.h:41