QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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
148
153 static QgsGeometry geometryFromGeos( GEOSGeometry *geos );
154
158 static QgsGeometry geometryFromGeos( const geos::unique_ptr &geos );
159
168 std::unique_ptr< QgsAbstractGeometry > makeValid( Qgis::MakeValidMethod method = Qgis::MakeValidMethod::Linework, bool keepCollapsed = false, QString *errorMsg = nullptr ) const;
169
176 static Qgis::GeometryOperationResult addPart( QgsGeometry &geometry, GEOSGeometry *newPart );
177
178 void geometryChanged() override;
179 void prepareGeometry() override;
180
181 QgsAbstractGeometry *intersection( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
182 QgsAbstractGeometry *difference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
183
188 std::unique_ptr< QgsAbstractGeometry > clip( const QgsRectangle &rectangle, QString *errorMsg = nullptr ) const;
189
207 std::unique_ptr< QgsAbstractGeometry > subdivide( int maxNodes, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
208
209 QgsAbstractGeometry *combine( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
210 QgsAbstractGeometry *combine( const QVector<QgsAbstractGeometry *> &geomList, QString *errorMsg, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
211 QgsAbstractGeometry *combine( const QVector< QgsGeometry > &, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
212 QgsAbstractGeometry *symDifference( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const override;
213 QgsAbstractGeometry *buffer( double distance, int segments, QString *errorMsg = nullptr ) const override;
214 QgsAbstractGeometry *buffer( double distance, int segments, Qgis::EndCapStyle endCapStyle, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
215 QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override;
216 QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override;
217 QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override;
218 QgsPoint *centroid( QString *errorMsg = nullptr ) const override;
219 QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override;
220 QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override;
221 double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
222 bool distanceWithin( const QgsAbstractGeometry *geom, double maxdistance, QString *errorMsg = nullptr ) const override;
223
231 bool contains( double x, double y, QString *errorMsg = nullptr ) const;
232
240 double distance( double x, double y, QString *errorMsg = nullptr ) const;
241
256 double hausdorffDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
257
273 double hausdorffDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
274
287 double frechetDistance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const;
288
311 double frechetDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg = nullptr ) const;
312
313 bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
314 bool touches( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
315 bool crosses( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
316 bool within( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
317 bool overlaps( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
318 bool contains( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
319 bool disjoint( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
320 QString relate( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
321 bool relatePattern( const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg = nullptr ) const override;
322 double area( QString *errorMsg = nullptr ) const override;
323 double length( QString *errorMsg = nullptr ) const override;
324 bool isValid( QString *errorMsg = nullptr, bool allowSelfTouchingHoles = false, QgsGeometry *errorLoc = nullptr ) const override;
325 bool isEqual( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
326 bool isEmpty( QString *errorMsg = nullptr ) const override;
327 bool isSimple( QString *errorMsg = nullptr ) const override;
328
329 EngineOperationResult splitGeometry( const QgsLineString &splitLine,
330 QVector<QgsGeometry> &newGeometries,
331 bool topological,
332 QgsPointSequence &topologyTestPoints,
333 QString *errorMsg = nullptr, bool skipIntersectionCheck = false ) const override;
334
335 QgsAbstractGeometry *offsetCurve( double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit, QString *errorMsg = nullptr ) const override;
336
349 std::unique_ptr< QgsAbstractGeometry > singleSidedBuffer( double distance, int segments, Qgis::BufferSide side,
350 Qgis::JoinStyle joinStyle, double miterLimit,
351 QString *errorMsg = nullptr ) const;
352
377 std::unique_ptr< QgsAbstractGeometry > maximumInscribedCircle( double tolerance, QString *errorMsg = nullptr ) const;
378
402 std::unique_ptr< QgsAbstractGeometry > largestEmptyCircle( double tolerance, const QgsAbstractGeometry *boundary = nullptr, QString *errorMsg = nullptr ) const;
403
418 std::unique_ptr< QgsAbstractGeometry > minimumWidth( QString *errorMsg = nullptr ) const;
419
441 double minimumClearance( QString *errorMsg = nullptr ) const;
442
454 std::unique_ptr< QgsAbstractGeometry > minimumClearanceLine( QString *errorMsg = nullptr ) const;
455
466 std::unique_ptr< QgsAbstractGeometry > node( QString *errorMsg = nullptr ) const;
467
482 std::unique_ptr< QgsAbstractGeometry > sharedPaths( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
483
491 std::unique_ptr< QgsAbstractGeometry > reshapeGeometry( const QgsLineString &reshapeWithLine, EngineOperationResult *errorCode, QString *errorMsg = nullptr ) const;
492
501 QgsGeometry mergeLines( QString *errorMsg = nullptr ) const;
502
507 QgsGeometry closestPoint( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
508
513 QgsGeometry shortestLine( const QgsGeometry &other, QString *errorMsg = nullptr ) const;
514
520 QgsGeometry shortestLine( const QgsAbstractGeometry *other, QString *errorMsg = nullptr ) const;
521
532 double lineLocatePoint( const QgsPoint &point, QString *errorMsg = nullptr ) const;
533
546 double lineLocatePoint( double x, double y, QString *errorMsg = nullptr ) const;
547
556 static QgsGeometry polygonize( const QVector<const QgsAbstractGeometry *> &geometries, QString *errorMsg = nullptr );
557
572 QgsGeometry voronoiDiagram( const QgsAbstractGeometry *extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
573
583 QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false, QString *errorMsg = nullptr ) const;
584
597 std::unique_ptr< QgsAbstractGeometry > constrainedDelaunayTriangulation( QString *errorMsg = nullptr ) const;
598
620 QgsAbstractGeometry *concaveHull( double targetPercent, bool allowHoles = false, QString *errorMsg = nullptr ) const;
621
641 Qgis::CoverageValidityResult validateCoverage( double gapWidth, std::unique_ptr< QgsAbstractGeometry > *invalidEdges, QString *errorMsg = nullptr ) const;
642
664 std::unique_ptr< QgsAbstractGeometry > simplifyCoverageVW( double tolerance, bool preserveBoundary, QString *errorMsg = nullptr ) const;
665
677 std::unique_ptr< QgsAbstractGeometry > unionCoverage( QString *errorMsg = nullptr ) const;
678
683 static std::unique_ptr< QgsAbstractGeometry > fromGeos( const GEOSGeometry *geos );
684 static std::unique_ptr< QgsPolygon > fromGeosPolygon( const GEOSGeometry *geos );
685
686
693 static geos::unique_ptr asGeos( const QgsGeometry &geometry, double precision = 0, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
694
702 static geos::unique_ptr asGeos( const QgsAbstractGeometry *geometry, double precision = 0, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
703
704 static QgsPoint coordSeqPoint( const GEOSCoordSequence *cs, int i, bool hasZ, bool hasM );
705
706 private:
707 mutable geos::unique_ptr mGeos;
708 geos::prepared_unique_ptr mGeosPrepared;
709 double mPrecision = 0.0;
710
711 enum Overlay
712 {
713 OverlayIntersection,
714 OverlayDifference,
715 OverlayUnion,
716 OverlaySymDifference
717 };
718
719 enum Relation
720 {
721 RelationIntersects,
722 RelationTouches,
723 RelationCrosses,
724 RelationWithin,
725 RelationOverlaps,
726 RelationContains,
727 RelationDisjoint
728 };
729
730 //geos util functions
731 void cacheGeos( Qgis::GeosCreationFlags flags ) const;
732
740 std::unique_ptr< QgsAbstractGeometry > overlay( const QgsAbstractGeometry *geom, Overlay op, QString *errorMsg = nullptr, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
741 bool relation( const QgsAbstractGeometry *geom, Relation r, QString *errorMsg = nullptr ) const;
742 static GEOSCoordSequence *createCoordinateSequence( const QgsCurve *curve, double precision, bool forceClose = false );
743 static std::unique_ptr< QgsLineString > sequenceToLinestring( const GEOSGeometry *geos, bool hasZ, bool hasM );
744 static int numberOfGeometries( GEOSGeometry *g );
745 static geos::unique_ptr nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
746 int mergeGeometriesMultiTypeSplit( std::vector<geos::unique_ptr> &splitResult ) const;
747
751 static geos::unique_ptr createGeosCollection( int typeId, std::vector<geos::unique_ptr> &geoms );
752
753 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() );
754 static geos::unique_ptr createGeosPoint( const QgsAbstractGeometry *point, int coordDims, double precision, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
755 static geos::unique_ptr createGeosLinestring( const QgsAbstractGeometry *curve, double precision, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
756 static geos::unique_ptr createGeosPolygon( const QgsAbstractGeometry *poly, double precision, Qgis::GeosCreationFlags flags = Qgis::GeosCreationFlags() );
757
758 //utils for geometry split
759 bool topologicalTestPointsSplit( const GEOSGeometry *splitLine, QgsPointSequence &testPoints, QString *errorMsg = nullptr ) const;
760 geos::unique_ptr linePointDifference( GEOSGeometry *GEOSsplitPoint ) const;
761 EngineOperationResult splitLinearGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
762 EngineOperationResult splitPolygonGeometry( const GEOSGeometry *splitLine, QVector<QgsGeometry > &newGeometries, bool skipIntersectionCheck ) const;
763
764 //utils for reshape
765 static geos::unique_ptr reshapeLine( const GEOSGeometry *line, const GEOSGeometry *reshapeLineGeos, double precision );
766 static geos::unique_ptr reshapePolygon( const GEOSGeometry *polygon, const GEOSGeometry *reshapeLineGeos, double precision );
767 static int lineContainedInLine( const GEOSGeometry *line1, const GEOSGeometry *line2 );
768 static int pointContainedInLine( const GEOSGeometry *point, const GEOSGeometry *line );
769 static int geomDigits( const GEOSGeometry *geom );
770 void subdivideRecursive( const GEOSGeometry *currentPart, int maxNodes, int depth, QgsGeometryCollection *parts, const QgsRectangle &clipRect, double gridSize = -1 ) const;
771};
772
774
775
776class GEOSException : public std::runtime_error
777{
778 public:
779 explicit GEOSException( const QString &message )
780 : std::runtime_error( message.toUtf8().constData() )
781 {
782 }
783};
784
786
787#endif // QGSGEOS_H
BufferSide
Side of line to buffer.
Definition qgis.h:1943
GeometryOperationResult
Success or failure of a geometry operation.
Definition qgis.h:1889
@ SkipEmptyInteriorRings
Skip any empty polygon interior ring.
QFlags< GeosCreationFlag > GeosCreationFlags
Geos geometry creation behavior flags.
Definition qgis.h:1992
JoinStyle
Join styles for buffers.
Definition qgis.h:1968
EndCapStyle
End cap styles for buffers.
Definition qgis.h:1955
CoverageValidityResult
Coverage validity results.
Definition qgis.h:2001
MakeValidMethod
Algorithms to use when repairing invalid geometries.
Definition qgis.h:2014
@ 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