QGIS API Documentation  2.99.0-Master (c558d51)
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 <QString>
20 #include <QVector>
21 #include <QDomDocument>
22 
23 #include "qgis.h"
24 
25 #include <geos_c.h>
26 #include <climits>
27 
28 #if defined(GEOS_VERSION_MAJOR) && (GEOS_VERSION_MAJOR<3)
29 #define GEOSGeometry struct GEOSGeom_t
30 #define GEOSCoordSequence struct GEOSCoordSeq_t
31 #endif
32 
33 #include "qgsabstractgeometry.h"
34 #include "qgspoint.h"
35 #include "qgspointv2.h"
36 
37 #include "qgsfeature.h"
38 #include <limits>
39 #include <QSet>
40 
41 class QgsGeometryEngine;
42 class QgsVectorLayer;
43 class QgsMapToPixel;
44 class QPainter;
45 class QgsPolygonV2;
47 
49 typedef QVector<QgsPoint> QgsPolyline;
50 
52 typedef QVector<QgsPolyline> QgsPolygon;
53 
55 typedef QVector<QgsPoint> QgsMultiPoint;
56 
58 typedef QVector<QgsPolyline> QgsMultiPolyline;
59 
61 typedef QVector<QgsPolygon> QgsMultiPolygon;
62 
63 class QgsRectangle;
64 
65 class QgsConstWkbPtr;
66 
67 struct QgsGeometryPrivate;
68 
78 class CORE_EXPORT QgsGeometry
79 {
80  public:
82  QgsGeometry();
83 
85  QgsGeometry( const QgsGeometry & );
86 
90  QgsGeometry & operator=( QgsGeometry const & rhs );
91 
96  explicit QgsGeometry( QgsAbstractGeometry* geom );
97 
99  ~QgsGeometry();
100 
105  QgsAbstractGeometry* geometry() const;
106 
111  void setGeometry( QgsAbstractGeometry* geometry );
112 
118  bool isEmpty() const;
119 
121  static QgsGeometry fromWkt( const QString& wkt );
123  static QgsGeometry fromPoint( const QgsPoint& point );
125  static QgsGeometry fromMultiPoint( const QgsMultiPoint& multipoint );
127  static QgsGeometry fromPolyline( const QgsPolyline& polyline );
129  static QgsGeometry fromMultiPolyline( const QgsMultiPolyline& multiline );
131  static QgsGeometry fromPolygon( const QgsPolygon& polygon );
133  static QgsGeometry fromMultiPolygon( const QgsMultiPolygon& multipoly );
135  static QgsGeometry fromRect( const QgsRectangle& rect );
137  static QgsGeometry collectGeometry( const QList< QgsGeometry >& geometries );
138 
144  void fromGeos( GEOSGeometry* geos );
145 
151  void fromWkb( unsigned char *wkb, int length );
152 
157  void fromWkb( const QByteArray& wkb );
158 
164  GEOSGeometry* exportToGeos( double precision = 0 ) const;
165 
169  QgsWkbTypes::Type wkbType() const;
170 
174  QgsWkbTypes::GeometryType type() const;
175 
177  bool isMultipart() const;
178 
182  bool isGeosEqual( const QgsGeometry& ) const;
183 
187  bool isGeosValid() const;
188 
192  bool isGeosEmpty() const;
193 
197  double area() const;
198 
202  double length() const;
203 
210  double distance( const QgsGeometry& geom ) const;
211 
224  //TODO QGIS 3.0 - rename beforeVertex to previousVertex, afterVertex to nextVertex
225  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist ) const;
226 
233  double distanceToVertex( int vertex ) const;
234 
242  double angleAtVertex( int vertex ) const;
243 
256  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex ) const;
257 
269  bool insertVertex( double x, double y, int beforeVertex );
270 
277  bool moveVertex( double x, double y, int atVertex );
278 
285  bool moveVertex( const QgsPointV2& p, int atVertex );
286 
297  bool deleteVertex( int atVertex );
298 
304  QgsPoint vertexAt( int atVertex ) const;
305 
311  double sqrDistToVertexAt( QgsPoint& point, int atVertex ) const;
312 
317  QgsGeometry nearestPoint( const QgsGeometry& other ) const;
318 
323  QgsGeometry shortestLine( const QgsGeometry& other ) const;
324 
331  double closestVertexWithContext( const QgsPoint& point, int& atVertex ) const;
332 
343  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = nullptr, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
344 
348  // TODO QGIS 3.0 returns an enum instead of a magic constant
349  int addRing( const QList<QgsPoint>& ring );
350 
354  // TODO QGIS 3.0 returns an enum instead of a magic constant
355  int addRing( QgsCurve* ring );
356 
363  // TODO QGIS 3.0 returns an enum instead of a magic constant
364  int addPart( const QList<QgsPoint> &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
365 
372  // TODO QGIS 3.0 returns an enum instead of a magic constant
373  int addPart( const QgsPointSequence &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
374 
381  // TODO QGIS 3.0 returns an enum instead of a magic constant
383 
390  // TODO QGIS 3.0 returns an enum instead of a magic constant
391  int addPart( GEOSGeometry *newPart );
392 
398  // TODO QGIS 3.0 returns an enum instead of a magic constant
399  int addPart( const QgsGeometry& newPart );
400 
407  QgsGeometry removeInteriorRings( double minimumAllowedArea = -1 ) const;
408 
411  int translate( double dx, double dy );
412 
415  int transform( const QgsCoordinateTransform& ct );
416 
420  int transform( const QTransform& ct );
421 
427  int rotate( double rotation, const QgsPoint& center );
428 
435  // TODO QGIS 3.0 returns an enum instead of a magic constant
436  int splitGeometry( const QList<QgsPoint>& splitLine,
437  QList<QgsGeometry>& newGeometries,
438  bool topological,
439  QList<QgsPoint> &topologyTestPoints );
440 
445  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
446 
451  int makeDifference( const QgsGeometry* other );
452 
459  QgsGeometry makeDifference( const QgsGeometry& other ) const;
460 
462  QgsRectangle boundingBox() const;
463 
465  bool intersects( const QgsRectangle& r ) const;
466 
468  bool intersects( const QgsGeometry& geometry ) const;
469 
471  bool contains( const QgsPoint* p ) const;
472 
475  bool contains( const QgsGeometry& geometry ) const;
476 
479  bool disjoint( const QgsGeometry& geometry ) const;
480 
483  bool equals( const QgsGeometry& geometry ) const;
484 
487  bool touches( const QgsGeometry& geometry ) const;
488 
491  bool overlaps( const QgsGeometry& geometry ) const;
492 
495  bool within( const QgsGeometry& geometry ) const;
496 
499  bool crosses( const QgsGeometry& geometry ) const;
500 
503  {
504  SideLeft = 0,
506  };
507 
510  {
511  CapRound = 1,
514  };
515 
518  {
519  JoinStyleRound = 1,
522  };
523 
526  QgsGeometry buffer( double distance, int segments ) const;
527 
536  QgsGeometry buffer( double distance, int segments, EndCapStyle endCapStyle, JoinStyle joinStyle, double mitreLimit ) const;
537 
545  QgsGeometry offsetCurve( double distance, int segments, JoinStyle joinStyle, double mitreLimit ) const;
546 
559  QgsGeometry singleSidedBuffer( double distance, int segments, BufferSide side,
560  JoinStyle joinStyle = JoinStyleRound,
561  double mitreLimit = 2.0 ) const;
562 
569  QgsGeometry extendLine( double startDistance, double endDistance ) const;
570 
572  QgsGeometry simplify( double tolerance ) const;
573 
581  QgsGeometry centroid() const;
582 
590  QgsGeometry pointOnSurface() const;
591 
604  QgsGeometry poleOfInaccessibility( double precision, double* distanceToBoundary = nullptr ) const;
605 
607  QgsGeometry convexHull() const;
608 
614  QgsGeometry interpolate( double distance ) const;
615 
626  double lineLocatePoint( const QgsGeometry& point ) const;
627 
636  double interpolateAngle( double distance ) const;
637 
639  QgsGeometry intersection( const QgsGeometry& geometry ) const;
640 
645  QgsGeometry combine( const QgsGeometry& geometry ) const;
646 
654  QgsGeometry mergeLines() const;
655 
657  QgsGeometry difference( const QgsGeometry& geometry ) const;
658 
660  QgsGeometry symDifference( const QgsGeometry& geometry ) const;
661 
663  QgsGeometry extrude( double x, double y );
664 
668  QByteArray exportToWkb() const;
669 
674  QString exportToWkt( int precision = 17 ) const;
675 
682  QString exportToGeoJSON( int precision = 17 ) const;
683 
690  QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const;
691 
692  /* Accessor functions for getting geometry data */
693 
697  QgsPoint asPoint() const;
698 
702  QgsPolyline asPolyline() const;
703 
707  QgsPolygon asPolygon() const;
708 
711  QgsMultiPoint asMultiPoint() const;
712 
715  QgsMultiPolyline asMultiPolyline() const;
716 
719  QgsMultiPolygon asMultiPolygon() const;
720 
723  QList<QgsGeometry> asGeometryCollection() const;
724 
729  QPointF asQPointF() const;
730 
736  QPolygonF asQPolygonF() const;
737 
742  bool deleteRing( int ringNum, int partNum = 0 );
743 
747  bool deletePart( int partNum );
748 
757  bool convertToMultiType();
758 
768  bool convertToSingleType();
769 
778  int avoidIntersections( const QHash<QgsVectorLayer*, QSet<QgsFeatureId> >& ignoreFeatures = ( QHash<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
779 
782  class Error
783  {
784  QString message;
785  QgsPoint location;
786  bool hasLocation;
787  public:
788  Error() : message( QStringLiteral( "none" ) ), hasLocation( false ) {}
789  explicit Error( const QString& m ) : message( m ), hasLocation( false ) {}
790  Error( const QString& m, const QgsPoint& p ) : message( m ), location( p ), hasLocation( true ) {}
791 
792  QString what() { return message; }
793  QgsPoint where() { return location; }
794  bool hasWhere() { return hasLocation; }
795  };
796 
801  void validateGeometry( QList<Error> &errors );
802 
807  static QgsGeometry unaryUnion( const QList<QgsGeometry>& geometryList );
808 
813  void convertToStraightSegment();
814 
820  bool requiresConversionToStraightSegments() const;
821 
826  void mapToPixel( const QgsMapToPixel& mtp );
827 
828  // not implemented for 2.10
829  /* Clips the geometry using the specified rectangle
830  * @param rect clip rectangle
831  * @note added in QGIS 2.10
832  */
833  // void clip( const QgsRectangle& rect );
834 
839  void draw( QPainter& p ) const;
840 
848  bool vertexIdFromVertexNr( int nr, QgsVertexId& id ) const;
849 
856  int vertexNrFromVertexId( QgsVertexId i ) const;
857 
862  static GEOSContextHandle_t getGEOSHandler();
863 
868  static QgsGeometry fromQPointF( QPointF point );
869 
876  static QgsGeometry fromQPolygonF( const QPolygonF& polygon );
877 
883  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
884 
890  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
891 
900  static bool compare( const QgsPolyline& p1, const QgsPolyline& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
901 
910  static bool compare( const QgsPolygon& p1, const QgsPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
911 
921  static bool compare( const QgsMultiPolygon& p1, const QgsMultiPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
922 
934  QgsGeometry smooth( const unsigned int iterations = 1, const double offset = 0.25,
935  double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
936 
939  static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometry* geometry );
940 
945  static void convertPointList( const QList<QgsPoint> &input, QgsPointSequence &output );
946 
951  static void convertPointList( const QgsPointSequence &input, QList<QgsPoint> &output );
952 
954  operator QVariant() const
955  {
956  return QVariant::fromValue( *this );
957  }
958 
963  operator bool() const;
964 
965  private:
966 
967  QgsGeometryPrivate* d; //implicitely shared data pointer
968 
969  void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
970 
971  static void convertToPolyline( const QgsPointSequence &input, QgsPolyline& output );
972  static void convertPolygon( const QgsPolygonV2& input, QgsPolygon& output );
973 
975  QgsGeometry convertToPoint( bool destMultipart ) const;
977  QgsGeometry convertToLine( bool destMultipart ) const;
979  QgsGeometry convertToPolygon( bool destMultipart ) const;
980 
991  QgsLineString* smoothLine( const QgsLineString & line, const unsigned int iterations = 1, const double offset = 0.25,
992  double minimumDistance = -1, double maxAngle = 180.0 ) const;
993 
1004  QgsPolygonV2* smoothPolygon( const QgsPolygonV2 &polygon, const unsigned int iterations = 1, const double offset = 0.25,
1005  double minimumDistance = -1, double maxAngle = 180.0 ) const;
1006 
1007 
1008 }; // class QgsGeometry
1009 
1011 
1012 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry );
1015 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry );
1016 
1017 #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:35
QgsPoint where()
Definition: qgsgeometry.h:793
Error(const QString &m)
Definition: qgsgeometry.h:789
QVector< QgsPoint > QgsPolyline
Polyline is represented as a vector of points.
Definition: qgsgeometry.h:46
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:78
EndCapStyle
End cap styles for buffers.
Definition: qgsgeometry.h:509
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:33
Polygon geometry type.
Definition: qgspolygon.h:29
Utility class for identifying a unique vertex within a geometry.
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:326
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:35
QVector< QgsPolygon > QgsMultiPolygon
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:61
QVector< QgsPoint > QgsMultiPoint
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:55
Square cap (extends past start/end of line by buffer distance)
Definition: qgsgeometry.h:513
Abstract base class for curved geometry type.
Definition: qgscurve.h:32
QVector< QgsPolyline > QgsPolygon
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
Definition: qgsgeometry.h:52
Abstract base class for all geometries.
A class to represent a point.
Definition: qgspoint.h:111
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:58
BufferSide
Side of line to buffer.
Definition: qgsgeometry.h:502
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:35
Buffer to right of line.
Definition: qgsgeometry.h:505
Class for doing transforms between two map coordinate systems.
Contains geometry relation and modification algorithms.
Use mitred joins.
Definition: qgsgeometry.h:520
Use beveled joins.
Definition: qgsgeometry.h:521
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:517
Flat cap (in line with start/end of line)
Definition: qgsgeometry.h:512
QList< QgsPointV2 > QgsPointSequence
Represents a vector layer which manages a vector based data sets.
Error(const QString &m, const QgsPoint &p)
Definition: qgsgeometry.h:790