QGIS API Documentation 3.38.0-Grenoble (exported)
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#define SIP_NO_FILE
20
21#include "qgis_core.h"
22#include "qgsgeometryengine.h"
23#include "qgsgeometry.h"
24#include "qgsconfig.h"
25#include <geos_c.h>
26
27class QgsLineString;
28class QgsPolygon;
29class QgsGeometry;
31
32#if !defined(USE_THREAD_LOCAL) || defined(Q_OS_WIN)
33#include <QThreadStorage>
34#endif
35
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
136class CORE_EXPORT QgsGeos: public QgsGeometryEngine
137{
138 public:
139
146 QgsGeos( const QgsAbstractGeometry *geometry, double precision = 0, bool allowInvalidSubGeom = true );
147
152 static QgsGeometry geometryFromGeos( GEOSGeometry *geos );
153
157 static QgsGeometry geometryFromGeos( const geos::unique_ptr &geos );
158
167 std::unique_ptr< QgsAbstractGeometry > makeValid( Qgis::MakeValidMethod method = Qgis::MakeValidMethod::Linework, bool keepCollapsed = false, QString *errorMsg = nullptr ) const;
168
175 static Qgis::GeometryOperationResult addPart( QgsGeometry &geometry, GEOSGeometry *newPart );
176
177 void geometryChanged() override;
178 void prepareGeometry() override;
179
180 QgsAbstractGeometry *intersection( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
181 QgsAbstractGeometry *difference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
182
187 std::unique_ptr< QgsAbstractGeometry > clip( const QgsRectangle &rectangle, QString *errorMsg = nullptr ) const;
188
206 std::unique_ptr< QgsAbstractGeometry > subdivide( int maxNodes, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
207
208 QgsAbstractGeometry *combine( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
209 QgsAbstractGeometry *combine( const QVector<QgsAbstractGeometry *> &geomList, QString *errorMsg, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
210 QgsAbstractGeometry *combine( const QVector< QgsGeometry > &, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
211 QgsAbstractGeometry *symDifference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
212 QgsAbstractGeometry *buffer( double distance, int segments, QString *errorMsg = nullptr ) const override;
213 QgsAbstractGeometry *buffer( double distance, int segments, Qgis::EndCapStyle endCapStyle, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
214 QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override;
215 QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override;
216 QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override;
217 QgsPoint *centroid( QString *errorMsg = nullptr ) const override;
218 QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override;
219 QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override;
220 double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
221 bool distanceWithin( const QgsAbstractGeometry *geom, double maxdistance, QString *errorMsg = nullptr ) const override;
222
230 bool contains( double x, double y, QString *errorMsg = nullptr ) const;
231
239 double distance( double x, double y, QString *errorMsg = nullptr ) const;
240
255 double hausdorffDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
256
272 double hausdorffDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
273
286 double frechetDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
287
310 double frechetDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
311
312 bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
313 bool touches( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
314 bool crosses( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
315 bool within( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
316 bool overlaps( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
317 bool contains( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
318 bool disjoint( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
319 QString relate( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
320 bool relatePattern( const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg = nullptr ) const override;
321 double area( QString *errorMsg = nullptr ) const override;
322 double length( QString *errorMsg = nullptr ) const override;
323 bool isValid( QString *errorMsg = nullptr, bool allowSelfTouchingHoles = false, QgsGeometry *errorLoc = nullptr ) const override;
324 bool isEqual( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
325 bool isEmpty( QString *errorMsg = nullptr ) const override;
326 bool isSimple( QString *errorMsg = nullptr ) const override;
327
328 EngineOperationResult splitGeometry( const QgsLineString &splitLine,
329 QVector<QgsGeometry> &newGeometries,
330 bool topological,
331 QgsPointSequence &topologyTestPoints,
332 QString *errorMsg = nullptr, bool skipIntersectionCheck = false ) const override;
333
334 QgsAbstractGeometry *offsetCurve( double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
335
348 std::unique_ptr< QgsAbstractGeometry > singleSidedBuffer( double distance, int segments, Qgis::BufferSide side,
349 Qgis::JoinStyle joinStyle, double miterLimit,
350 QString *errorMsg = nullptr ) const;
351
376 std::unique_ptr< QgsAbstractGeometry > maximumInscribedCircle( double tolerance, QString *errorMsg = nullptr ) const;
377
401 std::unique_ptr< QgsAbstractGeometry > largestEmptyCircle( double tolerance, const QgsAbstractGeometry *boundary = nullptr, QString *errorMsg = nullptr ) const;
402
417 std::unique_ptr< QgsAbstractGeometry > minimumWidth( QString *errorMsg = nullptr ) const;
418
440 double minimumClearance( QString *errorMsg = nullptr ) const;
441
453 std::unique_ptr< QgsAbstractGeometry > minimumClearanceLine( QString *errorMsg = nullptr ) const;
454
465 std::unique_ptr< QgsAbstractGeometry > node( QString *errorMsg = nullptr ) const;
466
481 std::unique_ptr< QgsAbstractGeometry > sharedPaths( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
482
490 std::unique_ptr< QgsAbstractGeometry > reshapeGeometry( const QgsLineString &reshapeWithLine, EngineOperationResult *errorCode, QString *errorMsg = nullptr ) const;
491
500 QgsGeometry mergeLines( QString *errorMsg = nullptr ) const;
501
506 QgsGeometry closestPoint( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
507
512 QgsGeometry shortestLine( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
513
519 QgsGeometry shortestLine( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
520
531 double lineLocatePoint( const QgsPoint &point, QString *errorMsg = nullptr ) const;
532
545 double lineLocatePoint( double x, double y, QString *errorMsg = nullptr ) const;
546
555 static QgsGeometry polygonize( const QVector<const QgsAbstractGeometry *> &geometries, QString *errorMsg = nullptr );
556
571 QgsGeometry voronoiDiagram( const QgsAbstractGeometry *extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
572
582 QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
583
596 std::unique_ptr< QgsAbstractGeometry > constrainedDelaunayTriangulation( QString *errorMsg = nullptr ) const;
597
619 QgsAbstractGeometry *concaveHull( double targetPercent, bool allowHoles = false, QString *errorMsg = nullptr ) const;
620
640 Qgis::CoverageValidityResult validateCoverage( double gapWidth, std::unique_ptr< QgsAbstractGeometry > *invalidEdges, QString *errorMsg = nullptr ) const;
641
663 std::unique_ptr< QgsAbstractGeometry > simplifyCoverageVW( double tolerance, bool preserveBoundary, QString *errorMsg = nullptr ) const;
664
676 std::unique_ptr< QgsAbstractGeometry > unionCoverage( QString *errorMsg = nullptr ) const;
677
682 static std::unique_ptr< QgsAbstractGeometry > fromGeos( const GEOSGeometry *geos );
683 static std::unique_ptr< QgsPolygon > fromGeosPolygon( const GEOSGeometry *geos );
684
685
691 static geos::unique_ptr asGeos( const QgsGeometry &geometry, double precision = 0 );
692
699 static geos::unique_ptr asGeos( const QgsAbstractGeometry *geometry, double precision = 0, bool allowInvalidSubGeom = true );
700 static QgsPoint coordSeqPoint( const GEOSCoordSequence *cs, int i, bool hasZ, bool hasM );
701
702 private:
703 mutable geos::unique_ptr mGeos;
704 geos::prepared_unique_ptr mGeosPrepared;
705 double mPrecision = 0.0;
706
707 enum Overlay
708 {
709 OverlayIntersection,
710 OverlayDifference,
711 OverlayUnion,
712 OverlaySymDifference
713 };
714
715 enum Relation
716 {
717 RelationIntersects,
718 RelationTouches,
719 RelationCrosses,
720 RelationWithin,
721 RelationOverlaps,
722 RelationContains,
723 RelationDisjoint
724 };
725
726 //geos util functions
727 void cacheGeos( bool allowInvalidSubGeom ) const;
728
736 std::unique_ptr< QgsAbstractGeometry > overlay( const QgsAbstractGeometry *geom, Overlay op, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
737 bool relation( const QgsAbstractGeometry *geom, Relation r, QString *errorMsg = nullptr ) const;
738 static GEOSCoordSequence *createCoordinateSequence( const QgsCurve *curve, double precision, bool forceClose = false );
739 static std::unique_ptr< QgsLineString > sequenceToLinestring( const GEOSGeometry *geos, bool hasZ, bool hasM );
740 static int numberOfGeometries( GEOSGeometry *g );
741 static geos::unique_ptr nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
742 int mergeGeometriesMultiTypeSplit( std::vector<geos::unique_ptr> &splitResult ) const;
743
747 static geos::unique_ptr createGeosCollection( int typeId, std::vector<geos::unique_ptr> &geoms );
748
749 static geos::unique_ptr createGeosPointXY( double x, double y, bool hasZ, double z, bool hasM, double m, int coordDims, double precision );
750 static geos::unique_ptr createGeosPoint( const QgsAbstractGeometry *point, int coordDims, double precision );
751 static geos::unique_ptr createGeosLinestring( const QgsAbstractGeometry *curve, double precision );
752 static geos::unique_ptr createGeosPolygon( const QgsAbstractGeometry *poly, double precision );
753
754 //utils for geometry split
755 bool topologicalTestPointsSplit( const GEOSGeometry *splitLine, QgsPointSequence &testPoints, QString *errorMsg = nullptr ) const;
756 geos::unique_ptr linePointDifference( GEOSGeometry *GEOSsplitPoint ) const;
757 EngineOperationResult splitLinearGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
758 EngineOperationResult splitPolygonGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
759
760 //utils for reshape
761 static geos::unique_ptr reshapeLine( const GEOSGeometry *line, const GEOSGeometry *reshapeLineGeos, double precision );
762 static geos::unique_ptr reshapePolygon( const GEOSGeometry *polygon, const GEOSGeometry *reshapeLineGeos, double precision );
763 static int lineContainedInLine( const GEOSGeometry *line1, const GEOSGeometry *line2 );
764 static int pointContainedInLine( const GEOSGeometry *point, const GEOSGeometry *line );
765 static int geomDigits( const GEOSGeometry *geom );
766 void subdivideRecursive( const GEOSGeometry *currentPart, int maxNodes, int depth, QgsGeometryCollection *parts, const QgsRectangle &clipRect, double gridSize = -1 ) const;
767};
768
770
771
772class GEOSException : public std::runtime_error
773{
774 public:
775 explicit GEOSException( const QString &message )
776 : std::runtime_error( message.toUtf8().constData() )
777 {
778 }
779};
780
782
783#endif // QGSGEOS_H
BufferSide
Side of line to buffer.
Definition qgis.h:1763
GeometryOperationResult
Success or failure of a geometry operation.
Definition qgis.h:1709
JoinStyle
Join styles for buffers.
Definition qgis.h:1788
EndCapStyle
End cap styles for buffers.
Definition qgis.h:1775
CoverageValidityResult
Coverage validity results.
Definition qgis.h:1801
MakeValidMethod
Algorithms to use when repairing invalid geometries.
Definition qgis.h:1814
@ 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, exception handling*.
Definition qgsgeos.h:137
Line string geometry type, with support for z-dimension and m-values.
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
QVector< QgsPoint > QgsPointSequence
int precision
struct GEOSGeom_t GEOSGeometry
Definition util.h:41