QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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#include "qgis_core.h"
20#include "qgis_sip.h"
21#include "qgsgeometryengine.h"
22#include "qgsgeometry.h"
23#include "qgsconfig.h"
24#include <geos_c.h>
25
26class QgsLineString;
27class QgsPolygon;
28class QgsGeometry;
30
31#if !defined(USE_THREAD_LOCAL) || defined(Q_OS_WIN)
32#include <QThreadStorage>
33#endif
34
35#ifndef SIP_RUN
43class CORE_EXPORT QgsGeosContext
44{
45 public:
46
49
53 static GEOSContextHandle_t get();
54
55 private:
56 GEOSContextHandle_t mContext = nullptr;
57
63#if defined(USE_THREAD_LOCAL) && !defined(Q_OS_WIN)
64 static thread_local QgsGeosContext sGeosContext;
65#else
66 static QThreadStorage< QgsGeosContext * > sGeosContext;
67#endif
68};
69
74namespace geos
75{
76
81 struct GeosDeleter
82 {
83
88 void CORE_EXPORT operator()( GEOSGeometry *geom ) const;
89
94 void CORE_EXPORT operator()( const GEOSPreparedGeometry *geom ) const;
95
100 void CORE_EXPORT operator()( GEOSBufferParams *params ) const;
101
106 void CORE_EXPORT operator()( GEOSCoordSequence *sequence ) const;
107 };
108
112 using unique_ptr = std::unique_ptr< GEOSGeometry, GeosDeleter>;
113
117 using prepared_unique_ptr = std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter>;
118
122 using buffer_params_unique_ptr = std::unique_ptr< GEOSBufferParams, GeosDeleter>;
123
127 using coord_sequence_unique_ptr = std::unique_ptr< GEOSCoordSequence, GeosDeleter>;
128
129}
130#endif
131
138class CORE_EXPORT QgsGeos: public QgsGeometryEngine
139{
140 public:
141
150
157 SIP_SKIP static QgsGeometry geometryFromGeos( GEOSGeometry *geos );
158
164 SIP_SKIP static QgsGeometry geometryFromGeos( const geos::unique_ptr &geos );
165
174 std::unique_ptr< QgsAbstractGeometry > makeValid( Qgis::MakeValidMethod method = Qgis::MakeValidMethod::Linework, bool keepCollapsed = false, QString *errorMsg = nullptr ) const SIP_THROW( QgsNotSupportedException );
175
184 SIP_SKIP static Qgis::GeometryOperationResult addPart( QgsGeometry &geometry, GEOSGeometry *newPart );
185
186 void geometryChanged() override;
187 void prepareGeometry() override;
188
189 QgsAbstractGeometry *intersection( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
190 QgsAbstractGeometry *difference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
191
201 std::unique_ptr< QgsAbstractGeometry > clip( const QgsRectangle &rectangle, QString *errorMsg SIP_OUT = nullptr ) const;
202
221 std::unique_ptr< QgsAbstractGeometry > subdivide( int maxNodes, QString *errorMsg SIP_OUT = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
222
223 QgsAbstractGeometry *combine( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
224 QgsAbstractGeometry *combine( const QVector<QgsAbstractGeometry *> &geomList, QString *errorMsg, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
225 QgsAbstractGeometry *combine( const QVector< QgsGeometry > &, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
226 QgsAbstractGeometry *symDifference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
227 QgsAbstractGeometry *buffer( double distance, int segments, QString *errorMsg = nullptr ) const override;
228 QgsAbstractGeometry *buffer( double distance, int segments, Qgis::EndCapStyle endCapStyle, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
229 QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override;
230 QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override;
231 QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override;
232 QgsPoint *centroid( QString *errorMsg = nullptr ) const override;
233 QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override;
234 QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override;
235 double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
236 bool distanceWithin( const QgsAbstractGeometry *geom, double maxdistance, QString *errorMsg = nullptr ) const override;
237
251 bool contains( double x, double y, QString *errorMsg SIP_OUT = nullptr ) const;
252
266 double distance( double x, double y, QString *errorMsg SIP_OUT = nullptr ) const;
267
287 double hausdorffDistance( const QgsAbstractGeometry *geometry, QString *errorMsg SIP_OUT = nullptr ) const;
288
310 double hausdorffDistanceDensify( const QgsAbstractGeometry *geometry, double densifyFraction, QString *errorMsg SIP_OUT = nullptr ) const;
311
329 double frechetDistance( const QgsAbstractGeometry *geometry, QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
330
359 double frechetDistanceDensify( const QgsAbstractGeometry *geometry, double densifyFraction, QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
360
361 bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
362 bool touches( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
363 bool crosses( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
364 bool within( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
365 bool overlaps( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
366 bool contains( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
367 bool disjoint( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
368 QString relate( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
369 bool relatePattern( const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg = nullptr ) const override;
370 double area( QString *errorMsg = nullptr ) const override;
371 double length( QString *errorMsg = nullptr ) const override;
372 bool isValid( QString *errorMsg = nullptr, bool allowSelfTouchingHoles = false, QgsGeometry *errorLoc = nullptr ) const override;
373 bool isEqual( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
374 bool isEmpty( QString *errorMsg = nullptr ) const override;
375 bool isSimple( QString *errorMsg = nullptr ) const override;
376
377 EngineOperationResult splitGeometry( const QgsLineString &splitLine,
378 QVector<QgsGeometry> &newGeometries,
379 bool topological,
380 QgsPointSequence &topologyTestPoints,
381 QString *errorMsg = nullptr, bool skipIntersectionCheck = false ) const override;
382
383 QgsAbstractGeometry *offsetCurve( double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
384
396 std::unique_ptr< QgsAbstractGeometry > singleSidedBuffer( double distance, int segments, Qgis::BufferSide side,
397 Qgis::JoinStyle joinStyle, double miterLimit,
398 QString *errorMsg SIP_OUT = nullptr ) const;
399
429 std::unique_ptr< QgsAbstractGeometry > maximumInscribedCircle( double tolerance, QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
430
460 std::unique_ptr< QgsAbstractGeometry > largestEmptyCircle( double tolerance, const QgsAbstractGeometry *boundary = nullptr, QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
461
480 std::unique_ptr< QgsAbstractGeometry > minimumWidth( QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
481
507 double minimumClearance( QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
508
524 std::unique_ptr< QgsAbstractGeometry > minimumClearanceLine( QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
525
540 std::unique_ptr< QgsAbstractGeometry > node( QString *errorMsg SIP_OUT = nullptr ) const;
541
559 std::unique_ptr< QgsAbstractGeometry > sharedPaths( const QgsAbstractGeometry *other, QString *errorMsg SIP_OUT = nullptr ) const;
560
570 SIP_SKIP std::unique_ptr< QgsAbstractGeometry > reshapeGeometry( const QgsLineString &reshapeWithLine, EngineOperationResult *errorCode, QString *errorMsg = nullptr ) const;
571
581 std::unique_ptr< QgsAbstractGeometry > mergeLines( QString *errorMsg SIP_OUT = nullptr ) const;
582
593 std::unique_ptr< QgsAbstractGeometry > closestPoint( const QgsGeometry &other, QString *errorMsg SIP_OUT = nullptr ) const;
594
605 std::unique_ptr< QgsAbstractGeometry > shortestLine( const QgsGeometry &other, QString *errorMsg SIP_OUT = nullptr ) const;
606
618 std::unique_ptr< QgsAbstractGeometry > shortestLine( const QgsAbstractGeometry *other, QString *errorMsg SIP_OUT = nullptr ) const;
619
631 double lineLocatePoint( const QgsPoint &point, QString *errorMsg SIP_OUT = nullptr ) const;
632
651 double lineLocatePoint( double x, double y, QString *errorMsg SIP_OUT = nullptr ) const;
652
663 SIP_SKIP static QgsGeometry polygonize( const QVector<const QgsAbstractGeometry *> &geometries, QString *errorMsg = nullptr );
664
686 std::unique_ptr< QgsAbstractGeometry > voronoiDiagram( const QgsAbstractGeometry *extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg SIP_OUT = nullptr ) const;
687
704 std::unique_ptr< QgsAbstractGeometry > delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg SIP_OUT = nullptr ) const;
705
722 std::unique_ptr< QgsAbstractGeometry > constrainedDelaunayTriangulation( QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
723
751 std::unique_ptr< QgsAbstractGeometry > concaveHull( double targetPercent, bool allowHoles = false, QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
752
774 SIP_SKIP Qgis::CoverageValidityResult validateCoverage( double gapWidth, std::unique_ptr< QgsAbstractGeometry > *invalidEdges, QString *errorMsg = nullptr ) const SIP_THROW( QgsNotSupportedException );
775
799 std::unique_ptr< QgsAbstractGeometry > simplifyCoverageVW( double tolerance, bool preserveBoundary, QString *errorMsg SIP_OUT = nullptr ) const SIP_THROW( QgsNotSupportedException );
800
816 std::unique_ptr< QgsAbstractGeometry > unionCoverage( QString *errorMsg SIP_OUT = nullptr ) const;
817
823 SIP_SKIP static std::unique_ptr< QgsAbstractGeometry > fromGeos( const GEOSGeometry *geos );
824
828 SIP_SKIP static std::unique_ptr< QgsPolygon > fromGeosPolygon( const GEOSGeometry *geos );
829
837 SIP_SKIP static geos::unique_ptr asGeos( const QgsGeometry &geometry, double precision = 0, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
838
847 SIP_SKIP static geos::unique_ptr asGeos( const QgsAbstractGeometry *geometry, double precision = 0, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
848
852 SIP_SKIP static QgsPoint coordSeqPoint( const GEOSCoordSequence *cs, int i, bool hasZ, bool hasM );
853
854 private:
855
856#ifdef SIP_RUN
857 QgsGeos( const QgsGeos & );
858#endif
859
860 mutable geos::unique_ptr mGeos;
861 geos::prepared_unique_ptr mGeosPrepared;
862 double mPrecision = 0.0;
863
864 enum Overlay
865 {
866 OverlayIntersection,
867 OverlayDifference,
868 OverlayUnion,
869 OverlaySymDifference
870 };
871
872 enum Relation
873 {
874 RelationIntersects,
875 RelationTouches,
876 RelationCrosses,
877 RelationWithin,
878 RelationOverlaps,
879 RelationContains,
880 RelationDisjoint
881 };
882
883 //geos util functions
884 void cacheGeos( Qgis::GeosCreationFlags flags ) const;
885
893 std::unique_ptr< QgsAbstractGeometry > overlay( const QgsAbstractGeometry *geom, Overlay op, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
894 bool relation( const QgsAbstractGeometry *geom, Relation r, QString *errorMsg = nullptr ) const;
895 static GEOSCoordSequence *createCoordinateSequence( const QgsCurve *curve, double precision, bool forceClose = false );
896 static std::unique_ptr< QgsLineString > sequenceToLinestring( const GEOSGeometry *geos, bool hasZ, bool hasM );
897 static int numberOfGeometries( GEOSGeometry *g );
898 static geos::unique_ptr nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
899 int mergeGeometriesMultiTypeSplit( std::vector<geos::unique_ptr> &splitResult ) const;
900
904 static geos::unique_ptr createGeosCollection( int typeId, std::vector<geos::unique_ptr> &geoms );
905
906 static geos::unique_ptr createGeosPointXY( double x, double y, bool hasZ, double z, bool hasM, double m, int coordDims, double precision, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
907 static geos::unique_ptr createGeosPoint( const QgsAbstractGeometry *point, int coordDims, double precision, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
908 static geos::unique_ptr createGeosLinestring( const QgsAbstractGeometry *curve, double precision, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
909 static geos::unique_ptr createGeosPolygon( const QgsAbstractGeometry *poly, double precision, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
910
911 //utils for geometry split
912 bool topologicalTestPointsSplit( const GEOSGeometry *splitLine, QgsPointSequence &testPoints, QString *errorMsg = nullptr ) const;
913 geos::unique_ptr linePointDifference( GEOSGeometry *GEOSsplitPoint ) const;
914 EngineOperationResult splitLinearGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
915 EngineOperationResult splitPolygonGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
916
917 //utils for reshape
918 static geos::unique_ptr reshapeLine( const GEOSGeometry *line, const GEOSGeometry *reshapeLineGeos, double precision );
919 static geos::unique_ptr reshapePolygon( const GEOSGeometry *polygon, const GEOSGeometry *reshapeLineGeos, double precision );
920 static int lineContainedInLine( const GEOSGeometry *line1, const GEOSGeometry *line2 );
921 static int pointContainedInLine( const GEOSGeometry *point, const GEOSGeometry *line );
922 static int geomDigits( const GEOSGeometry *geom );
923 void subdivideRecursive( const GEOSGeometry *currentPart, int maxNodes, int depth, QgsGeometryCollection *parts, const QgsRectangle &clipRect, double gridSize = -1 ) const;
924};
925
927
928#ifndef SIP_RUN
929
930class GEOSException : public std::runtime_error
931{
932 public:
933 explicit GEOSException( const QString &message )
934 : std::runtime_error( message.toUtf8().constData() )
935 {
936 }
937};
938
939#endif
940
942
943#endif // QGSGEOS_H
BufferSide
Side of line to buffer.
Definition qgis.h:2006
GeometryOperationResult
Success or failure of a geometry operation.
Definition qgis.h:1952
@ SkipEmptyInteriorRings
Skip any empty polygon interior ring.
QFlags< GeosCreationFlag > GeosCreationFlags
Geos geometry creation behavior flags.
Definition qgis.h:2055
JoinStyle
Join styles for buffers.
Definition qgis.h:2031
EndCapStyle
End cap styles for buffers.
Definition qgis.h:2018
CoverageValidityResult
Coverage validity results.
Definition qgis.h:2064
MakeValidMethod
Algorithms to use when repairing invalid geometries.
Definition qgis.h:2077
@ 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
A geometry engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
EngineOperationResult
Success or failure of a geometry operation.
Encapsulates parameters under which a geometry operation is performed.
A geometry is the spatial representation of a feature.
Used to create and store a proj context object, correctly freeing the context upon destruction.
Definition qgsgeos.h:44
static GEOSContextHandle_t get()
Returns a thread local instance of a GEOS context, safe for use in the current thread.
Does vector analysis using the GEOS library and handles import, export, and exception handling.
Definition qgsgeos.h:139
Line string geometry type, with support for z-dimension and m-values.
Custom exception class which is raised when an operation is not supported.
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.
Contains geos related utilities and functions.
Definition qgsgeos.h:75
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
QVector< QgsPoint > QgsPointSequence
int precision
struct GEOSGeom_t GEOSGeometry
Definition util.h:41