QGIS API Documentation  2.99.0-Master (5753576)
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 
119  bool isEmpty() const;
120 
122  static QgsGeometry fromWkt( const QString& wkt );
124  static QgsGeometry fromPoint( const QgsPoint& point );
126  static QgsGeometry fromMultiPoint( const QgsMultiPoint& multipoint );
128  static QgsGeometry fromPolyline( const QgsPolyline& polyline );
130  static QgsGeometry fromMultiPolyline( const QgsMultiPolyline& multiline );
132  static QgsGeometry fromPolygon( const QgsPolygon& polygon );
134  static QgsGeometry fromMultiPolygon( const QgsMultiPolygon& multipoly );
136  static QgsGeometry fromRect( const QgsRectangle& rect );
138  static QgsGeometry collectGeometry( const QList< QgsGeometry >& geometries );
139 
145  void fromGeos( GEOSGeometry* geos );
146 
152  void fromWkb( unsigned char *wkb, int length );
153 
158  void fromWkb( const QByteArray& wkb );
159 
165  GEOSGeometry* exportToGeos( double precision = 0 ) const;
166 
170  QgsWkbTypes::Type wkbType() const;
171 
175  QgsWkbTypes::GeometryType type() const;
176 
178  bool isMultipart() const;
179 
183  bool isGeosEqual( const QgsGeometry& ) const;
184 
188  bool isGeosValid() const;
189 
193  bool isGeosEmpty() const;
194 
198  double area() const;
199 
203  double length() const;
204 
211  double distance( const QgsGeometry& geom ) const;
212 
225  //TODO QGIS 3.0 - rename beforeVertex to previousVertex, afterVertex to nextVertex
226  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist ) const;
227 
234  double distanceToVertex( int vertex ) const;
235 
243  double angleAtVertex( int vertex ) const;
244 
257  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex ) const;
258 
270  bool insertVertex( double x, double y, int beforeVertex );
271 
278  bool moveVertex( double x, double y, int atVertex );
279 
286  bool moveVertex( const QgsPointV2& p, int atVertex );
287 
298  bool deleteVertex( int atVertex );
299 
305  QgsPoint vertexAt( int atVertex ) const;
306 
312  double sqrDistToVertexAt( QgsPoint& point, int atVertex ) const;
313 
318  QgsGeometry nearestPoint( const QgsGeometry& other ) const;
319 
324  QgsGeometry shortestLine( const QgsGeometry& other ) const;
325 
332  double closestVertexWithContext( const QgsPoint& point, int& atVertex ) const;
333 
344  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = nullptr, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
345 
349  // TODO QGIS 3.0 returns an enum instead of a magic constant
350  int addRing( const QList<QgsPoint>& ring );
351 
355  // TODO QGIS 3.0 returns an enum instead of a magic constant
356  int addRing( QgsCurve* ring );
357 
364  // TODO QGIS 3.0 returns an enum instead of a magic constant
365  int addPart( const QList<QgsPoint> &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
366 
373  // TODO QGIS 3.0 returns an enum instead of a magic constant
374  int addPart( const QgsPointSequence &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
375 
382  // TODO QGIS 3.0 returns an enum instead of a magic constant
384 
391  // TODO QGIS 3.0 returns an enum instead of a magic constant
392  int addPart( GEOSGeometry *newPart );
393 
399  // TODO QGIS 3.0 returns an enum instead of a magic constant
400  int addPart( const QgsGeometry& newPart );
401 
408  QgsGeometry removeInteriorRings( double minimumAllowedArea = -1 ) const;
409 
412  int translate( double dx, double dy );
413 
416  int transform( const QgsCoordinateTransform& ct );
417 
421  int transform( const QTransform& ct );
422 
428  int rotate( double rotation, const QgsPoint& center );
429 
436  // TODO QGIS 3.0 returns an enum instead of a magic constant
437  int splitGeometry( const QList<QgsPoint>& splitLine,
438  QList<QgsGeometry>& newGeometries,
439  bool topological,
440  QList<QgsPoint> &topologyTestPoints );
441 
446  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
447 
452  int makeDifference( const QgsGeometry* other );
453 
460  QgsGeometry makeDifference( const QgsGeometry& other ) const;
461 
466  QgsRectangle boundingBox() const;
467 
475  QgsGeometry orientedMinimumBoundingBox( double& area, double &angle, double& width, double& height ) const;
476 
485  QgsGeometry orthogonalize( double tolerance = 1.0E-8, int maxIterations = 1000, double angleThreshold = 15.0 ) const;
486 
488  bool intersects( const QgsRectangle& r ) const;
489 
491  bool intersects( const QgsGeometry& geometry ) const;
492 
494  bool contains( const QgsPoint* p ) const;
495 
498  bool contains( const QgsGeometry& geometry ) const;
499 
502  bool disjoint( const QgsGeometry& geometry ) const;
503 
506  bool equals( const QgsGeometry& geometry ) const;
507 
510  bool touches( const QgsGeometry& geometry ) const;
511 
514  bool overlaps( const QgsGeometry& geometry ) const;
515 
518  bool within( const QgsGeometry& geometry ) const;
519 
522  bool crosses( const QgsGeometry& geometry ) const;
523 
526  {
527  SideLeft = 0,
529  };
530 
533  {
534  CapRound = 1,
537  };
538 
541  {
542  JoinStyleRound = 1,
545  };
546 
549  QgsGeometry buffer( double distance, int segments ) const;
550 
559  QgsGeometry buffer( double distance, int segments, EndCapStyle endCapStyle, JoinStyle joinStyle, double mitreLimit ) const;
560 
568  QgsGeometry offsetCurve( double distance, int segments, JoinStyle joinStyle, double mitreLimit ) const;
569 
582  QgsGeometry singleSidedBuffer( double distance, int segments, BufferSide side,
583  JoinStyle joinStyle = JoinStyleRound,
584  double mitreLimit = 2.0 ) const;
585 
592  QgsGeometry extendLine( double startDistance, double endDistance ) const;
593 
595  QgsGeometry simplify( double tolerance ) const;
596 
604  QgsGeometry centroid() const;
605 
613  QgsGeometry pointOnSurface() const;
614 
627  QgsGeometry poleOfInaccessibility( double precision, double* distanceToBoundary = nullptr ) const;
628 
630  QgsGeometry convexHull() const;
631 
637  QgsGeometry interpolate( double distance ) const;
638 
649  double lineLocatePoint( const QgsGeometry& point ) const;
650 
659  double interpolateAngle( double distance ) const;
660 
662  QgsGeometry intersection( const QgsGeometry& geometry ) const;
663 
668  QgsGeometry combine( const QgsGeometry& geometry ) const;
669 
677  QgsGeometry mergeLines() const;
678 
680  QgsGeometry difference( const QgsGeometry& geometry ) const;
681 
683  QgsGeometry symDifference( const QgsGeometry& geometry ) const;
684 
686  QgsGeometry extrude( double x, double y );
687 
691  QByteArray exportToWkb() const;
692 
697  QString exportToWkt( int precision = 17 ) const;
698 
705  QString exportToGeoJSON( int precision = 17 ) const;
706 
713  QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const;
714 
715  /* Accessor functions for getting geometry data */
716 
720  QgsPoint asPoint() const;
721 
725  QgsPolyline asPolyline() const;
726 
730  QgsPolygon asPolygon() const;
731 
734  QgsMultiPoint asMultiPoint() const;
735 
738  QgsMultiPolyline asMultiPolyline() const;
739 
742  QgsMultiPolygon asMultiPolygon() const;
743 
746  QList<QgsGeometry> asGeometryCollection() const;
747 
752  QPointF asQPointF() const;
753 
759  QPolygonF asQPolygonF() const;
760 
765  bool deleteRing( int ringNum, int partNum = 0 );
766 
770  bool deletePart( int partNum );
771 
780  bool convertToMultiType();
781 
791  bool convertToSingleType();
792 
802  int avoidIntersections( const QList<QgsVectorLayer*>& avoidIntersectionsLayers,
803  const QHash<QgsVectorLayer*, QSet<QgsFeatureId> >& ignoreFeatures = ( QHash<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
804 
807  class Error
808  {
809  QString message;
810  QgsPoint location;
811  bool hasLocation;
812  public:
813  Error() : message( QStringLiteral( "none" ) ), hasLocation( false ) {}
814  explicit Error( const QString& m ) : message( m ), hasLocation( false ) {}
815  Error( const QString& m, const QgsPoint& p ) : message( m ), location( p ), hasLocation( true ) {}
816 
817  QString what() { return message; }
818  QgsPoint where() { return location; }
819  bool hasWhere() { return hasLocation; }
820  };
821 
826  void validateGeometry( QList<Error> &errors );
827 
832  static QgsGeometry unaryUnion( const QList<QgsGeometry>& geometryList );
833 
838  void convertToStraightSegment();
839 
845  bool requiresConversionToStraightSegments() const;
846 
851  void mapToPixel( const QgsMapToPixel& mtp );
852 
853  // not implemented for 2.10
854  /* Clips the geometry using the specified rectangle
855  * @param rect clip rectangle
856  * @note added in QGIS 2.10
857  */
858  // void clip( const QgsRectangle& rect );
859 
864  void draw( QPainter& p ) const;
865 
873  bool vertexIdFromVertexNr( int nr, QgsVertexId& id ) const;
874 
881  int vertexNrFromVertexId( QgsVertexId i ) const;
882 
887  static GEOSContextHandle_t getGEOSHandler();
888 
893  static QgsGeometry fromQPointF( QPointF point );
894 
901  static QgsGeometry fromQPolygonF( const QPolygonF& polygon );
902 
908  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
909 
915  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
916 
925  static bool compare( const QgsPolyline& p1, const QgsPolyline& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
926 
935  static bool compare( const QgsPolygon& p1, const QgsPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
936 
946  static bool compare( const QgsMultiPolygon& p1, const QgsMultiPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
947 
959  QgsGeometry smooth( const unsigned int iterations = 1, const double offset = 0.25,
960  double minimumDistance = -1.0, double maxAngle = 180.0 ) const;
961 
964  static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometry* geometry );
965 
970  static void convertPointList( const QList<QgsPoint> &input, QgsPointSequence &output );
971 
976  static void convertPointList( const QgsPointSequence &input, QList<QgsPoint> &output );
977 
979  operator QVariant() const
980  {
981  return QVariant::fromValue( *this );
982  }
983 
988  operator bool() const;
989 
990  private:
991 
992  QgsGeometryPrivate* d; //implicitly shared data pointer
993 
994  void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
995 
996  static void convertToPolyline( const QgsPointSequence &input, QgsPolyline& output );
997  static void convertPolygon( const QgsPolygonV2& input, QgsPolygon& output );
998 
1000  QgsGeometry convertToPoint( bool destMultipart ) const;
1002  QgsGeometry convertToLine( bool destMultipart ) const;
1004  QgsGeometry convertToPolygon( bool destMultipart ) const;
1005 
1016  QgsLineString* smoothLine( const QgsLineString & line, const unsigned int iterations = 1, const double offset = 0.25,
1017  double minimumDistance = -1, double maxAngle = 180.0 ) const;
1018 
1029  QgsPolygonV2* smoothPolygon( const QgsPolygonV2 &polygon, const unsigned int iterations = 1, const double offset = 0.25,
1030  double minimumDistance = -1, double maxAngle = 180.0 ) const;
1031 
1032 
1033 }; // class QgsGeometry
1034 
1036 
1037 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry );
1040 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry );
1041 
1042 #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:818
Error(const QString &m)
Definition: qgsgeometry.h:814
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:532
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:323
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:536
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:525
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:528
Class for doing transforms between two map coordinate systems.
Contains geometry relation and modification algorithms.
Use mitred joins.
Definition: qgsgeometry.h:543
Use beveled joins.
Definition: qgsgeometry.h:544
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:540
Flat cap (in line with start/end of line)
Definition: qgsgeometry.h:535
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
Error(const QString &m, const QgsPoint &p)
Definition: qgsgeometry.h:815