QGIS API Documentation  3.17.0-Master (df2c9ff931)
qgsgeometry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometry.h - Geometry (stored as Open Geospatial Consortium WKB)
3  -------------------------------------------------------------------
4 Date : 02 May 2005
5 Copyright : (C) 2005 by Brendan Morley
6 email : 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 
39 #ifndef SIP_RUN
40 #include "json_fwd.hpp"
41 using namespace nlohmann;
42 #endif
43 
44 class QgsGeometryEngine;
45 class QgsVectorLayer;
46 class QgsMapToPixel;
47 class QPainter;
48 class QgsPolygon;
49 class QgsLineString;
50 class QgsCurve;
52 
62 typedef QVector<QgsPointXY> QgsPolylineXY;
63 
72 
74 #ifndef SIP_RUN
75 typedef QVector<QgsPolylineXY> QgsPolygonXY;
76 #else
77 typedef QVector<QVector<QgsPointXY>> QgsPolygonXY;
78 #endif
79 
81 typedef QVector<QgsPointXY> QgsMultiPointXY;
82 
84 #ifndef SIP_RUN
85 typedef QVector<QgsPolylineXY> QgsMultiPolylineXY;
86 #else
87 typedef QVector<QVector<QgsPointXY>> QgsMultiPolylineXY;
88 #endif
89 
91 #ifndef SIP_RUN
92 typedef QVector<QgsPolygonXY> QgsMultiPolygonXY;
93 #else
94 typedef QVector<QVector<QVector<QgsPointXY>>> QgsMultiPolygonXY;
95 #endif
96 
97 class QgsRectangle;
98 
99 class QgsConstWkbPtr;
100 
101 struct QgsGeometryPrivate;
102 
123 class CORE_EXPORT QgsGeometry
124 {
125  Q_GADGET
126  Q_PROPERTY( bool isNull READ isNull )
127  Q_PROPERTY( QgsWkbTypes::GeometryType type READ type )
128 
129  public:
130 
136  {
137  Success = 0,
138  NothingHappened = 1000,
145  /* Add part issues */
148  /* Add ring issues*/
153  /* Split features */
155  };
156  Q_ENUM( OperationResult )
157 
158 
160 
162  QgsGeometry( const QgsGeometry & );
163 
168  QgsGeometry &operator=( QgsGeometry const &rhs ) SIP_SKIP;
169 
175  explicit QgsGeometry( QgsAbstractGeometry *geom SIP_TRANSFER );
176 
182  explicit QgsGeometry( std::unique_ptr< QgsAbstractGeometry > geom ) SIP_SKIP;
183 
184  virtual ~QgsGeometry();
185 
197  const QgsAbstractGeometry *constGet() const SIP_HOLDGIL;
198 
211  QgsAbstractGeometry *get();
212 
225  void set( QgsAbstractGeometry *geometry SIP_TRANSFER ) SIP_DEPRECATED;
226 
234  bool isNull() const SIP_HOLDGIL;
235 
237  static QgsGeometry fromWkt( const QString &wkt );
239  static QgsGeometry fromPointXY( const QgsPointXY &point ) SIP_HOLDGIL;
241  static QgsGeometry fromMultiPointXY( const QgsMultiPointXY &multipoint );
242 
254  static QgsGeometry fromPolylineXY( const QgsPolylineXY &polyline );
255 
265  static QgsGeometry fromPolyline( const QgsPolyline &polyline );
266 
268  static QgsGeometry fromMultiPolylineXY( const QgsMultiPolylineXY &multiline );
270  static QgsGeometry fromPolygonXY( const QgsPolygonXY &polygon );
272  static QgsGeometry fromMultiPolygonXY( const QgsMultiPolygonXY &multipoly );
274  static QgsGeometry fromRect( const QgsRectangle &rect ) SIP_HOLDGIL;
276  static QgsGeometry collectGeometry( const QVector<QgsGeometry> &geometries );
277 
293  static QgsGeometry createWedgeBuffer( const QgsPoint &center, double azimuth, double angularWidth,
294  double outerRadius, double innerRadius = 0 );
295 
301  void fromWkb( unsigned char *wkb, int length ) SIP_SKIP;
302 
307  void fromWkb( const QByteArray &wkb );
308 
313  QgsWkbTypes::Type wkbType() const SIP_HOLDGIL;
314 
319  QgsWkbTypes::GeometryType type() const SIP_HOLDGIL;
320 
327  bool isEmpty() const;
328 
330  bool isMultipart() const SIP_HOLDGIL;
331 
346  bool equals( const QgsGeometry &geometry ) const;
347 
364  bool isGeosEqual( const QgsGeometry & ) const;
365 
368  {
369  FlagAllowSelfTouchingHoles = 1 << 0,
370  };
371  Q_DECLARE_FLAGS( ValidityFlags, ValidityFlag )
372 
373 
380  bool isGeosValid( QgsGeometry::ValidityFlags flags = QgsGeometry::ValidityFlags() ) const;
381 
390  bool isSimple() const;
391 
404  double area() const;
405 
418  double length() const;
419 
427  double distance( const QgsGeometry &geom ) const;
428 
429 #ifndef SIP_RUN
430 
431  // TODO QGIS 4: consider renaming vertices_begin, vertices_end, parts_begin, parts_end, etc
432  // to camelCase
433 
438  QgsAbstractGeometry::vertex_iterator vertices_begin() const;
439 
444  QgsAbstractGeometry::vertex_iterator vertices_end() const;
445 #endif
446 
470  QgsVertexIterator vertices() const;
471 
472 #ifndef SIP_RUN
473 
483 
493 
502  QgsAbstractGeometry::const_part_iterator const_parts_begin() const;
503 
512  QgsAbstractGeometry::const_part_iterator const_parts_end() const;
513 #endif
514 
552  QgsGeometryPartIterator parts();
553 
586  QgsGeometryConstPartIterator constParts() const;
587 
605  double hausdorffDistance( const QgsGeometry &geom ) const;
606 
625  double hausdorffDistanceDensify( const QgsGeometry &geom, double densifyFraction ) const;
626 
639  QgsPointXY closestVertex( const QgsPointXY &point, int &closestVertexIndex SIP_OUT, int &previousVertexIndex SIP_OUT, int &nextVertexIndex SIP_OUT, double &sqrDist SIP_OUT ) const;
640 
649  double distanceToVertex( int vertex ) const;
650 
658  double angleAtVertex( int vertex ) const;
659 
672  void adjacentVertices( int atVertex, int &beforeVertex SIP_OUT, int &afterVertex SIP_OUT ) const;
673 
686  bool insertVertex( double x, double y, int beforeVertex );
687 
700  bool insertVertex( const QgsPoint &point, int beforeVertex );
701 
709  bool moveVertex( double x, double y, int atVertex );
710 
718  bool moveVertex( const QgsPoint &p, int atVertex );
719 
731  bool deleteVertex( int atVertex );
732 
738  QgsPoint vertexAt( int atVertex ) const;
739 
745  double sqrDistToVertexAt( QgsPointXY &point SIP_IN, int atVertex ) const;
746 
752  QgsGeometry nearestPoint( const QgsGeometry &other ) const;
753 
764  QgsGeometry shortestLine( const QgsGeometry &other ) const;
765 
772  double closestVertexWithContext( const QgsPointXY &point, int &atVertex SIP_OUT ) const;
773 
785  double closestSegmentWithContext( const QgsPointXY &point, QgsPointXY &minDistPoint SIP_OUT, int &nextVertexIndex SIP_OUT, int *leftOrRightOfSegment SIP_OUT = nullptr, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
786 
792  OperationResult addRing( const QVector<QgsPointXY> &ring );
793 
799  OperationResult addRing( QgsCurve *ring SIP_TRANSFER );
800 
807  OperationResult addPart( const QVector<QgsPointXY> &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry ) SIP_PYNAME( addPointsXY );
808 
816 
824 
830  OperationResult addPart( const QgsGeometry &newPart ) SIP_PYNAME( addPartGeometry );
831 
838  QgsGeometry removeInteriorRings( double minimumAllowedArea = -1 ) const;
839 
844  OperationResult translate( double dx, double dy, double dz = 0.0, double dm = 0.0 );
845 
861 
870  OperationResult transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );
871 
878  OperationResult rotate( double rotation, const QgsPointXY &center );
879 
890  Q_DECL_DEPRECATED OperationResult splitGeometry( const QVector<QgsPointXY> &splitLine, QVector<QgsGeometry> &newGeometries SIP_OUT, bool topological, QVector<QgsPointXY> &topologyTestPoints SIP_OUT, bool splitFeature = true ) SIP_DEPRECATED;
891 
913  OperationResult splitGeometry( const QgsPointSequence &splitLine, QVector<QgsGeometry> &newGeometries SIP_OUT, bool topological, QgsPointSequence &topologyTestPoints SIP_OUT, bool splitFeature = true, bool skipIntersectionTest SIP_PYARGREMOVE = false );
914 
926  OperationResult splitGeometry( const QgsCurve *curve, QVector<QgsGeometry> &newGeometries SIP_OUT, bool preserveCircular, bool topological, QgsPointSequence &topologyTestPoints SIP_OUT, bool splitFeature = true );
927 
932  OperationResult reshapeGeometry( const QgsLineString &reshapeLineString );
933 
939  int makeDifferenceInPlace( const QgsGeometry &other ) SIP_SKIP;
940 
948  QgsGeometry makeDifference( const QgsGeometry &other ) const;
949 
954  QgsRectangle boundingBox() const;
955 
967  QgsGeometry orientedMinimumBoundingBox( double &area SIP_OUT, double &angle SIP_OUT, double &width SIP_OUT, double &height SIP_OUT ) const;
968 
978  QgsGeometry orientedMinimumBoundingBox() const SIP_SKIP;
979 
988  QgsGeometry minimalEnclosingCircle( QgsPointXY &center SIP_OUT, double &radius SIP_OUT, unsigned int segments = 36 ) const;
989 
995  QgsGeometry minimalEnclosingCircle( unsigned int segments = 36 ) const SIP_SKIP;
996 
1005  QgsGeometry orthogonalize( double tolerance = 1.0E-8, int maxIterations = 1000, double angleThreshold = 15.0 ) const;
1006 
1019  QgsGeometry snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const;
1020 
1041  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false );
1042 
1052  bool intersects( const QgsRectangle &rectangle ) const;
1053 
1063  bool intersects( const QgsGeometry &geometry ) const;
1064 
1074  bool boundingBoxIntersects( const QgsRectangle &rectangle ) const;
1075 
1085  bool boundingBoxIntersects( const QgsGeometry &geometry ) const;
1086 
1090  bool contains( const QgsPointXY *p ) const;
1091 
1096  bool contains( const QgsGeometry &geometry ) const;
1097 
1102  bool disjoint( const QgsGeometry &geometry ) const;
1103 
1108  bool touches( const QgsGeometry &geometry ) const;
1109 
1114  bool overlaps( const QgsGeometry &geometry ) const;
1115 
1120  bool within( const QgsGeometry &geometry ) const;
1121 
1122 
1127  bool crosses( const QgsGeometry &geometry ) const;
1128 
1131  {
1132  SideLeft = 0,
1134  };
1135  Q_ENUM( BufferSide )
1136 
1137 
1139  {
1140  CapRound = 1,
1143  };
1144  Q_ENUM( EndCapStyle )
1145 
1146 
1148  {
1149  JoinStyleRound = 1,
1152  };
1153  Q_ENUM( JoinStyle )
1154 
1155 
1162  QgsGeometry buffer( double distance, int segments ) const;
1163 
1176  QgsGeometry buffer( double distance, int segments, EndCapStyle endCapStyle, JoinStyle joinStyle, double miterLimit ) const;
1177 
1186  QgsGeometry offsetCurve( double distance, int segments, JoinStyle joinStyle, double miterLimit ) const;
1187 
1203  QgsGeometry singleSidedBuffer( double distance, int segments, BufferSide side,
1204  JoinStyle joinStyle = JoinStyleRound,
1205  double miterLimit = 2.0 ) const;
1206 
1224  QgsGeometry taperedBuffer( double startWidth, double endWidth, int segments ) const;
1225 
1240  QgsGeometry variableWidthBufferByM( int segments ) const;
1241 
1248  QgsGeometry extendLine( double startDistance, double endDistance ) const;
1249 
1251  QgsGeometry simplify( double tolerance ) const;
1252 
1262  QgsGeometry densifyByCount( int extraNodesPerSegment ) const;
1263 
1278  QgsGeometry densifyByDistance( double distance ) const;
1279 
1295  QgsGeometry convertToCurves( double distanceTolerance = 1e-8, double angleTolerance = 1e-8 ) const;
1296 
1310  QgsGeometry centroid() const;
1311 
1325  QgsGeometry pointOnSurface() const;
1326 
1339  QgsGeometry poleOfInaccessibility( double precision, double *distanceToBoundary SIP_OUT = nullptr ) const;
1340 
1349  QgsGeometry convexHull() const;
1350 
1366  QgsGeometry voronoiDiagram( const QgsGeometry &extent = QgsGeometry(), double tolerance = 0.0, bool edgesOnly = false ) const;
1367 
1377  QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false ) const;
1378 
1398  QgsGeometry subdivide( int maxNodes = 256 ) const;
1399 
1415  QgsGeometry interpolate( double distance ) const;
1416 
1428  double lineLocatePoint( const QgsGeometry &point ) const;
1429 
1439  double interpolateAngle( double distance ) const;
1440 
1449  QgsGeometry intersection( const QgsGeometry &geometry ) const;
1450 
1458  QgsGeometry clipped( const QgsRectangle &rectangle );
1459 
1471  QgsGeometry combine( const QgsGeometry &geometry ) const;
1472 
1481  QgsGeometry mergeLines() const;
1482 
1491  QgsGeometry difference( const QgsGeometry &geometry ) const;
1492 
1501  QgsGeometry symDifference( const QgsGeometry &geometry ) const;
1502 
1504  QgsGeometry extrude( double x, double y );
1505 
1506 #ifndef SIP_RUN
1507 
1529  QVector< QgsPointXY > randomPointsInPolygon( int count, const std::function< bool( const QgsPointXY & ) > &acceptPoint, unsigned long seed = 0, QgsFeedback *feedback = nullptr, int maxTriesPerPoint = 0 ) const;
1530 
1544  QVector< QgsPointXY > randomPointsInPolygon( int count, unsigned long seed = 0, QgsFeedback *feedback = nullptr ) const;
1546 #else
1547 
1560  SIP_PYOBJECT randomPointsInPolygon( int count, unsigned long seed = 0 ) const SIP_TYPEHINT( QgsPolylineXY );
1561  % MethodCode
1562  const QgsWkbTypes::GeometryType type = sipCpp->type();
1563  if ( sipCpp->isNull() )
1564  {
1565  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Cannot generate points inside a null geometry." ).toUtf8().constData() );
1566  sipIsErr = 1;
1567  }
1568  else if ( type != QgsWkbTypes::PolygonGeometry )
1569  {
1570  PyErr_SetString( PyExc_TypeError, QStringLiteral( "Cannot generate points inside a %1 geometry. Only Polygon types are permitted." ).arg( QgsWkbTypes::displayString( sipCpp->wkbType() ) ).toUtf8().constData() );
1571  sipIsErr = 1;
1572  }
1573  else
1574  {
1575  const sipTypeDef *qvector_type = sipFindType( "QVector<QgsPointXY>" );
1576  sipRes = sipConvertFromNewType( new QVector< QgsPointXY >( sipCpp->randomPointsInPolygon( a0, a1 ) ), qvector_type, Py_None );
1577  }
1578  % End
1579 
1580 
1581 #endif
1582 
1591  int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const;
1592 
1600  QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const;
1601 
1607  QString asWkt( int precision = 17 ) const;
1608 
1609 #ifdef SIP_RUN
1610  SIP_PYOBJECT __repr__();
1611  % MethodCode
1612  QString str;
1613  if ( sipCpp->isNull() )
1614  str = QStringLiteral( "<QgsGeometry: null>" );
1615  else
1616  {
1617  QString wkt = sipCpp->asWkt();
1618  if ( wkt.length() > 1000 )
1619  wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
1620  str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
1621  }
1622  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1623  % End
1624 #endif
1625 
1629  QString asJson( int precision = 17 ) const;
1630 
1636  virtual json asJsonObject( int precision = 17 ) const SIP_SKIP;
1637 
1660  QVector< QgsGeometry > coerceToType( QgsWkbTypes::Type type ) const;
1661 
1674  QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const;
1675 
1676  /* Accessor functions for getting geometry data */
1677 
1678 #ifndef SIP_RUN
1679 
1687  QgsPointXY asPoint() const;
1688 #else
1689 
1699  SIP_PYOBJECT asPoint() const SIP_TYPEHINT( QgsPointXY );
1700  % MethodCode
1701  const QgsWkbTypes::Type type = sipCpp->wkbType();
1702  if ( sipCpp->isNull() )
1703  {
1704  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a point." ).toUtf8().constData() );
1705  sipIsErr = 1;
1706  }
1707  else if ( QgsWkbTypes::flatType( type ) != QgsWkbTypes::Point )
1708  {
1709  PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a point. Only Point types are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
1710  sipIsErr = 1;
1711  }
1712  else
1713  {
1714  sipRes = sipConvertFromNewType( new QgsPointXY( sipCpp->asPoint() ), sipType_QgsPointXY, Py_None );
1715  }
1716  % End
1717 #endif
1718 
1719 #ifndef SIP_RUN
1720 
1729  QgsPolylineXY asPolyline() const;
1730 #else
1731 
1742  SIP_PYOBJECT asPolyline() const SIP_TYPEHINT( QgsPolylineXY );
1743  % MethodCode
1744  const QgsWkbTypes::Type type = sipCpp->wkbType();
1745  if ( sipCpp->isNull() )
1746  {
1747  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a polyline." ).toUtf8().constData() );
1748  sipIsErr = 1;
1749  }
1751  {
1752  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() );
1753  sipIsErr = 1;
1754  }
1755  else
1756  {
1757  const sipTypeDef *qvector_type = sipFindType( "QVector< QgsPointXY >" );
1758  sipRes = sipConvertFromNewType( new QgsPolylineXY( sipCpp->asPolyline() ), qvector_type, Py_None );
1759  }
1760  % End
1761 #endif
1762 
1763 #ifndef SIP_RUN
1764 
1773  QgsPolygonXY asPolygon() const;
1774 #else
1775 
1786  SIP_PYOBJECT asPolygon() const SIP_TYPEHINT( QgsPolygonXY );
1787  % MethodCode
1788  const QgsWkbTypes::Type type = sipCpp->wkbType();
1789  if ( sipCpp->isNull() )
1790  {
1791  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a polygon." ).toUtf8().constData() );
1792  sipIsErr = 1;
1793  }
1795  {
1796  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() );
1797  sipIsErr = 1;
1798  }
1799  else
1800  {
1801  const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QgsPointXY>>" );
1802  sipRes = sipConvertFromNewType( new QgsPolygonXY( sipCpp->asPolygon() ), qvector_type, Py_None );
1803  }
1804  % End
1805 #endif
1806 
1807 #ifndef SIP_RUN
1808 
1816  QgsMultiPointXY asMultiPoint() const;
1817 #else
1818 
1828  SIP_PYOBJECT asMultiPoint() const SIP_TYPEHINT( QgsMultiPointXY );
1829  % MethodCode
1830  const QgsWkbTypes::Type type = sipCpp->wkbType();
1831  if ( sipCpp->isNull() )
1832  {
1833  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multipoint." ).toUtf8().constData() );
1834  sipIsErr = 1;
1835  }
1837  {
1838  PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a multipoint. Only multipoint types are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
1839  sipIsErr = 1;
1840  }
1841  else
1842  {
1843  const sipTypeDef *qvector_type = sipFindType( "QVector< QgsPointXY >" );
1844  sipRes = sipConvertFromNewType( new QgsPolylineXY( sipCpp->asMultiPoint() ), qvector_type, Py_None );
1845  }
1846  % End
1847 #endif
1848 
1849 #ifndef SIP_RUN
1850 
1859  QgsMultiPolylineXY asMultiPolyline() const;
1860 #else
1861 
1872  SIP_PYOBJECT asMultiPolyline() const SIP_TYPEHINT( QgsMultiPolylineXY );
1873  % MethodCode
1874  const QgsWkbTypes::Type type = sipCpp->wkbType();
1875  if ( sipCpp->isNull() )
1876  {
1877  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multilinestring." ).toUtf8().constData() );
1878  sipIsErr = 1;
1879  }
1881  {
1882  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() );
1883  sipIsErr = 1;
1884  }
1885  else
1886  {
1887  const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QgsPointXY>>" );
1888  sipRes = sipConvertFromNewType( new QgsMultiPolylineXY( sipCpp->asMultiPolyline() ), qvector_type, Py_None );
1889  }
1890  % End
1891 #endif
1892 
1893 #ifndef SIP_RUN
1894 
1903  QgsMultiPolygonXY asMultiPolygon() const;
1904 #else
1905 
1916  SIP_PYOBJECT asMultiPolygon() const SIP_TYPEHINT( QgsMultiPolygonXY );
1917  % MethodCode
1918  const QgsWkbTypes::Type type = sipCpp->wkbType();
1919  if ( sipCpp->isNull() )
1920  {
1921  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multipolygon." ).toUtf8().constData() );
1922  sipIsErr = 1;
1923  }
1925  {
1926  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() );
1927  sipIsErr = 1;
1928  }
1929  else
1930  {
1931  const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QVector<QgsPointXY>>>" );
1932  sipRes = sipConvertFromNewType( new QgsMultiPolygonXY( sipCpp->asMultiPolygon() ), qvector_type, Py_None );
1933  }
1934  % End
1935 #endif
1936 
1941  QVector<QgsGeometry> asGeometryCollection() const;
1942 
1948  QPointF asQPointF() const SIP_HOLDGIL;
1949 
1962  QPolygonF asQPolygonF() const SIP_HOLDGIL;
1963 
1970  bool deleteRing( int ringNum, int partNum = 0 );
1971 
1977  bool deletePart( int partNum );
1978 
1987  bool convertToMultiType();
1988 
1998  bool convertToSingleType();
1999 
2009  bool convertGeometryCollectionToSubclass( QgsWkbTypes::GeometryType geomType );
2010 
2021  int avoidIntersections( const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
2022  const QHash<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures SIP_PYARGREMOVE = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
2023 
2042  QgsGeometry makeValid() const;
2043 
2051  QgsGeometry forceRHR() const;
2052 
2056  class CORE_EXPORT Error
2057  {
2058  public:
2060  : mMessage( QStringLiteral( "none" ) )
2061  {}
2062 
2063  explicit Error( const QString &m )
2064  : mMessage( m )
2065  {}
2066 
2067  Error( const QString &m, const QgsPointXY &p )
2068  : mMessage( m )
2069  , mLocation( p )
2070  , mHasLocation( true ) {}
2071 
2075  QString what() const;
2076 
2080  QgsPointXY where() const;
2081 
2085  bool hasWhere() const;
2086 
2087 #ifdef SIP_RUN
2088  SIP_PYOBJECT __repr__();
2089  % MethodCode
2090  QString str = QStringLiteral( "<QgsGeometry.Error: %1>" ).arg( sipCpp->what() );
2091  sipRes = PyUnicode_FromString( str.toUtf8().data() );
2092  % End
2093 #endif
2094 
2095  bool operator==( const QgsGeometry::Error &other ) const
2096  {
2097  return other.mMessage == mMessage && other.mHasLocation == mHasLocation && other.mLocation == mLocation;
2098  }
2099 
2100  private:
2101  QString mMessage;
2102  QgsPointXY mLocation;
2103  bool mHasLocation = false;
2104  };
2105 
2111  {
2114  };
2115 
2124  void validateGeometry( QVector<QgsGeometry::Error> &errors SIP_OUT, ValidationMethod method = ValidatorQgisInternal, QgsGeometry::ValidityFlags flags = QgsGeometry::ValidityFlags() ) const;
2125 
2131  static QgsGeometry unaryUnion( const QVector<QgsGeometry> &geometries );
2132 
2141  static QgsGeometry polygonize( const QVector<QgsGeometry> &geometries );
2142 
2150  void convertToStraightSegment( double tolerance = M_PI / 180., QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle );
2151 
2158  bool requiresConversionToStraightSegments() const;
2159 
2165  void mapToPixel( const QgsMapToPixel &mtp );
2166 
2172  void draw( QPainter &p ) const;
2173 
2184  bool vertexIdFromVertexNr( int number, QgsVertexId &id SIP_OUT ) const;
2185 
2197  int vertexNrFromVertexId( QgsVertexId id ) const;
2198 
2206  QString lastError() const SIP_HOLDGIL;
2207 
2217  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) SIP_SKIP;
2218 
2233  void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) SIP_SKIP;
2234 
2240  static QgsGeometry fromQPointF( QPointF point ) SIP_HOLDGIL;
2241 
2249  static QgsGeometry fromQPolygonF( const QPolygonF &polygon );
2250 
2258  Q_DECL_DEPRECATED static QgsPolylineXY createPolylineFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2259 
2267  Q_DECL_DEPRECATED static QgsPolygonXY createPolygonFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2268 
2269 #ifndef SIP_RUN
2270 
2280  static bool compare( const QgsPolylineXY &p1, const QgsPolylineXY &p2,
2281  double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2282 
2292  static bool compare( const QgsPolygonXY &p1, const QgsPolygonXY &p2,
2293  double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2294 
2305  static bool compare( const QgsMultiPolygonXY &p1, const QgsMultiPolygonXY &p2,
2306  double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2307 #else
2308 
2328  static bool compare( PyObject *obj1, PyObject *obj2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2329  % MethodCode
2330  {
2331  sipRes = false;
2332  int state0;
2333  int state1;
2334  int sipIsErr = 0;
2335 
2336  if ( PyList_Check( a0 ) && PyList_Check( a1 ) &&
2337  PyList_GET_SIZE( a0 ) && PyList_GET_SIZE( a1 ) )
2338  {
2339  PyObject *o0 = PyList_GetItem( a0, 0 );
2340  PyObject *o1 = PyList_GetItem( a1, 0 );
2341  if ( o0 && o1 )
2342  {
2343  // compare polyline - polyline
2344  if ( sipCanConvertToType( o0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2345  sipCanConvertToType( o1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2346  sipCanConvertToType( a0, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2347  sipCanConvertToType( a1, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2348  {
2349  QgsPolylineXY *p0;
2350  QgsPolylineXY *p1;
2351  p0 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a0, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2352  p1 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a1, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2353  if ( sipIsErr )
2354  {
2355  sipReleaseType( p0, sipType_QVector_0100QgsPointXY, state0 );
2356  sipReleaseType( p1, sipType_QVector_0100QgsPointXY, state1 );
2357  }
2358  else
2359  {
2360  sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2361  }
2362  }
2363  else if ( PyList_Check( o0 ) && PyList_Check( o1 ) &&
2364  PyList_GET_SIZE( o0 ) && PyList_GET_SIZE( o1 ) )
2365  {
2366  PyObject *oo0 = PyList_GetItem( o0, 0 );
2367  PyObject *oo1 = PyList_GetItem( o1, 0 );
2368  if ( oo0 && oo1 )
2369  {
2370  // compare polygon - polygon
2371  if ( sipCanConvertToType( oo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2372  sipCanConvertToType( oo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2373  sipCanConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2374  sipCanConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2375  {
2376  QgsPolygonXY *p0;
2377  QgsPolygonXY *p1;
2378  p0 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2379  p1 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2380  if ( sipIsErr )
2381  {
2382  sipReleaseType( p0, sipType_QVector_0600QVector_0100QgsPointXY, state0 );
2383  sipReleaseType( p1, sipType_QVector_0600QVector_0100QgsPointXY, state1 );
2384  }
2385  else
2386  {
2387  sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2388  }
2389  }
2390  else if ( PyList_Check( oo0 ) && PyList_Check( oo1 ) &&
2391  PyList_GET_SIZE( oo0 ) && PyList_GET_SIZE( oo1 ) )
2392  {
2393  PyObject *ooo0 = PyList_GetItem( oo0, 0 );
2394  PyObject *ooo1 = PyList_GetItem( oo1, 0 );
2395  if ( ooo0 && ooo1 )
2396  {
2397  // compare multipolygon - multipolygon
2398  if ( sipCanConvertToType( ooo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2399  sipCanConvertToType( ooo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2400  sipCanConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2401  sipCanConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2402  {
2403  QgsMultiPolygonXY *p0;
2404  QgsMultiPolygonXY *p1;
2405  p0 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2406  p1 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2407  if ( sipIsErr )
2408  {
2409  sipReleaseType( p0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state0 );
2410  sipReleaseType( p1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state1 );
2411  }
2412  else
2413  {
2414  sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2415  }
2416  }
2417  }
2418  }
2419  }
2420  }
2421  }
2422  }
2423  }
2424  % End
2425 #endif
2426 
2443  QgsGeometry smooth( unsigned int iterations = 1, double offset = 0.25,
2444  double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
2445 
2449  static QgsGeometryEngine *createGeometryEngine( const QgsAbstractGeometry *geometry ) SIP_FACTORY;
2450 
2456  static void convertPointList( const QVector<QgsPointXY> &input, QgsPointSequence &output );
2457 
2463  static void convertPointList( const QgsPointSequence &input, QVector<QgsPointXY> &output );
2464 
2466  operator QVariant() const
2467  {
2468  return QVariant::fromValue( *this );
2469  }
2470 
2471  private:
2472 
2473  QgsGeometryPrivate *d; //implicitly shared data pointer
2474 
2476  mutable QString mLastError;
2477 
2482  void detach();
2483 
2488  void reset( std::unique_ptr< QgsAbstractGeometry > newGeometry );
2489 
2490  static void convertToPolyline( const QgsPointSequence &input, QgsPolylineXY &output );
2491  static void convertPolygon( const QgsPolygon &input, QgsPolygonXY &output );
2492 
2494  QgsGeometry convertToPoint( bool destMultipart ) const;
2496  QgsGeometry convertToLine( bool destMultipart ) const;
2498  QgsGeometry convertToPolygon( bool destMultipart ) const;
2499 
2511  std::unique_ptr< QgsLineString > smoothLine( const QgsLineString &line, unsigned int iterations = 1, double offset = 0.25,
2512  double minimumDistance = -1, double maxAngle = 180.0 ) const;
2513 
2525  std::unique_ptr< QgsPolygon > smoothPolygon( const QgsPolygon &polygon, unsigned int iterations = 1, double offset = 0.25,
2526  double minimumDistance = -1, double maxAngle = 180.0 ) const;
2527 
2528 
2530 
2531 }; // class QgsGeometry
2532 
2534 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsGeometry::ValidityFlags )
2535 
2536 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsGeometry &geometry );
2539 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsGeometry &geometry );
2540 
2541 #endif
Geometry engine misses a method implemented or an error occurred in the geometry engine.
Definition: qgsgeometry.h:143
int precision
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.
Definition: qgsgeometry.h:71
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Java-style iterator for traversal of parts of a geometry.
The input ring doesn&#39;t have any existing ring to fit into.
Definition: qgsgeometry.h:152
Java-style iterator for traversal of vertices of a geometry.
The source geometry is not multi.
Definition: qgsgeometry.h:147
Maximum angle between generating radii (lines from arc center to output vertices) ...
Use GEOS validation methods.
Definition: qgsgeometry.h:2113
Error(const QString &m)
Definition: qgsgeometry.h:2063
bool operator==(const QgsGeometry::Error &other) const
Definition: qgsgeometry.h:2095
Java-style iterator for const traversal of parts of a geometry.
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:41
A class to represent a 2D point.
Definition: qgspointxy.h:43
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
Definition: qgsgeometry.h:75
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features ...
Definition: qgsfeatureid.h:28
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:702
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:123
Use mitered joins.
Definition: qgsgeometry.h:1150
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:81
The part_iterator class provides STL-style iterator for const references to geometry parts...
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
EndCapStyle
End cap styles for buffers.
Definition: qgsgeometry.h:1138
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
OperationResult
Success or failure of a geometry operation.
Definition: qgsgeometry.h:135
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
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:92
Error(const QString &m, const QgsPointXY &p)
Definition: qgsgeometry.h:2067
#define SIP_IN
Definition: qgis_sip.h:63
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:85
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:43
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsGeometry &geometry)
Writes the geometry to stream out. QGIS version compatibility is not guaranteed.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:37
Cannot edit layer.
Definition: qgsgeometry.h:144
The selected geometry cannot be found.
Definition: qgsgeometry.h:146
No features were selected.
Definition: qgsgeometry.h:141
More than one features were selected.
Definition: qgsgeometry.h:142
Utility class for identifying a unique vertex within a geometry.
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:757
The input ring crosses existing rings (it is not disjoint)
Definition: qgsgeometry.h:151
#define SIP_SKIP
Definition: qgis_sip.h:126
The part_iterator class provides STL-style iterator for geometry parts.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
Use internal QgsGeometryValidator method.
Definition: qgsgeometry.h:2112
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
The input ring is not closed.
Definition: qgsgeometry.h:149
Square cap (extends past start/end of line by buffer distance)
Definition: qgsgeometry.h:1142
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
#define SIP_FACTORY
Definition: qgis_sip.h:76
Abstract base class for all geometries.
The vertex_iterator class provides STL-style iterator for vertices.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
This class offers geometry processing methods.
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry. QGIS version compatibility is not guaranteed.
BufferSide
Side of line to buffer.
Definition: qgsgeometry.h:1130
QVector< QgsPoint > QgsPointSequence
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:51
The base geometry on which the operation is done is invalid or empty.
Definition: qgsgeometry.h:139
The input geometry (ring, part, split line, etc.) has not the correct geometry type.
Definition: qgsgeometry.h:140
#define SIP_OUT
Definition: qgis_sip.h:58
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
ValidationMethod
Available methods for validating geometries.
Definition: qgsgeometry.h:2110
Buffer to right of line.
Definition: qgsgeometry.h:1133
Class for doing transforms between two map coordinate systems.
ValidityFlag
Validity check flags.
Definition: qgsgeometry.h:367
The input ring is not valid.
Definition: qgsgeometry.h:150
#define SIP_THROW(name)
Definition: qgis_sip.h:189
Contains geometry relation and modification algorithms.
Use beveled joins.
Definition: qgsgeometry.h:1151
Transform from source to destination CRS.
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:832
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
Polygon geometry type.
Definition: qgspolygon.h:33
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:1147
Flat cap (in line with start/end of line)
Definition: qgsgeometry.h:1141
Represents a vector layer which manages a vector based data sets.
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 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:938
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.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81