QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsabstractgeometryv2.h"
34 #include "qgspoint.h"
35 #include "qgscoordinatetransform.h"
36 #include "qgsfeature.h"
37 #include <limits>
38 #include <QSet>
39 
40 class QgsGeometryEngine;
41 class QgsVectorLayer;
42 class QgsMapToPixel;
43 class QPainter;
45 
48 
51 
54 
57 
60 
61 class QgsRectangle;
62 
63 class QgsConstWkbPtr;
64 
65 struct QgsGeometryPrivate;
66 
76 class CORE_EXPORT QgsGeometry
77 {
78  public:
80  QgsGeometry();
81 
83  QgsGeometry( const QgsGeometry & );
84 
88  QgsGeometry & operator=( QgsGeometry const & rhs );
89 
94  explicit QgsGeometry( QgsAbstractGeometryV2* geom );
95 
97  ~QgsGeometry();
98 
103  QgsAbstractGeometryV2* geometry() const;
104 
109  void setGeometry( QgsAbstractGeometryV2* geometry );
110 
116  bool isEmpty() const;
117 
119  static QgsGeometry* fromWkt( const QString& wkt );
121  static QgsGeometry* fromPoint( const QgsPoint& point );
123  static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
125  static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
127  static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
129  static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
131  static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
133  static QgsGeometry* fromRect( const QgsRectangle& rect );
134 
140  void fromGeos( GEOSGeometry* geos );
141 
146  void fromWkb( unsigned char *wkb, int length );
147 
153  const unsigned char* asWkb() const;
154 
159  int wkbSize() const;
160 
166  const GEOSGeometry* asGeos( double precision = 0 ) const;
167 
171  QGis::WkbType wkbType() const;
172 
176  QGis::GeometryType type() const;
177 
179  bool isMultipart() const;
180 
184  bool isGeosEqual( const QgsGeometry& ) const;
185 
189  bool isGeosValid() const;
190 
194  bool isGeosEmpty() const;
195 
199  double area() const;
200 
204  double length() const;
205 
212  double distance( const QgsGeometry& geom ) const;
213 
226  //TODO QGIS 3.0 - rename beforeVertex to previousVertex, afterVertex to nextVertex
227  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist ) const;
228 
235  double distanceToVertex( int vertex ) const;
236 
244  double angleAtVertex( int vertex ) const;
245 
258  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex ) const;
259 
271  bool insertVertex( double x, double y, int beforeVertex );
272 
279  bool moveVertex( double x, double y, int atVertex );
280 
287  bool moveVertex( const QgsPointV2& p, int atVertex );
288 
299  bool deleteVertex( int atVertex );
300 
306  QgsPoint vertexAt( int atVertex ) const;
307 
313  double sqrDistToVertexAt( QgsPoint& point, int atVertex ) const;
314 
319  QgsGeometry nearestPoint( const QgsGeometry& other ) const;
320 
325  QgsGeometry shortestLine( const QgsGeometry& other ) const;
326 
333  double closestVertexWithContext( const QgsPoint& point, int& atVertex ) const;
334 
345  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = nullptr, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
346 
350  // TODO QGIS 3.0 returns an enum instead of a magic constant
351  int addRing( const QList<QgsPoint>& ring );
352 
356  // TODO QGIS 3.0 returns an enum instead of a magic constant
357  int addRing( QgsCurveV2* ring );
358 
365  // TODO QGIS 3.0 returns an enum instead of a magic constant
366  int addPart( const QList<QgsPoint> &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
367 
374  // TODO QGIS 3.0 returns an enum instead of a magic constant
375  int addPart( const QgsPointSequenceV2 &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
376 
383  // TODO QGIS 3.0 returns an enum instead of a magic constant
384  int addPart( QgsAbstractGeometryV2* part, QGis::GeometryType geomType = QGis::UnknownGeometry );
385 
392  // TODO QGIS 3.0 returns an enum instead of a magic constant
393  int addPart( GEOSGeometry *newPart );
394 
400  // TODO QGIS 3.0 returns an enum instead of a magic constant
401  int addPart( const QgsGeometry *newPart );
402 
405  int translate( double dx, double dy );
406 
409  int transform( const QgsCoordinateTransform& ct );
410 
414  int transform( const QTransform& ct );
415 
421  int rotate( double rotation, const QgsPoint& center );
422 
429  // TODO QGIS 3.0 returns an enum instead of a magic constant
430  int splitGeometry( const QList<QgsPoint>& splitLine,
431  QList<QgsGeometry*>&newGeometries,
432  bool topological,
433  QList<QgsPoint> &topologyTestPoints );
434 
439  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
440 
448  int reshapeGeometry( const QList<QgsPointV2>& reshapeLine );
449 
454  int makeDifference( const QgsGeometry* other );
455 
457  QgsRectangle boundingBox() const;
458 
460  bool intersects( const QgsRectangle& r ) const;
461 
463  bool intersects( const QgsGeometry* geometry ) const;
464 
466  bool contains( const QgsPoint* p ) const;
467 
470  bool contains( const QgsGeometry* geometry ) const;
471 
474  bool disjoint( const QgsGeometry* geometry ) const;
475 
478  bool equals( const QgsGeometry* geometry ) const;
479 
482  bool touches( const QgsGeometry* geometry ) const;
483 
486  bool overlaps( const QgsGeometry* geometry ) const;
487 
490  bool within( const QgsGeometry* geometry ) const;
491 
494  bool crosses( const QgsGeometry* geometry ) const;
495 
498  QgsGeometry* buffer( double distance, int segments ) const;
499 
509  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const;
510 
516  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const;
517 
519  QgsGeometry* simplify( double tolerance ) const;
520 
525  QgsGeometry* centroid() const;
526 
528  QgsGeometry* pointOnSurface() const;
529 
531  QgsGeometry* convexHull() const;
532 
538  QgsGeometry* interpolate( double distance ) const;
539 
550  double lineLocatePoint( const QgsGeometry& point ) const;
551 
560  double interpolateAngle( double distance ) const;
561 
563  QgsGeometry* intersection( const QgsGeometry* geometry ) const;
564 
569  QgsGeometry* combine( const QgsGeometry* geometry ) const;
570 
578  QgsGeometry mergeLines() const;
579 
581  QgsGeometry* difference( const QgsGeometry* geometry ) const;
582 
584  QgsGeometry* symDifference( const QgsGeometry* geometry ) const;
585 
587  QgsGeometry extrude( double x, double y );
588 
593  QString exportToWkt( int precision = 17 ) const;
594 
601  QString exportToGeoJSON( int precision = 17 ) const;
602 
609  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false ) const;
610 
611  /* Accessor functions for getting geometry data */
612 
616  QgsPoint asPoint() const;
617 
621  QgsPolyline asPolyline() const;
622 
626  QgsPolygon asPolygon() const;
627 
630  QgsMultiPoint asMultiPoint() const;
631 
634  QgsMultiPolyline asMultiPolyline() const;
635 
638  QgsMultiPolygon asMultiPolygon() const;
639 
642  QList<QgsGeometry*> asGeometryCollection() const;
643 
648  QPointF asQPointF() const;
649 
655  QPolygonF asQPolygonF() const;
656 
661  bool deleteRing( int ringNum, int partNum = 0 );
662 
666  bool deletePart( int partNum );
667 
676  bool convertToMultiType();
677 
687  bool convertToSingleType();
688 
697  int avoidIntersections( const QMap<QgsVectorLayer*, QSet<QgsFeatureId> >& ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
698 
701  class Error
702  {
703  QString message;
704  QgsPoint location;
705  bool hasLocation;
706  public:
707  Error() : message( "none" ), hasLocation( false ) {}
708  explicit Error( const QString& m ) : message( m ), hasLocation( false ) {}
709  Error( const QString& m, const QgsPoint& p ) : message( m ), location( p ), hasLocation( true ) {}
710 
711  QString what() { return message; }
712  QgsPoint where() { return location; }
713  bool hasWhere() { return hasLocation; }
714  };
715 
720  void validateGeometry( QList<Error> &errors );
721 
726  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
727 
732  void convertToStraightSegment();
733 
739  bool requiresConversionToStraightSegments() const;
740 
745  void mapToPixel( const QgsMapToPixel& mtp );
746 
747  // not implemented for 2.10
748  /* Clips the geometry using the specified rectangle
749  * @param rect clip rectangle
750  * @note added in QGIS 2.10
751  */
752  // void clip( const QgsRectangle& rect );
753 
758  void draw( QPainter& p ) const;
759 
767  bool vertexIdFromVertexNr( int nr, QgsVertexId& id ) const;
768 
775  int vertexNrFromVertexId( QgsVertexId i ) const;
776 
781  static GEOSContextHandle_t getGEOSHandler();
782 
787  static QgsGeometry* fromQPointF( QPointF point );
788 
795  static QgsGeometry* fromQPolygonF( const QPolygonF& polygon );
796 
802  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
803 
809  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
810 
819  static bool compare( const QgsPolyline& p1, const QgsPolyline& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
820 
829  static bool compare( const QgsPolygon& p1, const QgsPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
830 
840  static bool compare( const QgsMultiPolygon& p1, const QgsMultiPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
841 
851  QgsGeometry* smooth( const unsigned int iterations = 1, const double offset = 0.25 ) const;
852 
854  QgsPolygon smoothPolygon( const QgsPolygon &polygon, const unsigned int iterations = 1, const double offset = 0.25 ) const;
856  QgsPolyline smoothLine( const QgsPolyline &polyline, const unsigned int iterations = 1, const double offset = 0.25 ) const;
857 
860  static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometryV2* geometry );
861 
866  static void convertPointList( const QList<QgsPoint> &input, QgsPointSequenceV2 &output );
867 
872  static void convertPointList( const QgsPointSequenceV2 &input, QList<QgsPoint> &output );
873 
875  operator QVariant() const
876  {
877  return QVariant::fromValue( *this );
878  }
879 
880  private:
881 
882  QgsGeometryPrivate* d; //implicitely shared data pointer
883 
884  void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
885  void removeWkbGeos();
886 
887  static void convertToPolyline( const QgsPointSequenceV2 &input, QgsPolyline& output );
888  static void convertPolygon( const QgsPolygonV2& input, QgsPolygon& output );
889 
891  QgsGeometry* convertToPoint( bool destMultipart ) const;
893  QgsGeometry* convertToLine( bool destMultipart ) const;
895  QgsGeometry* convertToPolygon( bool destMultipart ) const;
896 }; // class QgsGeometry
897 
899 
900 
901 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry );
903 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry );
904 
905 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
GeometryType
Definition: qgis.h:115
QgsPoint where()
Definition: qgsgeometry.h:712
Error(const QString &m)
Definition: qgsgeometry.h:708
QVector< QgsPoint > QgsPolyline
Polyline is represented as a vector of points.
Definition: qgsgeometry.h:44
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
Abstract base class for all geometries.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
WkbType
Used for symbology operations.
Definition: qgis.h:61
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsGeometry &geometry)
Writes the geometry to stream out.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
Polygon geometry type.
Definition: qgspolygonv2.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:490
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
QVector< QgsPolygon > QgsMultiPolygon
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:59
QVector< QgsPoint > QgsMultiPoint
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:53
QVector< QgsPolyline > QgsPolygon
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
Definition: qgsgeometry.h:50
A class to represent a point.
Definition: qgspoint.h:117
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry.
QVariant fromValue(const T &value)
QVector< QgsPolyline > QgsMultiPolyline
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:56
Class for doing transforms between two map coordinate systems.
Contains geometry relation and modification algorithms.
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;.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
Represents a vector layer which manages a vector based data sets.
Error(const QString &m, const QgsPoint &p)
Definition: qgsgeometry.h:709