QGIS API Documentation 3.29.0-Master (19d7edcfed)
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
2394 bool convertToCurvedMultiType();
2395
2405 bool convertToSingleType();
2406
2416 bool convertGeometryCollectionToSubclass( QgsWkbTypes::GeometryType geomType );
2417
2429 int avoidIntersections( const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
2430 const QHash<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures SIP_PYARGREMOVE = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
2431
2453 QgsGeometry makeValid( Qgis::MakeValidMethod method = Qgis::MakeValidMethod::Linework, bool keepCollapsed = false ) const SIP_THROW( QgsNotSupportedException );
2454
2467 QgsGeometry forceRHR() const;
2468
2477 QgsGeometry forcePolygonClockwise() const;
2478
2487 QgsGeometry forcePolygonCounterClockwise() const;
2488
2493 class CORE_EXPORT Error
2494 {
2495 public:
2497 : mMessage( QStringLiteral( "none" ) )
2498 {}
2499
2500 explicit Error( const QString &m )
2501 : mMessage( m )
2502 {}
2503
2504 Error( const QString &m, const QgsPointXY &p )
2505 : mMessage( m )
2506 , mLocation( p )
2507 , mHasLocation( true ) {}
2508
2512 QString what() const;
2513
2517 QgsPointXY where() const;
2518
2522 bool hasWhere() const;
2523
2524#ifdef SIP_RUN
2525 SIP_PYOBJECT __repr__();
2526 % MethodCode
2527 QString str = QStringLiteral( "<QgsGeometry.Error: %1>" ).arg( sipCpp->what() );
2528 sipRes = PyUnicode_FromString( str.toUtf8().data() );
2529 % End
2530#endif
2531
2532 // TODO c++20 - replace with = default
2533 bool operator==( const QgsGeometry::Error &other ) const
2534 {
2535 return other.mMessage == mMessage && other.mHasLocation == mHasLocation && other.mLocation == mLocation;
2536 }
2537
2538 private:
2539 QString mMessage;
2540 QgsPointXY mLocation;
2541 bool mHasLocation = false;
2542 };
2543
2552 void validateGeometry( QVector<QgsGeometry::Error> &errors SIP_OUT, Qgis::GeometryValidationEngine method = Qgis::GeometryValidationEngine::QgisInternal, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const;
2553
2563 void normalize();
2564
2573 static QgsGeometry unaryUnion( const QVector<QgsGeometry> &geometries, const QgsGeometryParameters &parameters = QgsGeometryParameters() );
2574
2583 static QgsGeometry polygonize( const QVector<QgsGeometry> &geometries );
2584
2592 void convertToStraightSegment( double tolerance = M_PI / 180., QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle );
2593
2600 bool requiresConversionToStraightSegments() const;
2601
2607 void mapToPixel( const QgsMapToPixel &mtp );
2608
2614 void draw( QPainter &p ) const;
2615
2626 bool vertexIdFromVertexNr( int number, QgsVertexId &id SIP_OUT ) const;
2627
2639 int vertexNrFromVertexId( QgsVertexId id ) const;
2640
2648 QString lastError() const SIP_HOLDGIL;
2649
2659 void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) SIP_SKIP;
2660
2675 void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) SIP_SKIP;
2676
2682 static QgsGeometry fromQPointF( QPointF point ) SIP_HOLDGIL;
2683
2691 static QgsGeometry fromQPolygonF( const QPolygonF &polygon );
2692
2700 Q_DECL_DEPRECATED static QgsPolylineXY createPolylineFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2701
2709 Q_DECL_DEPRECATED static QgsPolygonXY createPolygonFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2710
2711#ifndef SIP_RUN
2712
2722 static bool compare( const QgsPolylineXY &p1, const QgsPolylineXY &p2,
2723 double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2724
2734 static bool compare( const QgsPolygonXY &p1, const QgsPolygonXY &p2,
2735 double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2736
2747 static bool compare( const QgsMultiPolygonXY &p1, const QgsMultiPolygonXY &p2,
2748 double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2749#else
2750
2770 static bool compare( PyObject *obj1, PyObject *obj2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2771 % MethodCode
2772 {
2773 sipRes = false;
2774 int state0;
2775 int state1;
2776 int sipIsErr = 0;
2777
2778 if ( PyList_Check( a0 ) && PyList_Check( a1 ) &&
2779 PyList_GET_SIZE( a0 ) && PyList_GET_SIZE( a1 ) )
2780 {
2781 PyObject *o0 = PyList_GetItem( a0, 0 );
2782 PyObject *o1 = PyList_GetItem( a1, 0 );
2783 if ( o0 && o1 )
2784 {
2785 // compare polyline - polyline
2786 if ( sipCanConvertToType( o0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2787 sipCanConvertToType( o1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2788 sipCanConvertToType( a0, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2789 sipCanConvertToType( a1, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2790 {
2791 QgsPolylineXY *p0;
2792 QgsPolylineXY *p1;
2793 p0 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a0, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2794 p1 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a1, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2795 if ( sipIsErr )
2796 {
2797 sipReleaseType( p0, sipType_QVector_0100QgsPointXY, state0 );
2798 sipReleaseType( p1, sipType_QVector_0100QgsPointXY, state1 );
2799 }
2800 else
2801 {
2802 sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2803 }
2804 }
2805 else if ( PyList_Check( o0 ) && PyList_Check( o1 ) &&
2806 PyList_GET_SIZE( o0 ) && PyList_GET_SIZE( o1 ) )
2807 {
2808 PyObject *oo0 = PyList_GetItem( o0, 0 );
2809 PyObject *oo1 = PyList_GetItem( o1, 0 );
2810 if ( oo0 && oo1 )
2811 {
2812 // compare polygon - polygon
2813 if ( sipCanConvertToType( oo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2814 sipCanConvertToType( oo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2815 sipCanConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2816 sipCanConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2817 {
2818 QgsPolygonXY *p0;
2819 QgsPolygonXY *p1;
2820 p0 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2821 p1 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2822 if ( sipIsErr )
2823 {
2824 sipReleaseType( p0, sipType_QVector_0600QVector_0100QgsPointXY, state0 );
2825 sipReleaseType( p1, sipType_QVector_0600QVector_0100QgsPointXY, state1 );
2826 }
2827 else
2828 {
2829 sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2830 }
2831 }
2832 else if ( PyList_Check( oo0 ) && PyList_Check( oo1 ) &&
2833 PyList_GET_SIZE( oo0 ) && PyList_GET_SIZE( oo1 ) )
2834 {
2835 PyObject *ooo0 = PyList_GetItem( oo0, 0 );
2836 PyObject *ooo1 = PyList_GetItem( oo1, 0 );
2837 if ( ooo0 && ooo1 )
2838 {
2839 // compare multipolygon - multipolygon
2840 if ( sipCanConvertToType( ooo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2841 sipCanConvertToType( ooo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2842 sipCanConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2843 sipCanConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2844 {
2847 p0 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2848 p1 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2849 if ( sipIsErr )
2850 {
2851 sipReleaseType( p0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state0 );
2852 sipReleaseType( p1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state1 );
2853 }
2854 else
2855 {
2856 sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2857 }
2858 }
2859 }
2860 }
2861 }
2862 }
2863 }
2864 }
2865 }
2866 % End
2867#endif
2868
2885 QgsGeometry smooth( unsigned int iterations = 1, double offset = 0.25,
2886 double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
2887
2925 static QgsGeometryEngine *createGeometryEngine( const QgsAbstractGeometry *geometry ) SIP_FACTORY;
2926
2932 static void convertPointList( const QVector<QgsPointXY> &input, QgsPointSequence &output );
2933
2939 static void convertPointList( const QgsPointSequence &input, QVector<QgsPointXY> &output );
2940
2942 operator QVariant() const
2943 {
2944 return QVariant::fromValue( *this );
2945 }
2946
2947 private:
2948
2949 QgsGeometryPrivate *d; //implicitly shared data pointer
2950
2952 mutable QString mLastError;
2953
2958 void detach();
2959
2964 void reset( std::unique_ptr< QgsAbstractGeometry > newGeometry );
2965
2966 static void convertPolygon( const QgsPolygon &input, QgsPolygonXY &output );
2967
2969 QgsGeometry convertToPoint( bool destMultipart ) const;
2971 QgsGeometry convertToLine( bool destMultipart ) const;
2973 QgsGeometry convertToPolygon( bool destMultipart ) const;
2974
2986 std::unique_ptr< QgsLineString > smoothLine( const QgsLineString &line, unsigned int iterations = 1, double offset = 0.25,
2987 double minimumDistance = -1, double maxAngle = 180.0 ) const;
2988
3000 std::unique_ptr< QgsPolygon > smoothPolygon( const QgsPolygon &polygon, unsigned int iterations = 1, double offset = 0.25,
3001 double minimumDistance = -1, double maxAngle = 180.0 ) const;
3002
3003
3005
3006}; // class QgsGeometry
3007
3009
3010
3011CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsGeometry &geometry );
3013CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsGeometry &geometry );
3014
3015#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:1127
GeometryValidationEngine
Available engines for validating geometries.
Definition: qgis.h:1168
TransformDirection
Flags for raster layer temporal capabilities.
Definition: qgis.h:1492
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:2494
Error(const QString &m)
Definition: qgsgeometry.h:2500
Error(const QString &m, const QgsPointXY &p)
Definition: qgsgeometry.h:2504
bool operator==(const QgsGeometry::Error &other) const
Definition: qgsgeometry.h:2533
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:3620
#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