QGIS API Documentation  2.99.0-Master (90ae728)
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 
621  QgsGeometry centroid() const;
622 
630  QgsGeometry pointOnSurface() const;
631 
644  QgsGeometry poleOfInaccessibility( double precision, double* distanceToBoundary = nullptr ) const;
645 
647  QgsGeometry convexHull() const;
648 
664  QgsGeometry voronoiDiagram( const QgsGeometry& extent = QgsGeometry(), double tolerance = 0.0, bool edgesOnly = false ) const;
665 
675  QgsGeometry delaunayTriangulation( double tolerance = 0.0, bool edgesOnly = false ) const;
676 
682  QgsGeometry interpolate( double distance ) const;
683 
694  double lineLocatePoint( const QgsGeometry& point ) const;
695 
704  double interpolateAngle( double distance ) const;
705 
707  QgsGeometry intersection( const QgsGeometry& geometry ) const;
708 
713  QgsGeometry combine( const QgsGeometry& geometry ) const;
714 
722  QgsGeometry mergeLines() const;
723 
725  QgsGeometry difference( const QgsGeometry& geometry ) const;
726 
728  QgsGeometry symDifference( const QgsGeometry& geometry ) const;
729 
731  QgsGeometry extrude( double x, double y );
732 
736  QByteArray exportToWkb() const;
737 
742  QString exportToWkt( int precision = 17 ) const;
743 
750  QString exportToGeoJSON( int precision = 17 ) const;
751 
758  QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const;
759 
760  /* Accessor functions for getting geometry data */
761 
765  QgsPoint asPoint() const;
766 
770  QgsPolyline asPolyline() const;
771 
775  QgsPolygon asPolygon() const;
776 
779  QgsMultiPoint asMultiPoint() const;
780 
783  QgsMultiPolyline asMultiPolyline() const;
784 
787  QgsMultiPolygon asMultiPolygon() const;
788 
791  QList<QgsGeometry> asGeometryCollection() const;
792 
797  QPointF asQPointF() const;
798 
804  QPolygonF asQPolygonF() const;
805 
810  bool deleteRing( int ringNum, int partNum = 0 );
811 
815  bool deletePart( int partNum );
816 
825  bool convertToMultiType();
826 
836  bool convertToSingleType();
837 
847  int avoidIntersections( const QList<QgsVectorLayer*>& avoidIntersectionsLayers,
848  const QHash<QgsVectorLayer*, QSet<QgsFeatureId> >& ignoreFeatures = ( QHash<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
849 
862  QgsGeometry makeValid();
863 
866  class Error
867  {
868  QString message;
869  QgsPoint location;
870  bool hasLocation;
871  public:
872  Error() : message( QStringLiteral( "none" ) ), hasLocation( false ) {}
873  explicit Error( const QString& m ) : message( m ), hasLocation( false ) {}
874  Error( const QString& m, const QgsPoint& p ) : message( m ), location( p ), hasLocation( true ) {}
875 
876  QString what() { return message; }
877  QgsPoint where() { return location; }
878  bool hasWhere() { return hasLocation; }
879  };
880 
885  void validateGeometry( QList<Error> &errors );
886 
891  static QgsGeometry unaryUnion( const QList<QgsGeometry>& geometries );
892 
901  static QgsGeometry polygonize( const QList< QgsGeometry>& geometries );
902 
907  void convertToStraightSegment();
908 
914  bool requiresConversionToStraightSegments() const;
915 
920  void mapToPixel( const QgsMapToPixel& mtp );
921 
922  // not implemented for 2.10
923  /* Clips the geometry using the specified rectangle
924  * @param rect clip rectangle
925  * @note added in QGIS 2.10
926  */
927  // void clip( const QgsRectangle& rect );
928 
933  void draw( QPainter& p ) const;
934 
942  bool vertexIdFromVertexNr( int nr, QgsVertexId& id ) const;
943 
950  int vertexNrFromVertexId( QgsVertexId i ) const;
951 
956  static GEOSContextHandle_t getGEOSHandler();
957 
962  static QgsGeometry fromQPointF( QPointF point );
963 
970  static QgsGeometry fromQPolygonF( const QPolygonF& polygon );
971 
977  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
978 
984  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
985 
994  static bool compare( const QgsPolyline& p1, const QgsPolyline& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
995 
1004  static bool compare( const QgsPolygon& p1, const QgsPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
1005 
1015  static bool compare( const QgsMultiPolygon& p1, const QgsMultiPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
1016 
1028  QgsGeometry smooth( const unsigned int iterations = 1, const double offset = 0.25,
1029  double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
1030 
1033  static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometry* geometry );
1034 
1039  static void convertPointList( const QList<QgsPoint> &input, QgsPointSequence &output );
1040 
1045  static void convertPointList( const QgsPointSequence &input, QList<QgsPoint> &output );
1046 
1048  operator QVariant() const
1049  {
1050  return QVariant::fromValue( *this );
1051  }
1052 
1057  operator bool() const;
1058 
1059  private:
1060 
1061  QgsGeometryPrivate* d; //implicitly shared data pointer
1062 
1063  void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
1064 
1065  static void convertToPolyline( const QgsPointSequence &input, QgsPolyline& output );
1066  static void convertPolygon( const QgsPolygonV2& input, QgsPolygon& output );
1067 
1069  QgsGeometry convertToPoint( bool destMultipart ) const;
1071  QgsGeometry convertToLine( bool destMultipart ) const;
1073  QgsGeometry convertToPolygon( bool destMultipart ) const;
1074 
1085  QgsLineString* smoothLine( const QgsLineString & line, const unsigned int iterations = 1, const double offset = 0.25,
1086  double minimumDistance = -1, double maxAngle = 180.0 ) const;
1087 
1098  QgsPolygonV2* smoothPolygon( const QgsPolygonV2 &polygon, const unsigned int iterations = 1, const double offset = 0.25,
1099  double minimumDistance = -1, double maxAngle = 180.0 ) const;
1100 
1101 
1102 }; // class QgsGeometry
1103 
1105 
1106 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry );
1109 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry );
1110 
1111 #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:877
Error(const QString &m)
Definition: qgsgeometry.h:873
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:328
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:143
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:131
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:874