QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsgeometry.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometry.h - Geometry (stored as Open Geospatial Consortium WKB)
3 -------------------------------------------------------------------
4Date : 02 May 2005
5Copyright : (C) 2005 by Brendan Morley
6email : morb at ozemail dot com dot au
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 QGSGEOMETRY_H
17#define QGSGEOMETRY_H
18
19#include <functional>
20
21#include <QDomDocument>
22#include <QJsonObject>
23#include <QSet>
24#include <QString>
25#include <QVector>
26
27#include <climits>
28#include <limits>
29#include <memory>
30
31#include "qgis_core.h"
32#include "qgis_sip.h"
33
34#include "qgsabstractgeometry.h"
35#include "qgspointxy.h"
36#include "qgspoint.h"
37#include "qgsfeatureid.h"
38#include "qgsvertexid.h"
39
40#ifndef SIP_RUN
41#include "json_fwd.hpp"
42using namespace nlohmann;
43#endif
44
46class QgsVectorLayer;
47class QgsMapToPixel;
48class QPainter;
49class QgsPolygon;
50class QgsLineString;
51class QgsCurve;
52class QgsFeedback;
53
63typedef QVector<QgsPointXY> QgsPolylineXY;
64
73
75#ifndef SIP_RUN
76typedef QVector<QgsPolylineXY> QgsPolygonXY;
77#else
78typedef QVector<QVector<QgsPointXY>> QgsPolygonXY;
79#endif
80
82typedef QVector<QgsPointXY> QgsMultiPointXY;
83
85#ifndef SIP_RUN
86typedef QVector<QgsPolylineXY> QgsMultiPolylineXY;
87#else
88typedef QVector<QVector<QgsPointXY>> QgsMultiPolylineXY;
89#endif
90
92#ifndef SIP_RUN
93typedef QVector<QgsPolygonXY> QgsMultiPolygonXY;
94#else
95typedef QVector<QVector<QVector<QgsPointXY>>> QgsMultiPolygonXY;
96#endif
97
98class QgsRectangle;
99
100class QgsConstWkbPtr;
101
102struct QgsGeometryPrivate;
103
110class CORE_EXPORT QgsGeometryParameters
111{
112 public:
113
124 double gridSize() const { return mGridSize; }
125
136 void setGridSize( double size ) { mGridSize = size; }
137
138 private:
139
140 double mGridSize = -1;
141};
142
163class CORE_EXPORT QgsGeometry
164{
165 Q_GADGET
166 Q_PROPERTY( bool isNull READ isNull )
167 Q_PROPERTY( QgsWkbTypes::GeometryType type READ type )
168
169 public:
170
173
175 QgsGeometry( const QgsGeometry & );
176
181 QgsGeometry &operator=( QgsGeometry const &rhs ) SIP_SKIP;
182
189
195 explicit QgsGeometry( std::unique_ptr< QgsAbstractGeometry > geom ) SIP_SKIP;
196
197 virtual ~QgsGeometry();
198
210 const QgsAbstractGeometry *constGet() const SIP_HOLDGIL;
211
224 QgsAbstractGeometry *get();
225
238 void set( QgsAbstractGeometry *geometry SIP_TRANSFER ) SIP_DEPRECATED;
239
248 bool isNull() const SIP_HOLDGIL;
249
251 static QgsGeometry fromWkt( const QString &wkt );
253 static QgsGeometry fromPointXY( const QgsPointXY &point ) SIP_HOLDGIL;
255 static QgsGeometry fromMultiPointXY( const QgsMultiPointXY &multipoint );
256
268 static QgsGeometry fromPolylineXY( const QgsPolylineXY &polyline );
269
279 static QgsGeometry fromPolyline( const QgsPolyline &polyline );
280
284 static QgsGeometry fromMultiPolylineXY( const QgsMultiPolylineXY &multiline );
285
286#ifndef SIP_RUN
287
291#else
292
310#endif
311 static QgsGeometry fromPolygonXY( const QgsPolygonXY &polygon );
312
316 static QgsGeometry fromMultiPolygonXY( const QgsMultiPolygonXY &multipoly );
317
319 static QgsGeometry fromRect( const QgsRectangle &rect ) SIP_HOLDGIL;
321 static QgsGeometry collectGeometry( const QVector<QgsGeometry> &geometries );
322
338 static QgsGeometry createWedgeBuffer( const QgsPoint &center, double azimuth, double angularWidth,
339 double outerRadius, double innerRadius = 0 );
340
346 void fromWkb( unsigned char *wkb, int length ) SIP_SKIP;
347
352 void fromWkb( const QByteArray &wkb );
353
358 QgsWkbTypes::Type wkbType() const SIP_HOLDGIL;
359
364 QgsWkbTypes::GeometryType type() const SIP_HOLDGIL;
365
372 bool isEmpty() const;
373
375 bool isMultipart() const SIP_HOLDGIL;
376
391 bool equals( const QgsGeometry &geometry ) const;
392
409 bool isGeosEqual( const QgsGeometry & ) const;
410
418 bool isGeosValid( Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const;
419
428 bool isSimple() const;
429
443 bool isAxisParallelRectangle( double maximumDeviation, bool simpleRectanglesOnly = false ) const;
444
457 double area() const;
458
473 double length() const;
474
482 double distance( const QgsGeometry &geom ) const;
483
484#ifndef SIP_RUN
485
486 // TODO QGIS 4: consider renaming vertices_begin, vertices_end, parts_begin, parts_end, etc
487 // to camelCase
488
493 QgsAbstractGeometry::vertex_iterator vertices_begin() const;
494
499 QgsAbstractGeometry::vertex_iterator vertices_end() const;
500#endif
501
525 QgsVertexIterator vertices() const;
526
527#ifndef SIP_RUN
528
538
548
557 QgsAbstractGeometry::const_part_iterator const_parts_begin() const;
558
567 QgsAbstractGeometry::const_part_iterator const_parts_end() const;
568#endif
569
608
641 QgsGeometryConstPartIterator constParts() const;
642
660 double hausdorffDistance( const QgsGeometry &geom ) const;
661
680 double hausdorffDistanceDensify( const QgsGeometry &geom, double densifyFraction ) const;
681
696 double frechetDistance( const QgsGeometry &geom ) const SIP_THROW( QgsNotSupportedException );
697
720 double frechetDistanceDensify( const QgsGeometry &geom, double densifyFraction ) const SIP_THROW( QgsNotSupportedException );
721
734 QgsPointXY closestVertex( const QgsPointXY &point, int &closestVertexIndex SIP_OUT, int &previousVertexIndex SIP_OUT, int &nextVertexIndex SIP_OUT, double &sqrDist SIP_OUT ) const;
735
744 double distanceToVertex( int vertex ) const;
745
753 double angleAtVertex( int vertex ) const;
754
767 void adjacentVertices( int atVertex, int &beforeVertex SIP_OUT, int &afterVertex SIP_OUT ) const;
768
781 bool insertVertex( double x, double y, int beforeVertex );
782
795 bool insertVertex( const QgsPoint &point, int beforeVertex );
796
804 bool moveVertex( double x, double y, int atVertex );
805
813 bool moveVertex( const QgsPoint &p, int atVertex );
814
826 bool deleteVertex( int atVertex );
827
835 bool toggleCircularAtVertex( int atVertex );
836
842 QgsPoint vertexAt( int atVertex ) const;
843
849 double sqrDistToVertexAt( QgsPointXY &point SIP_IN, int atVertex ) const;
850
856 QgsGeometry nearestPoint( const QgsGeometry &other ) const;
857
868 QgsGeometry shortestLine( const QgsGeometry &other ) const;
869
876 double closestVertexWithContext( const QgsPointXY &point, int &atVertex SIP_OUT ) const;
877
889 double closestSegmentWithContext( const QgsPointXY &point, QgsPointXY &minDistPoint SIP_OUT, int &nextVertexIndex SIP_OUT, int *leftOrRightOfSegment SIP_OUT = nullptr, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
890
896 Qgis::GeometryOperationResult addRing( const QVector<QgsPointXY> &ring );
897
904
911 Qgis::GeometryOperationResult addPart( const QVector<QgsPointXY> &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry ) SIP_PYNAME( addPointsXY );
912
920
928
934 Qgis::GeometryOperationResult addPart( const QgsGeometry &newPart ) SIP_PYNAME( addPartGeometry );
935
942 QgsGeometry removeInteriorRings( double minimumAllowedArea = -1 ) const;
943
948 Qgis::GeometryOperationResult translate( double dx, double dy, double dz = 0.0, double dm = 0.0 );
949
964 Qgis::GeometryOperationResult transform( const QgsCoordinateTransform &ct, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward, bool transformZ = false ) SIP_THROW( QgsCsException );
965
974 Qgis::GeometryOperationResult transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );
975
982 Qgis::GeometryOperationResult rotate( double rotation, const QgsPointXY &center );
983
994 Q_DECL_DEPRECATED Qgis::GeometryOperationResult splitGeometry( const QVector<QgsPointXY> &splitLine, QVector<QgsGeometry> &newGeometries SIP_OUT, bool topological, QVector<QgsPointXY> &topologyTestPoints SIP_OUT, bool splitFeature = true ) SIP_DEPRECATED;
995
1018 Qgis::GeometryOperationResult splitGeometry( const QgsPointSequence &splitLine, QVector<QgsGeometry> &newGeometries SIP_OUT, bool topological, QgsPointSequence &topologyTestPoints SIP_OUT, bool splitFeature = true, bool skipIntersectionTest SIP_PYARGREMOVE = false );
1019
1031 Qgis::GeometryOperationResult splitGeometry( const QgsCurve *curve, QVector<QgsGeometry> &newGeometries SIP_OUT, bool preserveCircular, bool topological, QgsPointSequence &topologyTestPoints SIP_OUT, bool splitFeature = true );
1032
1037 Qgis::GeometryOperationResult reshapeGeometry( const QgsLineString &reshapeLineString );
1038
1044 int makeDifferenceInPlace( const QgsGeometry &other ) SIP_SKIP;
1045
1053 QgsGeometry makeDifference( const QgsGeometry &other ) const;
1054
1059 QgsRectangle boundingBox() const;
1060
1072 QgsGeometry orientedMinimumBoundingBox( double &area SIP_OUT, double &angle SIP_OUT, double &width SIP_OUT, double &height SIP_OUT ) const;
1073
1083 QgsGeometry orientedMinimumBoundingBox() const SIP_SKIP;
1084
1093 QgsGeometry minimalEnclosingCircle( QgsPointXY &center SIP_OUT, double &radius SIP_OUT, unsigned int segments = 36 ) const;
1094
1100 QgsGeometry minimalEnclosingCircle( unsigned int segments = 36 ) const SIP_SKIP;
1101
1110 QgsGeometry orthogonalize( double tolerance = 1.0E-8, int maxIterations = 1000, double angleThreshold = 15.0 ) const;
1111
1124 QgsGeometry triangularWaves( double wavelength, double amplitude, bool strictWavelength = false ) const;
1125
1144 QgsGeometry triangularWavesRandomized( double minimumWavelength, double maximumWavelength, double minimumAmplitude, double maximumAmplitude, unsigned long seed = 0 ) const;
1145
1158 QgsGeometry squareWaves( double wavelength, double amplitude, bool strictWavelength = false ) const;
1159
1178 QgsGeometry squareWavesRandomized( double minimumWavelength, double maximumWavelength, double minimumAmplitude, double maximumAmplitude, unsigned long seed = 0 ) const;
1179
1192 QgsGeometry roundWaves( double wavelength, double amplitude, bool strictWavelength = false ) const;
1193
1212 QgsGeometry roundWavesRandomized( double minimumWavelength, double maximumWavelength, double minimumAmplitude, double maximumAmplitude, unsigned long seed = 0 ) const;
1213
1227 QgsGeometry applyDashPattern( const QVector< double > &pattern,
1228 Qgis::DashPatternLineEndingRule startRule = Qgis::DashPatternLineEndingRule::NoRule,
1229 Qgis::DashPatternLineEndingRule endRule = Qgis::DashPatternLineEndingRule::NoRule,
1230 Qgis::DashPatternSizeAdjustment adjustment = Qgis::DashPatternSizeAdjustment::ScaleBothDashAndGap,
1231 double patternOffset = 0 ) const;
1232
1245 QgsGeometry snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const;
1246
1267 bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false );
1268
1278 bool intersects( const QgsRectangle &rectangle ) const;
1279
1294 bool intersects( const QgsGeometry &geometry ) const;
1295
1305 bool boundingBoxIntersects( const QgsRectangle &rectangle ) const;
1306
1316 bool boundingBoxIntersects( const QgsGeometry &geometry ) const;
1317
1321 bool contains( const QgsPointXY *p ) const;
1322
1333 bool contains( const QgsGeometry &geometry ) const;
1334
1345 bool disjoint( const QgsGeometry &geometry ) const;
1346
1357 bool touches( const QgsGeometry &geometry ) const;
1358
1369 bool overlaps( const QgsGeometry &geometry ) const;
1370
1381 bool within( const QgsGeometry &geometry ) const;
1382
1393 bool crosses( const QgsGeometry &geometry ) const;
1394
1402 QgsGeometry buffer( double distance, int segments ) const;
1403
1416 QgsGeometry buffer( double distance, int segments, Qgis::EndCapStyle endCapStyle, Qgis::JoinStyle joinStyle, double miterLimit ) const;
1417
1426 QgsGeometry offsetCurve( double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit ) const;
1427
1443 QgsGeometry singleSidedBuffer( double distance, int segments, Qgis::BufferSide side,
1444 Qgis::JoinStyle joinStyle = Qgis::JoinStyle::Round,
1445 double miterLimit = 2.0 ) const;
1446
1464 QgsGeometry taperedBuffer( double startWidth, double endWidth, int segments ) const;
1465
1480 QgsGeometry variableWidthBufferByM( int segments ) const;
1481
1488 QgsGeometry extendLine( double startDistance, double endDistance ) const;
1489
1491 QgsGeometry simplify( double tolerance ) const;
1492
1502 QgsGeometry densifyByCount( int extraNodesPerSegment ) const;
1503
1518 QgsGeometry densifyByDistance( double distance ) const;
1519
1535 QgsGeometry convertToCurves( double distanceTolerance = 1e-8, double angleTolerance = 1e-8 ) const;
1536
1550 QgsGeometry centroid() const;
1551
1565 QgsGeometry pointOnSurface() const;
1566
1579 QgsGeometry poleOfInaccessibility( double precision, double *distanceToBoundary SIP_OUT = nullptr ) const;
1580
1604 QgsGeometry largestEmptyCircle( double tolerance, const QgsGeometry &boundary = QgsGeometry() ) const SIP_THROW( QgsNotSupportedException );
1605
1620 QgsGeometry minimumWidth() const SIP_THROW( QgsNotSupportedException );
1621
1643 double minimumClearance() const SIP_THROW( QgsNotSupportedException );
1644
1656 QgsGeometry minimumClearanceLine() const SIP_THROW( QgsNotSupportedException );
1657
1666 QgsGeometry convexHull() const;
1667
1681 QgsGeometry concaveHull( double targetPercent, bool allowHoles = false ) const SIP_THROW( QgsNotSupportedException );
1682
1698 QgsGeometry voronoiDiagram( const QgsGeometry &extent = QgsGeometry(), double tolerance = 0.0, bool edgesOnly = false ) const;
1699
1709 QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false ) const;
1710
1721 QgsGeometry node() const;
1722
1737 QgsGeometry sharedPaths( const QgsGeometry &other ) const;
1738
1761 QgsGeometry subdivide( int maxNodes = 256, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
1762
1778 QgsGeometry interpolate( double distance ) const;
1779
1791 double lineLocatePoint( const QgsGeometry &point ) const;
1792
1802 double interpolateAngle( double distance ) const;
1803
1815 QgsGeometry intersection( const QgsGeometry &geometry, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
1816
1824 QgsGeometry clipped( const QgsRectangle &rectangle );
1825
1840 QgsGeometry combine( const QgsGeometry &geometry, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
1841
1850 QgsGeometry mergeLines() const;
1851
1863 QgsGeometry difference( const QgsGeometry &geometry, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
1864
1876 QgsGeometry symDifference( const QgsGeometry &geometry, const QgsGeometryParameters &parameters = QgsGeometryParameters() ) const;
1877
1879 QgsGeometry extrude( double x, double y );
1880
1881#ifndef SIP_RUN
1882
1904 QVector< QgsPointXY > randomPointsInPolygon( int count, const std::function< bool( const QgsPointXY & ) > &acceptPoint, unsigned long seed = 0, QgsFeedback *feedback = nullptr, int maxTriesPerPoint = 0 ) const;
1905
1919 QVector< QgsPointXY > randomPointsInPolygon( int count, unsigned long seed = 0, QgsFeedback *feedback = nullptr ) const;
1921#else
1922
1936 SIP_PYOBJECT randomPointsInPolygon( int count, unsigned long seed = 0 ) const SIP_TYPEHINT( QgsPolylineXY );
1937 % MethodCode
1938 const QgsWkbTypes::GeometryType type = sipCpp->type();
1939 if ( sipCpp->isNull() )
1940 {
1941 PyErr_SetString( PyExc_ValueError, QStringLiteral( "Cannot generate points inside a null geometry." ).toUtf8().constData() );
1942 sipIsErr = 1;
1943 }
1944 else if ( type != QgsWkbTypes::PolygonGeometry )
1945 {
1946 PyErr_SetString( PyExc_TypeError, QStringLiteral( "Cannot generate points inside a %1 geometry. Only Polygon types are permitted." ).arg( QgsWkbTypes::displayString( sipCpp->wkbType() ) ).toUtf8().constData() );
1947 sipIsErr = 1;
1948 }
1949 else
1950 {
1951 const sipTypeDef *qvector_type = sipFindType( "QVector<QgsPointXY>" );
1952 sipRes = sipConvertFromNewType( new QVector< QgsPointXY >( sipCpp->randomPointsInPolygon( a0, a1 ) ), qvector_type, Py_None );
1953 }
1954 % End
1955
1956
1957#endif
1959
1967 int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const;
1968
1976 QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const;
1977
1983 QString asWkt( int precision = 17 ) const;
1984
1985#ifdef SIP_RUN
1986 SIP_PYOBJECT __repr__();
1987 % MethodCode
1988 QString str;
1989 if ( sipCpp->isNull() )
1990 str = QStringLiteral( "<QgsGeometry: null>" );
1991 else
1992 {
1993 QString wkt = sipCpp->asWkt();
1994 if ( wkt.length() > 1000 )
1995 wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
1996 str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
1997 }
1998 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1999 % End
2000#endif
2001
2005 QString asJson( int precision = 17 ) const;
2006
2012 virtual json asJsonObject( int precision = 17 ) const SIP_SKIP;
2013
2040 QVector< QgsGeometry > coerceToType( QgsWkbTypes::Type type, double defaultZ = 0, double defaultM = 0 ) const;
2041
2054 QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const;
2055
2056 /* Accessor functions for getting geometry data */
2057
2058#ifndef SIP_RUN
2059
2068 QgsPointXY asPoint() const;
2069#else
2070
2081 SIP_PYOBJECT asPoint() const SIP_TYPEHINT( QgsPointXY );
2082 % MethodCode
2083 if ( sipCpp->isNull() )
2084 {
2085 PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a point." ).toUtf8().constData() );
2086 sipIsErr = 1;
2087 }
2088 else
2089 {
2090 const QgsAbstractGeometry *geom = sipCpp->constGet();
2092 {
2093 PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a point. Only Point types are permitted." ).arg( QgsWkbTypes::displayString( geom->wkbType() ) ).toUtf8().constData() );
2094 sipIsErr = 1;
2095 }
2096 else
2097 {
2098 sipRes = sipConvertFromNewType( new QgsPointXY( sipCpp->asPoint() ), sipType_QgsPointXY, Py_None );
2099 }
2100 }
2101 % End
2102#endif
2103
2104#ifndef SIP_RUN
2105
2114 QgsPolylineXY asPolyline() const;
2115#else
2116
2128 SIP_PYOBJECT asPolyline() const SIP_TYPEHINT( QgsPolylineXY );
2129 % MethodCode
2130 const QgsWkbTypes::Type type = sipCpp->wkbType();
2131 if ( sipCpp->isNull() )
2132 {
2133 PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a polyline." ).toUtf8().constData() );
2134 sipIsErr = 1;
2135 }
2137 {
2138 PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a polyline. Only single line or curve types are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
2139 sipIsErr = 1;
2140 }
2141 else
2142 {
2143 const sipTypeDef *qvector_type = sipFindType( "QVector< QgsPointXY >" );
2144 sipRes = sipConvertFromNewType( new QgsPolylineXY( sipCpp->asPolyline() ), qvector_type, Py_None );
2145 }
2146 % End
2147#endif
2148
2149#ifndef SIP_RUN
2150
2159 QgsPolygonXY asPolygon() const;
2160#else
2161
2173 SIP_PYOBJECT asPolygon() const SIP_TYPEHINT( QgsPolygonXY );
2174 % MethodCode
2175 const QgsWkbTypes::Type type = sipCpp->wkbType();
2176 if ( sipCpp->isNull() )
2177 {
2178 PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a polygon." ).toUtf8().constData() );
2179 sipIsErr = 1;
2180 }
2182 {
2183 PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a polygon. Only single polygon or curve polygon types are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
2184 sipIsErr = 1;
2185 }
2186 else
2187 {
2188 const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QgsPointXY>>" );
2189 sipRes = sipConvertFromNewType( new QgsPolygonXY( sipCpp->asPolygon() ), qvector_type, Py_None );
2190 }
2191 % End
2192#endif
2193
2194#ifndef SIP_RUN
2195
2203 QgsMultiPointXY asMultiPoint() const;
2204#else
2205
2216 SIP_PYOBJECT asMultiPoint() const SIP_TYPEHINT( QgsMultiPointXY );
2217 % MethodCode
2218 const QgsWkbTypes::Type type = sipCpp->wkbType();
2219 if ( sipCpp->isNull() )
2220 {
2221 PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multipoint." ).toUtf8().constData() );
2222 sipIsErr = 1;
2223 }
2225 {
2226 PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a multipoint. Only multipoint types are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
2227 sipIsErr = 1;
2228 }
2229 else
2230 {
2231 const sipTypeDef *qvector_type = sipFindType( "QVector< QgsPointXY >" );
2232 sipRes = sipConvertFromNewType( new QgsPolylineXY( sipCpp->asMultiPoint() ), qvector_type, Py_None );
2233 }
2234 % End
2235#endif
2236
2237#ifndef SIP_RUN
2238
2247 QgsMultiPolylineXY asMultiPolyline() const;
2248#else
2249
2261 SIP_PYOBJECT asMultiPolyline() const SIP_TYPEHINT( QgsMultiPolylineXY );
2262 % MethodCode
2263 const QgsWkbTypes::Type type = sipCpp->wkbType();
2264 if ( sipCpp->isNull() )
2265 {
2266 PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multilinestring." ).toUtf8().constData() );
2267 sipIsErr = 1;
2268 }
2270 {
2271 PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a multilinestring. Only multi linestring or curves are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
2272 sipIsErr = 1;
2273 }
2274 else
2275 {
2276 const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QgsPointXY>>" );
2277 sipRes = sipConvertFromNewType( new QgsMultiPolylineXY( sipCpp->asMultiPolyline() ), qvector_type, Py_None );
2278 }
2279 % End
2280#endif
2281
2282#ifndef SIP_RUN
2283
2292 QgsMultiPolygonXY asMultiPolygon() const;
2293#else
2294
2306 SIP_PYOBJECT asMultiPolygon() const SIP_TYPEHINT( QgsMultiPolygonXY );
2307 % MethodCode
2308 const QgsWkbTypes::Type type = sipCpp->wkbType();
2309 if ( sipCpp->isNull() )
2310 {
2311 PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multipolygon." ).toUtf8().constData() );
2312 sipIsErr = 1;
2313 }
2315 {
2316 PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a multipolygon. Only multi polygon or curves are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
2317 sipIsErr = 1;
2318 }
2319 else
2320 {
2321 const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QVector<QgsPointXY>>>" );
2322 sipRes = sipConvertFromNewType( new QgsMultiPolygonXY( sipCpp->asMultiPolygon() ), qvector_type, Py_None );
2323 }
2324 % End
2325#endif
2326
2331 QVector<QgsGeometry> asGeometryCollection() const;
2332
2338 QPointF asQPointF() const SIP_HOLDGIL;
2339
2352 QPolygonF asQPolygonF() const SIP_HOLDGIL;
2353
2360 bool deleteRing( int ringNum, int partNum = 0 );
2361
2367 bool deletePart( int partNum );
2368
2377 bool convertToMultiType();
2378
2388 bool convertToSingleType();
2389
2399 bool convertGeometryCollectionToSubclass( QgsWkbTypes::GeometryType geomType );
2400
2411 int avoidIntersections( const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
2412 const QHash<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures SIP_PYARGREMOVE = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
2413
2435 QgsGeometry makeValid( Qgis::MakeValidMethod method = Qgis::MakeValidMethod::Linework, bool keepCollapsed = false ) const SIP_THROW( QgsNotSupportedException );
2436
2449 QgsGeometry forceRHR() const;
2450
2459 QgsGeometry forcePolygonClockwise() const;
2460
2469 QgsGeometry forcePolygonCounterClockwise() const;
2470
2475 class CORE_EXPORT Error
2476 {
2477 public:
2479 : mMessage( QStringLiteral( "none" ) )
2480 {}
2481
2482 explicit Error( const QString &m )
2483 : mMessage( m )
2484 {}
2485
2486 Error( const QString &m, const QgsPointXY &p )
2487 : mMessage( m )
2488 , mLocation( p )
2489 , mHasLocation( true ) {}
2490
2494 QString what() const;
2495
2499 QgsPointXY where() const;
2500
2504 bool hasWhere() const;
2505
2506#ifdef SIP_RUN
2507 SIP_PYOBJECT __repr__();
2508 % MethodCode
2509 QString str = QStringLiteral( "<QgsGeometry.Error: %1>" ).arg( sipCpp->what() );
2510 sipRes = PyUnicode_FromString( str.toUtf8().data() );
2511 % End
2512#endif
2513
2514 // TODO c++20 - replace with = default
2515 bool operator==( const QgsGeometry::Error &other ) const
2516 {
2517 return other.mMessage == mMessage && other.mHasLocation == mHasLocation && other.mLocation == mLocation;
2518 }
2519
2520 private:
2521 QString mMessage;
2522 QgsPointXY mLocation;
2523 bool mHasLocation = false;
2524 };
2525
2534 void validateGeometry( QVector<QgsGeometry::Error> &errors SIP_OUT, Qgis::GeometryValidationEngine method = Qgis::GeometryValidationEngine::QgisInternal, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const;
2535
2545 void normalize();
2546
2555 static QgsGeometry unaryUnion( const QVector<QgsGeometry> &geometries, const QgsGeometryParameters &parameters = QgsGeometryParameters() );
2556
2565 static QgsGeometry polygonize( const QVector<QgsGeometry> &geometries );
2566
2574 void convertToStraightSegment( double tolerance = M_PI / 180., QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle );
2575
2582 bool requiresConversionToStraightSegments() const;
2583
2589 void mapToPixel( const QgsMapToPixel &mtp );
2590
2596 void draw( QPainter &p ) const;
2597
2608 bool vertexIdFromVertexNr( int number, QgsVertexId &id SIP_OUT ) const;
2609
2621 int vertexNrFromVertexId( QgsVertexId id ) const;
2622
2630 QString lastError() const SIP_HOLDGIL;
2631
2641 void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) SIP_SKIP;
2642
2657 void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) SIP_SKIP;
2658
2664 static QgsGeometry fromQPointF( QPointF point ) SIP_HOLDGIL;
2665
2673 static QgsGeometry fromQPolygonF( const QPolygonF &polygon );
2674
2682 Q_DECL_DEPRECATED static QgsPolylineXY createPolylineFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2683
2691 Q_DECL_DEPRECATED static QgsPolygonXY createPolygonFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2692
2693#ifndef SIP_RUN
2694
2704 static bool compare( const QgsPolylineXY &p1, const QgsPolylineXY &p2,
2705 double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2706
2716 static bool compare( const QgsPolygonXY &p1, const QgsPolygonXY &p2,
2717 double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2718
2729 static bool compare( const QgsMultiPolygonXY &p1, const QgsMultiPolygonXY &p2,
2730 double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2731#else
2732
2752 static bool compare( PyObject *obj1, PyObject *obj2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2753 % MethodCode
2754 {
2755 sipRes = false;
2756 int state0;
2757 int state1;
2758 int sipIsErr = 0;
2759
2760 if ( PyList_Check( a0 ) && PyList_Check( a1 ) &&
2761 PyList_GET_SIZE( a0 ) && PyList_GET_SIZE( a1 ) )
2762 {
2763 PyObject *o0 = PyList_GetItem( a0, 0 );
2764 PyObject *o1 = PyList_GetItem( a1, 0 );
2765 if ( o0 && o1 )
2766 {
2767 // compare polyline - polyline
2768 if ( sipCanConvertToType( o0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2769 sipCanConvertToType( o1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2770 sipCanConvertToType( a0, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2771 sipCanConvertToType( a1, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2772 {
2773 QgsPolylineXY *p0;
2774 QgsPolylineXY *p1;
2775 p0 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a0, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2776 p1 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a1, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2777 if ( sipIsErr )
2778 {
2779 sipReleaseType( p0, sipType_QVector_0100QgsPointXY, state0 );
2780 sipReleaseType( p1, sipType_QVector_0100QgsPointXY, state1 );
2781 }
2782 else
2783 {
2784 sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2785 }
2786 }
2787 else if ( PyList_Check( o0 ) && PyList_Check( o1 ) &&
2788 PyList_GET_SIZE( o0 ) && PyList_GET_SIZE( o1 ) )
2789 {
2790 PyObject *oo0 = PyList_GetItem( o0, 0 );
2791 PyObject *oo1 = PyList_GetItem( o1, 0 );
2792 if ( oo0 && oo1 )
2793 {
2794 // compare polygon - polygon
2795 if ( sipCanConvertToType( oo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2796 sipCanConvertToType( oo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2797 sipCanConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2798 sipCanConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2799 {
2800 QgsPolygonXY *p0;
2801 QgsPolygonXY *p1;
2802 p0 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2803 p1 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2804 if ( sipIsErr )
2805 {
2806 sipReleaseType( p0, sipType_QVector_0600QVector_0100QgsPointXY, state0 );
2807 sipReleaseType( p1, sipType_QVector_0600QVector_0100QgsPointXY, state1 );
2808 }
2809 else
2810 {
2811 sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2812 }
2813 }
2814 else if ( PyList_Check( oo0 ) && PyList_Check( oo1 ) &&
2815 PyList_GET_SIZE( oo0 ) && PyList_GET_SIZE( oo1 ) )
2816 {
2817 PyObject *ooo0 = PyList_GetItem( oo0, 0 );
2818 PyObject *ooo1 = PyList_GetItem( oo1, 0 );
2819 if ( ooo0 && ooo1 )
2820 {
2821 // compare multipolygon - multipolygon
2822 if ( sipCanConvertToType( ooo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2823 sipCanConvertToType( ooo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2824 sipCanConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2825 sipCanConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2826 {
2829 p0 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2830 p1 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2831 if ( sipIsErr )
2832 {
2833 sipReleaseType( p0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state0 );
2834 sipReleaseType( p1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state1 );
2835 }
2836 else
2837 {
2838 sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2839 }
2840 }
2841 }
2842 }
2843 }
2844 }
2845 }
2846 }
2847 }
2848 % End
2849#endif
2850
2867 QgsGeometry smooth( unsigned int iterations = 1, double offset = 0.25,
2868 double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
2869
2907 static QgsGeometryEngine *createGeometryEngine( const QgsAbstractGeometry *geometry ) SIP_FACTORY;
2908
2914 static void convertPointList( const QVector<QgsPointXY> &input, QgsPointSequence &output );
2915
2921 static void convertPointList( const QgsPointSequence &input, QVector<QgsPointXY> &output );
2922
2924 operator QVariant() const
2925 {
2926 return QVariant::fromValue( *this );
2927 }
2928
2929 private:
2930
2931 QgsGeometryPrivate *d; //implicitly shared data pointer
2932
2934 mutable QString mLastError;
2935
2940 void detach();
2941
2946 void reset( std::unique_ptr< QgsAbstractGeometry > newGeometry );
2947
2948 static void convertPolygon( const QgsPolygon &input, QgsPolygonXY &output );
2949
2951 QgsGeometry convertToPoint( bool destMultipart ) const;
2953 QgsGeometry convertToLine( bool destMultipart ) const;
2955 QgsGeometry convertToPolygon( bool destMultipart ) const;
2956
2968 std::unique_ptr< QgsLineString > smoothLine( const QgsLineString &line, unsigned int iterations = 1, double offset = 0.25,
2969 double minimumDistance = -1, double maxAngle = 180.0 ) const;
2970
2982 std::unique_ptr< QgsPolygon > smoothPolygon( const QgsPolygon &polygon, unsigned int iterations = 1, double offset = 0.25,
2983 double minimumDistance = -1, double maxAngle = 180.0 ) const;
2984
2985
2987
2988}; // class QgsGeometry
2989
2991
2992
2993CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsGeometry &geometry );
2995CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsGeometry &geometry );
2996
2997#endif
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:72
GeometryOperationResult
Success or failure of a geometry operation.
Definition: qgis.h:955
GeometryValidationEngine
Available engines for validating geometries.
Definition: qgis.h:996
TransformDirection
Flags for raster layer temporal capabilities.
Definition: qgis.h:1320
The part_iterator class provides STL-style iterator for const references to geometry parts.
The part_iterator class provides STL-style iterator for geometry parts.
The vertex_iterator class provides STL-style iterator for vertices.
Abstract base class for all geometries.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
@ MaximumAngle
Maximum angle between generating radii (lines from arc center to output vertices)
virtual const QgsAbstractGeometry * simplifiedTypeRef() const SIP_HOLDGIL
Returns a reference to the simplest lossless representation of this geometry, e.g.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
A const WKB pointer.
Definition: qgswkbptr.h:138
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Abstract base class for curved geometry type.
Definition: qgscurve.h:36
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Java-style iterator for const traversal of parts of a geometry.
A geometry engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
Encapsulates parameters under which a geometry operation is performed.
Definition: qgsgeometry.h:111
double gridSize() const
Returns the grid size which will be used to snap vertices of a geometry.
Definition: qgsgeometry.h:124
void setGridSize(double size)
Sets the grid size which will be used to snap vertices of a geometry.
Definition: qgsgeometry.h:136
Java-style iterator for traversal of parts of a geometry.
A geometry error.
Definition: qgsgeometry.h:2476
Error(const QString &m)
Definition: qgsgeometry.h:2482
Error(const QString &m, const QgsPointXY &p)
Definition: qgsgeometry.h:2486
bool operator==(const QgsGeometry::Error &other) const
Definition: qgsgeometry.h:2515
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
QVector< QgsPointXY > randomPointsInPolygon(int count, const std::function< bool(const QgsPointXY &) > &acceptPoint, unsigned long seed=0, QgsFeedback *feedback=nullptr, int maxTriesPerPoint=0) const
Returns a list of count random points generated inside a (multi)polygon geometry (if acceptPoint is s...
QVector< QgsPointXY > randomPointsInPolygon(int count, unsigned long seed=0, QgsFeedback *feedback=nullptr) const
Returns a list of count random points generated inside a (multi)polygon geometry.
static bool compare(const QgsPolylineXY &p1, const QgsPolylineXY &p2, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compares two polylines for equality within a specified tolerance.
This class offers geometry processing methods.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:45
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
Custom exception class which is raised when an operation is not supported.
Definition: qgsexception.h:118
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Polygon geometry type.
Definition: qgspolygon.h:34
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Represents a vector layer which manages a vector based data sets.
Java-style iterator for traversal of vertices of a geometry.
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:862
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
static QString displayString(Type type) SIP_HOLDGIL
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:732
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
CORE_EXPORT QgsMeshVertex centroid(const QgsMeshFace &face, const QVector< QgsMeshVertex > &vertices)
Returns the centroid of the face.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:74
#define str(x)
Definition: qgis.cpp:37
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:3020
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:227
#define SIP_IN
Definition: qgis_sip.h:63
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:151
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_HOLDGIL
Definition: qgis_sip.h:166
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_THROW(name,...)
Definition: qgis_sip.h:198
QVector< QgsPoint > QgsPointSequence
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item.
Definition: qgsgeometry.h:76
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry. QGIS version compatibility is not guaranteed.
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsGeometry &geometry)
Writes the geometry to stream out. QGIS version compatibility is not guaranteed.
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:86
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:82
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:63
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:93
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.
Definition: qgsgeometry.h:72
int precision
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:31