QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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;
51 class QgsFeedback;
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 
815  OperationResult addPart( const QgsPointSequence &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry ) SIP_PYNAME( addPoints );
816 
823  OperationResult addPart( QgsAbstractGeometry *part SIP_TRANSFER, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
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 
860  OperationResult transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction = QgsCoordinateTransform::ForwardTransform, bool transformZ = false ) SIP_THROW( QgsCsException );
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 
914  OperationResult splitGeometry( const QgsPointSequence &splitLine, QVector<QgsGeometry> &newGeometries SIP_OUT, bool topological, QgsPointSequence &topologyTestPoints SIP_OUT, bool splitFeature = true, bool skipIntersectionTest SIP_PYARGREMOVE = false );
915 
927  OperationResult splitGeometry( const QgsCurve *curve, QVector<QgsGeometry> &newGeometries SIP_OUT, bool preserveCircular, bool topological, QgsPointSequence &topologyTestPoints SIP_OUT, bool splitFeature = true );
928 
933  OperationResult reshapeGeometry( const QgsLineString &reshapeLineString );
934 
940  int makeDifferenceInPlace( const QgsGeometry &other ) SIP_SKIP;
941 
949  QgsGeometry makeDifference( const QgsGeometry &other ) const;
950 
955  QgsRectangle boundingBox() const;
956 
968  QgsGeometry orientedMinimumBoundingBox( double &area SIP_OUT, double &angle SIP_OUT, double &width SIP_OUT, double &height SIP_OUT ) const;
969 
979  QgsGeometry orientedMinimumBoundingBox() const SIP_SKIP;
980 
989  QgsGeometry minimalEnclosingCircle( QgsPointXY &center SIP_OUT, double &radius SIP_OUT, unsigned int segments = 36 ) const;
990 
996  QgsGeometry minimalEnclosingCircle( unsigned int segments = 36 ) const SIP_SKIP;
997 
1006  QgsGeometry orthogonalize( double tolerance = 1.0E-8, int maxIterations = 1000, double angleThreshold = 15.0 ) const;
1007 
1020  QgsGeometry snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const;
1021 
1042  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false );
1043 
1053  bool intersects( const QgsRectangle &rectangle ) const;
1054 
1064  bool intersects( const QgsGeometry &geometry ) const;
1065 
1075  bool boundingBoxIntersects( const QgsRectangle &rectangle ) const;
1076 
1086  bool boundingBoxIntersects( const QgsGeometry &geometry ) const;
1087 
1091  bool contains( const QgsPointXY *p ) const;
1092 
1097  bool contains( const QgsGeometry &geometry ) const;
1098 
1103  bool disjoint( const QgsGeometry &geometry ) const;
1104 
1109  bool touches( const QgsGeometry &geometry ) const;
1110 
1115  bool overlaps( const QgsGeometry &geometry ) const;
1116 
1121  bool within( const QgsGeometry &geometry ) const;
1122 
1123 
1128  bool crosses( const QgsGeometry &geometry ) const;
1129 
1132  {
1133  SideLeft = 0,
1135  };
1136  Q_ENUM( BufferSide )
1137 
1138 
1140  {
1141  CapRound = 1,
1144  };
1145  Q_ENUM( EndCapStyle )
1146 
1147 
1149  {
1150  JoinStyleRound = 1,
1153  };
1154  Q_ENUM( JoinStyle )
1155 
1156 
1163  QgsGeometry buffer( double distance, int segments ) const;
1164 
1177  QgsGeometry buffer( double distance, int segments, EndCapStyle endCapStyle, JoinStyle joinStyle, double miterLimit ) const;
1178 
1187  QgsGeometry offsetCurve( double distance, int segments, JoinStyle joinStyle, double miterLimit ) const;
1188 
1204  QgsGeometry singleSidedBuffer( double distance, int segments, BufferSide side,
1205  JoinStyle joinStyle = JoinStyleRound,
1206  double miterLimit = 2.0 ) const;
1207 
1225  QgsGeometry taperedBuffer( double startWidth, double endWidth, int segments ) const;
1226 
1241  QgsGeometry variableWidthBufferByM( int segments ) const;
1242 
1249  QgsGeometry extendLine( double startDistance, double endDistance ) const;
1250 
1252  QgsGeometry simplify( double tolerance ) const;
1253 
1263  QgsGeometry densifyByCount( int extraNodesPerSegment ) const;
1264 
1279  QgsGeometry densifyByDistance( double distance ) const;
1280 
1296  QgsGeometry convertToCurves( double distanceTolerance = 1e-8, double angleTolerance = 1e-8 ) const;
1297 
1311  QgsGeometry centroid() const;
1312 
1326  QgsGeometry pointOnSurface() const;
1327 
1340  QgsGeometry poleOfInaccessibility( double precision, double *distanceToBoundary SIP_OUT = nullptr ) const;
1341 
1350  QgsGeometry convexHull() const;
1351 
1367  QgsGeometry voronoiDiagram( const QgsGeometry &extent = QgsGeometry(), double tolerance = 0.0, bool edgesOnly = false ) const;
1368 
1378  QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false ) const;
1379 
1399  QgsGeometry subdivide( int maxNodes = 256 ) const;
1400 
1416  QgsGeometry interpolate( double distance ) const;
1417 
1429  double lineLocatePoint( const QgsGeometry &point ) const;
1430 
1440  double interpolateAngle( double distance ) const;
1441 
1450  QgsGeometry intersection( const QgsGeometry &geometry ) const;
1451 
1459  QgsGeometry clipped( const QgsRectangle &rectangle );
1460 
1472  QgsGeometry combine( const QgsGeometry &geometry ) const;
1473 
1482  QgsGeometry mergeLines() const;
1483 
1492  QgsGeometry difference( const QgsGeometry &geometry ) const;
1493 
1502  QgsGeometry symDifference( const QgsGeometry &geometry ) const;
1503 
1505  QgsGeometry extrude( double x, double y );
1506 
1507 #ifndef SIP_RUN
1508 
1530  QVector< QgsPointXY > randomPointsInPolygon( int count, const std::function< bool( const QgsPointXY & ) > &acceptPoint, unsigned long seed = 0, QgsFeedback *feedback = nullptr, int maxTriesPerPoint = 0 ) const;
1531 
1545  QVector< QgsPointXY > randomPointsInPolygon( int count, unsigned long seed = 0, QgsFeedback *feedback = nullptr ) const;
1547 #else
1548 
1561  SIP_PYOBJECT randomPointsInPolygon( int count, unsigned long seed = 0 ) const SIP_TYPEHINT( QgsPolylineXY );
1562  % MethodCode
1563  const QgsWkbTypes::GeometryType type = sipCpp->type();
1564  if ( sipCpp->isNull() )
1565  {
1566  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Cannot generate points inside a null geometry." ).toUtf8().constData() );
1567  sipIsErr = 1;
1568  }
1569  else if ( type != QgsWkbTypes::PolygonGeometry )
1570  {
1571  PyErr_SetString( PyExc_TypeError, QStringLiteral( "Cannot generate points inside a %1 geometry. Only Polygon types are permitted." ).arg( QgsWkbTypes::displayString( sipCpp->wkbType() ) ).toUtf8().constData() );
1572  sipIsErr = 1;
1573  }
1574  else
1575  {
1576  const sipTypeDef *qvector_type = sipFindType( "QVector<QgsPointXY>" );
1577  sipRes = sipConvertFromNewType( new QVector< QgsPointXY >( sipCpp->randomPointsInPolygon( a0, a1 ) ), qvector_type, Py_None );
1578  }
1579  % End
1580 
1581 
1582 #endif
1584 
1592  int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const;
1593 
1601  QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const;
1602 
1608  QString asWkt( int precision = 17 ) const;
1609 
1610 #ifdef SIP_RUN
1611  SIP_PYOBJECT __repr__();
1612  % MethodCode
1613  QString str;
1614  if ( sipCpp->isNull() )
1615  str = QStringLiteral( "<QgsGeometry: null>" );
1616  else
1617  {
1618  QString wkt = sipCpp->asWkt();
1619  if ( wkt.length() > 1000 )
1620  wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
1621  str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
1622  }
1623  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1624  % End
1625 #endif
1626 
1630  QString asJson( int precision = 17 ) const;
1631 
1637  virtual json asJsonObject( int precision = 17 ) const SIP_SKIP;
1638 
1661  QVector< QgsGeometry > coerceToType( QgsWkbTypes::Type type ) const;
1662 
1675  QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const;
1676 
1677  /* Accessor functions for getting geometry data */
1678 
1679 #ifndef SIP_RUN
1680 
1688  QgsPointXY asPoint() const;
1689 #else
1690 
1700  SIP_PYOBJECT asPoint() const SIP_TYPEHINT( QgsPointXY );
1701  % MethodCode
1702  const QgsWkbTypes::Type type = sipCpp->wkbType();
1703  if ( sipCpp->isNull() )
1704  {
1705  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a point." ).toUtf8().constData() );
1706  sipIsErr = 1;
1707  }
1708  else if ( QgsWkbTypes::flatType( type ) != QgsWkbTypes::Point )
1709  {
1710  PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a point. Only Point types are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
1711  sipIsErr = 1;
1712  }
1713  else
1714  {
1715  sipRes = sipConvertFromNewType( new QgsPointXY( sipCpp->asPoint() ), sipType_QgsPointXY, Py_None );
1716  }
1717  % End
1718 #endif
1719 
1720 #ifndef SIP_RUN
1721 
1730  QgsPolylineXY asPolyline() const;
1731 #else
1732 
1743  SIP_PYOBJECT asPolyline() const SIP_TYPEHINT( QgsPolylineXY );
1744  % MethodCode
1745  const QgsWkbTypes::Type type = sipCpp->wkbType();
1746  if ( sipCpp->isNull() )
1747  {
1748  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a polyline." ).toUtf8().constData() );
1749  sipIsErr = 1;
1750  }
1752  {
1753  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() );
1754  sipIsErr = 1;
1755  }
1756  else
1757  {
1758  const sipTypeDef *qvector_type = sipFindType( "QVector< QgsPointXY >" );
1759  sipRes = sipConvertFromNewType( new QgsPolylineXY( sipCpp->asPolyline() ), qvector_type, Py_None );
1760  }
1761  % End
1762 #endif
1763 
1764 #ifndef SIP_RUN
1765 
1774  QgsPolygonXY asPolygon() const;
1775 #else
1776 
1787  SIP_PYOBJECT asPolygon() const SIP_TYPEHINT( QgsPolygonXY );
1788  % MethodCode
1789  const QgsWkbTypes::Type type = sipCpp->wkbType();
1790  if ( sipCpp->isNull() )
1791  {
1792  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a polygon." ).toUtf8().constData() );
1793  sipIsErr = 1;
1794  }
1796  {
1797  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() );
1798  sipIsErr = 1;
1799  }
1800  else
1801  {
1802  const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QgsPointXY>>" );
1803  sipRes = sipConvertFromNewType( new QgsPolygonXY( sipCpp->asPolygon() ), qvector_type, Py_None );
1804  }
1805  % End
1806 #endif
1807 
1808 #ifndef SIP_RUN
1809 
1817  QgsMultiPointXY asMultiPoint() const;
1818 #else
1819 
1829  SIP_PYOBJECT asMultiPoint() const SIP_TYPEHINT( QgsMultiPointXY );
1830  % MethodCode
1831  const QgsWkbTypes::Type type = sipCpp->wkbType();
1832  if ( sipCpp->isNull() )
1833  {
1834  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multipoint." ).toUtf8().constData() );
1835  sipIsErr = 1;
1836  }
1838  {
1839  PyErr_SetString( PyExc_TypeError, QStringLiteral( "%1 geometry cannot be converted to a multipoint. Only multipoint types are permitted." ).arg( QgsWkbTypes::displayString( type ) ).toUtf8().constData() );
1840  sipIsErr = 1;
1841  }
1842  else
1843  {
1844  const sipTypeDef *qvector_type = sipFindType( "QVector< QgsPointXY >" );
1845  sipRes = sipConvertFromNewType( new QgsPolylineXY( sipCpp->asMultiPoint() ), qvector_type, Py_None );
1846  }
1847  % End
1848 #endif
1849 
1850 #ifndef SIP_RUN
1851 
1860  QgsMultiPolylineXY asMultiPolyline() const;
1861 #else
1862 
1873  SIP_PYOBJECT asMultiPolyline() const SIP_TYPEHINT( QgsMultiPolylineXY );
1874  % MethodCode
1875  const QgsWkbTypes::Type type = sipCpp->wkbType();
1876  if ( sipCpp->isNull() )
1877  {
1878  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multilinestring." ).toUtf8().constData() );
1879  sipIsErr = 1;
1880  }
1882  {
1883  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() );
1884  sipIsErr = 1;
1885  }
1886  else
1887  {
1888  const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QgsPointXY>>" );
1889  sipRes = sipConvertFromNewType( new QgsMultiPolylineXY( sipCpp->asMultiPolyline() ), qvector_type, Py_None );
1890  }
1891  % End
1892 #endif
1893 
1894 #ifndef SIP_RUN
1895 
1904  QgsMultiPolygonXY asMultiPolygon() const;
1905 #else
1906 
1917  SIP_PYOBJECT asMultiPolygon() const SIP_TYPEHINT( QgsMultiPolygonXY );
1918  % MethodCode
1919  const QgsWkbTypes::Type type = sipCpp->wkbType();
1920  if ( sipCpp->isNull() )
1921  {
1922  PyErr_SetString( PyExc_ValueError, QStringLiteral( "Null geometry cannot be converted to a multipolygon." ).toUtf8().constData() );
1923  sipIsErr = 1;
1924  }
1926  {
1927  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() );
1928  sipIsErr = 1;
1929  }
1930  else
1931  {
1932  const sipTypeDef *qvector_type = sipFindType( "QVector<QVector<QVector<QgsPointXY>>>" );
1933  sipRes = sipConvertFromNewType( new QgsMultiPolygonXY( sipCpp->asMultiPolygon() ), qvector_type, Py_None );
1934  }
1935  % End
1936 #endif
1937 
1942  QVector<QgsGeometry> asGeometryCollection() const;
1943 
1949  QPointF asQPointF() const SIP_HOLDGIL;
1950 
1963  QPolygonF asQPolygonF() const SIP_HOLDGIL;
1964 
1971  bool deleteRing( int ringNum, int partNum = 0 );
1972 
1978  bool deletePart( int partNum );
1979 
1988  bool convertToMultiType();
1989 
1999  bool convertToSingleType();
2000 
2010  bool convertGeometryCollectionToSubclass( QgsWkbTypes::GeometryType geomType );
2011 
2022  int avoidIntersections( const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
2023  const QHash<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures SIP_PYARGREMOVE = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
2024 
2043  QgsGeometry makeValid() const;
2044 
2052  QgsGeometry forceRHR() const;
2053 
2058  class CORE_EXPORT Error
2059  {
2060  public:
2062  : mMessage( QStringLiteral( "none" ) )
2063  {}
2064 
2065  explicit Error( const QString &m )
2066  : mMessage( m )
2067  {}
2068 
2069  Error( const QString &m, const QgsPointXY &p )
2070  : mMessage( m )
2071  , mLocation( p )
2072  , mHasLocation( true ) {}
2073 
2077  QString what() const;
2078 
2082  QgsPointXY where() const;
2083 
2087  bool hasWhere() const;
2088 
2089 #ifdef SIP_RUN
2090  SIP_PYOBJECT __repr__();
2091  % MethodCode
2092  QString str = QStringLiteral( "<QgsGeometry.Error: %1>" ).arg( sipCpp->what() );
2093  sipRes = PyUnicode_FromString( str.toUtf8().data() );
2094  % End
2095 #endif
2096 
2097  bool operator==( const QgsGeometry::Error &other ) const
2098  {
2099  return other.mMessage == mMessage && other.mHasLocation == mHasLocation && other.mLocation == mLocation;
2100  }
2101 
2102  private:
2103  QString mMessage;
2104  QgsPointXY mLocation;
2105  bool mHasLocation = false;
2106  };
2107 
2113  {
2116  };
2117 
2126  void validateGeometry( QVector<QgsGeometry::Error> &errors SIP_OUT, ValidationMethod method = ValidatorQgisInternal, QgsGeometry::ValidityFlags flags = QgsGeometry::ValidityFlags() ) const;
2127 
2133  static QgsGeometry unaryUnion( const QVector<QgsGeometry> &geometries );
2134 
2143  static QgsGeometry polygonize( const QVector<QgsGeometry> &geometries );
2144 
2152  void convertToStraightSegment( double tolerance = M_PI / 180., QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle );
2153 
2160  bool requiresConversionToStraightSegments() const;
2161 
2167  void mapToPixel( const QgsMapToPixel &mtp );
2168 
2174  void draw( QPainter &p ) const;
2175 
2186  bool vertexIdFromVertexNr( int number, QgsVertexId &id SIP_OUT ) const;
2187 
2199  int vertexNrFromVertexId( QgsVertexId id ) const;
2200 
2208  QString lastError() const SIP_HOLDGIL;
2209 
2219  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) SIP_SKIP;
2220 
2235  void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) SIP_SKIP;
2236 
2242  static QgsGeometry fromQPointF( QPointF point ) SIP_HOLDGIL;
2243 
2251  static QgsGeometry fromQPolygonF( const QPolygonF &polygon );
2252 
2260  Q_DECL_DEPRECATED static QgsPolylineXY createPolylineFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2261 
2269  Q_DECL_DEPRECATED static QgsPolygonXY createPolygonFromQPolygonF( const QPolygonF &polygon ) SIP_DEPRECATED;
2270 
2271 #ifndef SIP_RUN
2272 
2282  static bool compare( const QgsPolylineXY &p1, const QgsPolylineXY &p2,
2283  double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2284 
2294  static bool compare( const QgsPolygonXY &p1, const QgsPolygonXY &p2,
2295  double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2296 
2307  static bool compare( const QgsMultiPolygonXY &p1, const QgsMultiPolygonXY &p2,
2308  double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2309 #else
2310 
2330  static bool compare( PyObject *obj1, PyObject *obj2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
2331  % MethodCode
2332  {
2333  sipRes = false;
2334  int state0;
2335  int state1;
2336  int sipIsErr = 0;
2337 
2338  if ( PyList_Check( a0 ) && PyList_Check( a1 ) &&
2339  PyList_GET_SIZE( a0 ) && PyList_GET_SIZE( a1 ) )
2340  {
2341  PyObject *o0 = PyList_GetItem( a0, 0 );
2342  PyObject *o1 = PyList_GetItem( a1, 0 );
2343  if ( o0 && o1 )
2344  {
2345  // compare polyline - polyline
2346  if ( sipCanConvertToType( o0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2347  sipCanConvertToType( o1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2348  sipCanConvertToType( a0, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2349  sipCanConvertToType( a1, sipType_QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2350  {
2351  QgsPolylineXY *p0;
2352  QgsPolylineXY *p1;
2353  p0 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a0, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2354  p1 = reinterpret_cast<QgsPolylineXY *>( sipConvertToType( a1, sipType_QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2355  if ( sipIsErr )
2356  {
2357  sipReleaseType( p0, sipType_QVector_0100QgsPointXY, state0 );
2358  sipReleaseType( p1, sipType_QVector_0100QgsPointXY, state1 );
2359  }
2360  else
2361  {
2362  sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2363  }
2364  }
2365  else if ( PyList_Check( o0 ) && PyList_Check( o1 ) &&
2366  PyList_GET_SIZE( o0 ) && PyList_GET_SIZE( o1 ) )
2367  {
2368  PyObject *oo0 = PyList_GetItem( o0, 0 );
2369  PyObject *oo1 = PyList_GetItem( o1, 0 );
2370  if ( oo0 && oo1 )
2371  {
2372  // compare polygon - polygon
2373  if ( sipCanConvertToType( oo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2374  sipCanConvertToType( oo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2375  sipCanConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2376  sipCanConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2377  {
2378  QgsPolygonXY *p0;
2379  QgsPolygonXY *p1;
2380  p0 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2381  p1 = reinterpret_cast<QgsPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2382  if ( sipIsErr )
2383  {
2384  sipReleaseType( p0, sipType_QVector_0600QVector_0100QgsPointXY, state0 );
2385  sipReleaseType( p1, sipType_QVector_0600QVector_0100QgsPointXY, state1 );
2386  }
2387  else
2388  {
2389  sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2390  }
2391  }
2392  else if ( PyList_Check( oo0 ) && PyList_Check( oo1 ) &&
2393  PyList_GET_SIZE( oo0 ) && PyList_GET_SIZE( oo1 ) )
2394  {
2395  PyObject *ooo0 = PyList_GetItem( oo0, 0 );
2396  PyObject *ooo1 = PyList_GetItem( oo1, 0 );
2397  if ( ooo0 && ooo1 )
2398  {
2399  // compare multipolygon - multipolygon
2400  if ( sipCanConvertToType( ooo0, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2401  sipCanConvertToType( ooo1, sipType_QgsPointXY, SIP_NOT_NONE ) &&
2402  sipCanConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) &&
2403  sipCanConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, SIP_NOT_NONE ) )
2404  {
2405  QgsMultiPolygonXY *p0;
2406  QgsMultiPolygonXY *p1;
2407  p0 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
2408  p1 = reinterpret_cast<QgsMultiPolygonXY *>( sipConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
2409  if ( sipIsErr )
2410  {
2411  sipReleaseType( p0, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state0 );
2412  sipReleaseType( p1, sipType_QVector_0600QVector_0600QVector_0100QgsPointXY, state1 );
2413  }
2414  else
2415  {
2416  sipRes = QgsGeometry::compare( *p0, *p1, a2 );
2417  }
2418  }
2419  }
2420  }
2421  }
2422  }
2423  }
2424  }
2425  }
2426  % End
2427 #endif
2428 
2445  QgsGeometry smooth( unsigned int iterations = 1, double offset = 0.25,
2446  double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
2447 
2451  static QgsGeometryEngine *createGeometryEngine( const QgsAbstractGeometry *geometry ) SIP_FACTORY;
2452 
2458  static void convertPointList( const QVector<QgsPointXY> &input, QgsPointSequence &output );
2459 
2465  static void convertPointList( const QgsPointSequence &input, QVector<QgsPointXY> &output );
2466 
2468  operator QVariant() const
2469  {
2470  return QVariant::fromValue( *this );
2471  }
2472 
2473  private:
2474 
2475  QgsGeometryPrivate *d; //implicitly shared data pointer
2476 
2478  mutable QString mLastError;
2479 
2484  void detach();
2485 
2490  void reset( std::unique_ptr< QgsAbstractGeometry > newGeometry );
2491 
2492  static void convertToPolyline( const QgsPointSequence &input, QgsPolylineXY &output );
2493  static void convertPolygon( const QgsPolygon &input, QgsPolygonXY &output );
2494 
2496  QgsGeometry convertToPoint( bool destMultipart ) const;
2498  QgsGeometry convertToLine( bool destMultipart ) const;
2500  QgsGeometry convertToPolygon( bool destMultipart ) const;
2501 
2513  std::unique_ptr< QgsLineString > smoothLine( const QgsLineString &line, unsigned int iterations = 1, double offset = 0.25,
2514  double minimumDistance = -1, double maxAngle = 180.0 ) const;
2515 
2527  std::unique_ptr< QgsPolygon > smoothPolygon( const QgsPolygon &polygon, unsigned int iterations = 1, double offset = 0.25,
2528  double minimumDistance = -1, double maxAngle = 180.0 ) const;
2529 
2530 
2532 
2533 }; // class QgsGeometry
2534 
2536 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsGeometry::ValidityFlags )
2537 
2538 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsGeometry &geometry );
2541 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsGeometry &geometry );
2542 
2543 #endif
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)
A const WKB pointer.
Definition: qgswkbptr.h:130
Class for doing transforms between two map coordinate systems.
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
@ ForwardTransform
Transform from source to destination CRS.
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.
Contains geometry relation and modification algorithms.
Java-style iterator for traversal of parts of a geometry.
A geometry error.
Definition: qgsgeometry.h:2059
Error(const QString &m)
Definition: qgsgeometry.h:2065
Error(const QString &m, const QgsPointXY &p)
Definition: qgsgeometry.h:2069
bool operator==(const QgsGeometry::Error &other) const
Definition: qgsgeometry.h:2097
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
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...
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:1149
@ JoinStyleMiter
Use mitered joins.
Definition: qgsgeometry.h:1151
@ JoinStyleBevel
Use beveled joins.
Definition: qgsgeometry.h:1152
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.
BufferSide
Side of line to buffer.
Definition: qgsgeometry.h:1132
@ SideRight
Buffer to right of line.
Definition: qgsgeometry.h:1134
EndCapStyle
End cap styles for buffers.
Definition: qgsgeometry.h:1140
@ CapSquare
Square cap (extends past start/end of line by buffer distance)
Definition: qgsgeometry.h:1143
@ CapFlat
Flat cap (in line with start/end of line)
Definition: qgsgeometry.h:1142
OperationResult
Success or failure of a geometry operation.
Definition: qgsgeometry.h:136
@ InvalidBaseGeometry
The base geometry on which the operation is done is invalid or empty.
Definition: qgsgeometry.h:139
@ SplitCannotSplitPoint
Cannot split points.
Definition: qgsgeometry.h:154
@ AddRingNotInExistingFeature
The input ring doesn't have any existing ring to fit into.
Definition: qgsgeometry.h:152
@ AddRingNotClosed
The input ring is not closed.
Definition: qgsgeometry.h:149
@ AddPartSelectedGeometryNotFound
The selected geometry cannot be found.
Definition: qgsgeometry.h:146
@ GeometryEngineError
Geometry engine misses a method implemented or an error occurred in the geometry engine.
Definition: qgsgeometry.h:143
@ AddPartNotMultiGeometry
The source geometry is not multi.
Definition: qgsgeometry.h:147
@ InvalidInputGeometryType
The input geometry (ring, part, split line, etc.) has not the correct geometry type.
Definition: qgsgeometry.h:140
@ SelectionIsEmpty
No features were selected.
Definition: qgsgeometry.h:141
@ AddRingCrossesExistingRings
The input ring crosses existing rings (it is not disjoint)
Definition: qgsgeometry.h:151
@ SelectionIsGreaterThanOne
More than one features were selected.
Definition: qgsgeometry.h:142
@ LayerNotEditable
Cannot edit layer.
Definition: qgsgeometry.h:144
@ AddRingNotValid
The input ring is not valid.
Definition: qgsgeometry.h:150
ValidityFlag
Validity check flags.
Definition: qgsgeometry.h:368
ValidationMethod
Available methods for validating geometries.
Definition: qgsgeometry.h:2113
@ ValidatorQgisInternal
Use internal QgsGeometryValidator method.
Definition: qgsgeometry.h:2114
@ ValidatorGeos
Use GEOS validation methods.
Definition: qgsgeometry.h:2115
This class offers geometry processing methods.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
A class to represent a 2D point.
Definition: qgspointxy.h:44
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
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:938
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:832
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
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:702
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
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:757
#define SIP_THROW(name)
Definition: qgis_sip.h:189
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
#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:146
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
#define SIP_FACTORY
Definition: qgis_sip.h:76
QVector< QgsPoint > QgsPointSequence
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
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.
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item.
Definition: qgsgeometry.h:75
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:85
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry. QGIS version compatibility is not guaranteed.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:81
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:51
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsGeometry &geometry)
Writes the geometry to stream out. QGIS version compatibility is not guaranteed.
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:92
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.
Definition: qgsgeometry.h:71
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
int precision
Utility class for identifying a unique vertex within a geometry.