QGIS API Documentation  2.99.0-Master (314842d)
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 "qgis_core.h"
20 #include <QString>
21 #include <QVector>
22 #include <QDomDocument>
23 
24 #include "qgis.h"
25 
26 #include <geos_c.h>
27 #include <climits>
28 
29 #if defined(GEOS_VERSION_MAJOR) && (GEOS_VERSION_MAJOR<3)
30 #define GEOSGeometry struct GEOSGeom_t
31 #define GEOSCoordSequence struct GEOSCoordSeq_t
32 #endif
33 
34 #include "qgsabstractgeometry.h"
35 #include "qgspoint.h"
36 #include "qgspointv2.h"
37 
38 #include "qgsfeature.h"
39 #include <limits>
40 #include <QSet>
41 
42 class QgsGeometryEngine;
43 class QgsVectorLayer;
44 class QgsMapToPixel;
45 class QPainter;
46 class QgsPolygonV2;
48 
50 typedef QVector<QgsPoint> QgsPolyline;
51 
53 typedef QVector<QgsPolyline> QgsPolygon;
54 
56 typedef QVector<QgsPoint> QgsMultiPoint;
57 
59 typedef QVector<QgsPolyline> QgsMultiPolyline;
60 
62 typedef QVector<QgsPolygon> QgsMultiPolygon;
63 
64 class QgsRectangle;
65 
66 class QgsConstWkbPtr;
67 
68 struct QgsGeometryPrivate;
69 
79 class CORE_EXPORT QgsGeometry
80 {
81  public:
83  QgsGeometry();
84 
86  QgsGeometry( const QgsGeometry & );
87 
91  QgsGeometry &operator=( QgsGeometry const &rhs );
92 
97  explicit QgsGeometry( QgsAbstractGeometry *geom );
98 
99 
100  ~QgsGeometry();
101 
106  QgsAbstractGeometry *geometry() const;
107 
112  void setGeometry( QgsAbstractGeometry *geometry );
113 
120  bool isNull() const;
121 
123  static QgsGeometry fromWkt( const QString &wkt );
125  static QgsGeometry fromPoint( const QgsPoint &point );
127  static QgsGeometry fromMultiPoint( const QgsMultiPoint &multipoint );
129  static QgsGeometry fromPolyline( const QgsPolyline &polyline );
131  static QgsGeometry fromMultiPolyline( const QgsMultiPolyline &multiline );
133  static QgsGeometry fromPolygon( const QgsPolygon &polygon );
135  static QgsGeometry fromMultiPolygon( const QgsMultiPolygon &multipoly );
137  static QgsGeometry fromRect( const QgsRectangle &rect );
139  static QgsGeometry collectGeometry( const QList< QgsGeometry > &geometries );
140 
146  void fromGeos( GEOSGeometry *geos );
147 
153  void fromWkb( unsigned char *wkb, int length );
154 
159  void fromWkb( const QByteArray &wkb );
160 
166  GEOSGeometry *exportToGeos( double precision = 0 ) const;
167 
171  QgsWkbTypes::Type wkbType() const;
172 
176  QgsWkbTypes::GeometryType type() const;
177 
184  bool isEmpty() const;
185 
187  bool isMultipart() const;
188 
192  bool isGeosEqual( const QgsGeometry & ) const;
193 
197  bool isGeosValid() const;
198 
202  double area() const;
203 
207  double length() const;
208 
215  double distance( const QgsGeometry &geom ) const;
216 
229  //TODO QGIS 3.0 - rename beforeVertex to previousVertex, afterVertex to nextVertex
230  QgsPoint closestVertex( const QgsPoint &point, int &atVertex, int &beforeVertex, int &afterVertex, double &sqrDist ) const;
231 
238  double distanceToVertex( int vertex ) const;
239 
247  double angleAtVertex( int vertex ) const;
248 
261  void adjacentVertices( int atVertex, int &beforeVertex, int &afterVertex ) const;
262 
274  bool insertVertex( double x, double y, int beforeVertex );
275 
287  bool insertVertex( const QgsPointV2 &point, int beforeVertex );
288 
295  bool moveVertex( double x, double y, int atVertex );
296 
303  bool moveVertex( const QgsPointV2 &p, int atVertex );
304 
315  bool deleteVertex( int atVertex );
316 
322  QgsPoint vertexAt( int atVertex ) const;
323 
329  double sqrDistToVertexAt( QgsPoint &point, int atVertex ) const;
330 
335  QgsGeometry nearestPoint( const QgsGeometry &other ) const;
336 
341  QgsGeometry shortestLine( const QgsGeometry &other ) const;
342 
349  double closestVertexWithContext( const QgsPoint &point, int &atVertex ) const;
350 
361  double closestSegmentWithContext( const QgsPoint &point, QgsPoint &minDistPoint, int &afterVertex, double *leftOf = nullptr, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
362 
366  // TODO QGIS 3.0 returns an enum instead of a magic constant
367  int addRing( const QList<QgsPoint> &ring );
368 
372  // TODO QGIS 3.0 returns an enum instead of a magic constant
373  int addRing( QgsCurve *ring );
374 
381  // TODO QGIS 3.0 returns an enum instead of a magic constant
382  int addPart( const QList<QgsPoint> &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
383 
390  // TODO QGIS 3.0 returns an enum instead of a magic constant
391  int addPart( const QgsPointSequence &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
392 
399  // TODO QGIS 3.0 returns an enum instead of a magic constant
401 
408  // TODO QGIS 3.0 returns an enum instead of a magic constant
409  int addPart( GEOSGeometry *newPart );
410 
416  // TODO QGIS 3.0 returns an enum instead of a magic constant
417  int addPart( const QgsGeometry &newPart );
418 
425  QgsGeometry removeInteriorRings( double minimumAllowedArea = -1 ) const;
426 
429  int translate( double dx, double dy );
430 
433  int transform( const QgsCoordinateTransform &ct );
434 
438  int transform( const QTransform &ct );
439 
445  int rotate( double rotation, const QgsPoint &center );
446 
453  // TODO QGIS 3.0 returns an enum instead of a magic constant
454  int splitGeometry( const QList<QgsPoint> &splitLine,
455  QList<QgsGeometry> &newGeometries,
456  bool topological,
457  QList<QgsPoint> &topologyTestPoints );
458 
463  int reshapeGeometry( const QList<QgsPoint> &reshapeWithLine );
464 
469  int makeDifference( const QgsGeometry *other );
470 
477  QgsGeometry makeDifference( const QgsGeometry &other ) const;
478 
483  QgsRectangle boundingBox() const;
484 
492  QgsGeometry orientedMinimumBoundingBox( double &area, double &angle, double &width, double &height ) const;
493 
502  QgsGeometry orthogonalize( double tolerance = 1.0E-8, int maxIterations = 1000, double angleThreshold = 15.0 ) const;
503 
505  bool intersects( const QgsRectangle &r ) const;
506 
508  bool intersects( const QgsGeometry &geometry ) const;
509 
511  bool contains( const QgsPoint *p ) const;
512 
515  bool contains( const QgsGeometry &geometry ) const;
516 
519  bool disjoint( const QgsGeometry &geometry ) const;
520 
523  bool equals( const QgsGeometry &geometry ) const;
524 
527  bool touches( const QgsGeometry &geometry ) const;
528 
531  bool overlaps( const QgsGeometry &geometry ) const;
532 
535  bool within( const QgsGeometry &geometry ) const;
536 
539  bool crosses( const QgsGeometry &geometry ) const;
540 
543  {
544  SideLeft = 0,
546  };
547 
550  {
551  CapRound = 1,
554  };
555 
558  {
559  JoinStyleRound = 1,
562  };
563 
566  QgsGeometry buffer( double distance, int segments ) const;
567 
576  QgsGeometry buffer( double distance, int segments, EndCapStyle endCapStyle, JoinStyle joinStyle, double mitreLimit ) const;
577 
585  QgsGeometry offsetCurve( double distance, int segments, JoinStyle joinStyle, double mitreLimit ) const;
586 
599  QgsGeometry singleSidedBuffer( double distance, int segments, BufferSide side,
600  JoinStyle joinStyle = JoinStyleRound,
601  double mitreLimit = 2.0 ) const;
602 
609  QgsGeometry extendLine( double startDistance, double endDistance ) const;
610 
612  QgsGeometry simplify( double tolerance ) const;
613 
623  QgsGeometry densifyByCount( int extraNodesPerSegment ) const;
624 
639  QgsGeometry densifyByDistance( double distance ) const;
640 
648  QgsGeometry centroid() const;
649 
657  QgsGeometry pointOnSurface() const;
658 
671  QgsGeometry poleOfInaccessibility( double precision, double *distanceToBoundary = nullptr ) const;
672 
674  QgsGeometry convexHull() const;
675 
691  QgsGeometry voronoiDiagram( const QgsGeometry &extent = QgsGeometry(), double tolerance = 0.0, bool edgesOnly = false ) const;
692 
702  QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false ) const;
703 
709  QgsGeometry interpolate( double distance ) const;
710 
721  double lineLocatePoint( const QgsGeometry &point ) const;
722 
731  double interpolateAngle( double distance ) const;
732 
734  QgsGeometry intersection( const QgsGeometry &geometry ) const;
735 
740  QgsGeometry combine( const QgsGeometry &geometry ) const;
741 
749  QgsGeometry mergeLines() const;
750 
752  QgsGeometry difference( const QgsGeometry &geometry ) const;
753 
755  QgsGeometry symDifference( const QgsGeometry &geometry ) const;
756 
758  QgsGeometry extrude( double x, double y );
759 
763  QByteArray exportToWkb() const;
764 
769  QString exportToWkt( int precision = 17 ) const;
770 
777  QString exportToGeoJSON( int precision = 17 ) const;
778 
785  QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const;
786 
787  /* Accessor functions for getting geometry data */
788 
792  QgsPoint asPoint() const;
793 
797  QgsPolyline asPolyline() const;
798 
802  QgsPolygon asPolygon() const;
803 
806  QgsMultiPoint asMultiPoint() const;
807 
810  QgsMultiPolyline asMultiPolyline() const;
811 
814  QgsMultiPolygon asMultiPolygon() const;
815 
818  QList<QgsGeometry> asGeometryCollection() const;
819 
824  QPointF asQPointF() const;
825 
831  QPolygonF asQPolygonF() const;
832 
837  bool deleteRing( int ringNum, int partNum = 0 );
838 
842  bool deletePart( int partNum );
843 
852  bool convertToMultiType();
853 
863  bool convertToSingleType();
864 
874  int avoidIntersections( const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
875  const QHash<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
876 
889  QgsGeometry makeValid();
890 
893  class Error
894  {
895  QString message;
896  QgsPoint location;
897  bool hasLocation;
898  public:
899  Error() : message( QStringLiteral( "none" ) ), hasLocation( false ) {}
900  explicit Error( const QString &m ) : message( m ), hasLocation( false ) {}
901  Error( const QString &m, const QgsPoint &p ) : message( m ), location( p ), hasLocation( true ) {}
902 
903  QString what() { return message; }
904  QgsPoint where() { return location; }
905  bool hasWhere() { return hasLocation; }
906  };
907 
912  void validateGeometry( QList<Error> &errors );
913 
918  static QgsGeometry unaryUnion( const QList<QgsGeometry> &geometries );
919 
928  static QgsGeometry polygonize( const QList< QgsGeometry> &geometries );
929 
934  void convertToStraightSegment();
935 
941  bool requiresConversionToStraightSegments() const;
942 
947  void mapToPixel( const QgsMapToPixel &mtp );
948 
949  // not implemented for 2.10
950  /* Clips the geometry using the specified rectangle
951  * @param rect clip rectangle
952  * @note added in QGIS 2.10
953  */
954  // void clip( const QgsRectangle& rect );
955 
960  void draw( QPainter &p ) const;
961 
969  bool vertexIdFromVertexNr( int nr, QgsVertexId &id ) const;
970 
977  int vertexNrFromVertexId( QgsVertexId i ) const;
978 
983  static GEOSContextHandle_t getGEOSHandler();
984 
989  static QgsGeometry fromQPointF( QPointF point );
990 
997  static QgsGeometry fromQPolygonF( const QPolygonF &polygon );
998 
1004  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
1005 
1011  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
1012 
1021  static bool compare( const QgsPolyline &p1, const QgsPolyline &p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
1022 
1031  static bool compare( const QgsPolygon &p1, const QgsPolygon &p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
1032 
1042  static bool compare( const QgsMultiPolygon &p1, const QgsMultiPolygon &p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
1043 
1055  QgsGeometry smooth( const unsigned int iterations = 1, const double offset = 0.25,
1056  double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
1057 
1060  static QgsGeometryEngine *createGeometryEngine( const QgsAbstractGeometry *geometry );
1061 
1066  static void convertPointList( const QList<QgsPoint> &input, QgsPointSequence &output );
1067 
1072  static void convertPointList( const QgsPointSequence &input, QList<QgsPoint> &output );
1073 
1075  operator QVariant() const
1076  {
1077  return QVariant::fromValue( *this );
1078  }
1079 
1084  operator bool() const;
1085 
1086  private:
1087 
1088  QgsGeometryPrivate *d; //implicitly shared data pointer
1089 
1090  void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
1091 
1092  static void convertToPolyline( const QgsPointSequence &input, QgsPolyline &output );
1093  static void convertPolygon( const QgsPolygonV2 &input, QgsPolygon &output );
1094 
1096  QgsGeometry convertToPoint( bool destMultipart ) const;
1098  QgsGeometry convertToLine( bool destMultipart ) const;
1100  QgsGeometry convertToPolygon( bool destMultipart ) const;
1101 
1112  QgsLineString *smoothLine( const QgsLineString &line, const unsigned int iterations = 1, const double offset = 0.25,
1113  double minimumDistance = -1, double maxAngle = 180.0 ) const;
1114 
1125  QgsPolygonV2 *smoothPolygon( const QgsPolygonV2 &polygon, const unsigned int iterations = 1, const double offset = 0.25,
1126  double minimumDistance = -1, double maxAngle = 180.0 ) const;
1127 
1128 
1129 }; // class QgsGeometry
1130 
1132 
1133 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsGeometry &geometry );
1136 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsGeometry &geometry );
1137 
1138 #endif
double ANALYSIS_EXPORT leftOf(Point3D *thepoint, Point3D *p1, Point3D *p2)
Returns whether &#39;thepoint&#39; is left or right of the line from &#39;p1&#39; to &#39;p2&#39;. Negativ values mean left a...
Definition: MathUtils.cc:310
A rectangle specified with double values.
Definition: qgsrectangle.h:36
QgsPoint where()
Definition: qgsgeometry.h:904
Error(const QString &m)
Definition: qgsgeometry.h:900
QVector< QgsPoint > QgsPolyline
Polyline is represented as a vector of points.
Definition: qgsgeometry.h:47
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
EndCapStyle
End cap styles for buffers.
Definition: qgsgeometry.h:549
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:34
Polygon geometry type.
Definition: qgspolygon.h:30
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:65
Utility class for identifying a unique vertex within a geometry.
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:339
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:36
QVector< QgsPolygon > QgsMultiPolygon
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:62
QVector< QgsPoint > QgsMultiPoint
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:56
Square cap (extends past start/end of line by buffer distance)
Definition: qgsgeometry.h:553
Abstract base class for curved geometry type.
Definition: qgscurve.h:33
QVector< QgsPolyline > QgsPolygon
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
Definition: qgsgeometry.h:53
Abstract base class for all geometries.
A class to represent a point.
Definition: qgspoint.h:37
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry. QGIS version compatibility is not guaranteed.
QVector< QgsPolyline > QgsMultiPolyline
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:59
BufferSide
Side of line to buffer.
Definition: qgsgeometry.h:542
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:135
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:36
Buffer to right of line.
Definition: qgsgeometry.h:545
Class for doing transforms between two map coordinate systems.
Contains geometry relation and modification algorithms.
Use mitred joins.
Definition: qgsgeometry.h:560
Use beveled joins.
Definition: qgsgeometry.h:561
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:557
Flat cap (in line with start/end of line)
Definition: qgsgeometry.h:552
QList< QgsPointV2 > QgsPointSequence
Represents a vector layer which manages a vector based data sets.
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cc:857
bool isNull(const QVariant &v)
Error(const QString &m, const QgsPoint &p)
Definition: qgsgeometry.h:901