QGIS API Documentation  2.12.0-Lyon
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 );
145  void fromWkb( unsigned char * wkb, size_t length );
146 
152  const unsigned char* asWkb() const;
153 
158  size_t wkbSize() const;
159 
165  const GEOSGeometry* asGeos( double precision = 0 ) const;
166 
170  QGis::WkbType wkbType() const;
171 
175  QGis::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 
208  double distance( const QgsGeometry& geom ) const;
209 
214  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist ) const;
215 
228  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex ) const;
229 
241  bool insertVertex( double x, double y, int beforeVertex );
242 
249  bool moveVertex( double x, double y, int atVertex );
250 
257  bool moveVertex( const QgsPointV2& p, int atVertex );
258 
269  bool deleteVertex( int atVertex );
270 
276  QgsPoint vertexAt( int atVertex ) const;
277 
283  double sqrDistToVertexAt( QgsPoint& point, int atVertex ) const;
284 
291  double closestVertexWithContext( const QgsPoint& point, int& atVertex ) const;
292 
303  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
304 
308  int addRing( const QList<QgsPoint>& ring );
309 
313  int addRing( QgsCurveV2* ring );
314 
321  int addPart( const QList<QgsPoint> &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
322 
329  int addPart( QgsAbstractGeometryV2* part, QGis::GeometryType geomType = QGis::UnknownGeometry );
330 
337  int addPart( GEOSGeometry *newPart );
338 
344  int addPart( const QgsGeometry *newPart );
345 
348  int translate( double dx, double dy );
349 
352  int transform( const QgsCoordinateTransform& ct );
353 
357  int transform( const QTransform& ct );
358 
364  int rotate( double rotation, const QgsPoint& center );
365 
372  int splitGeometry( const QList<QgsPoint>& splitLine,
373  QList<QgsGeometry*>&newGeometries,
374  bool topological,
375  QList<QgsPoint> &topologyTestPoints );
376 
380  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
381 
385  int makeDifference( const QgsGeometry* other );
386 
388  QgsRectangle boundingBox() const;
389 
391  bool intersects( const QgsRectangle& r ) const;
392 
394  bool intersects( const QgsGeometry* geometry ) const;
395 
397  bool contains( const QgsPoint* p ) const;
398 
401  bool contains( const QgsGeometry* geometry ) const;
402 
405  bool disjoint( const QgsGeometry* geometry ) const;
406 
409  bool equals( const QgsGeometry* geometry ) const;
410 
413  bool touches( const QgsGeometry* geometry ) const;
414 
417  bool overlaps( const QgsGeometry* geometry ) const;
418 
421  bool within( const QgsGeometry* geometry ) const;
422 
425  bool crosses( const QgsGeometry* geometry ) const;
426 
429  QgsGeometry* buffer( double distance, int segments ) const;
430 
440  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const;
441 
447  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const;
448 
450  QgsGeometry* simplify( double tolerance ) const;
451 
455  QgsGeometry* centroid() const;
456 
458  QgsGeometry* pointOnSurface() const;
459 
461  QgsGeometry* convexHull() const;
462 
463  /* Return interpolated point on line at distance
464  * @note added in 1.9
465  */
466  QgsGeometry* interpolate( double distance ) const;
467 
469  QgsGeometry* intersection( const QgsGeometry* geometry ) const;
470 
474  QgsGeometry* combine( const QgsGeometry* geometry ) const;
475 
477  QgsGeometry* difference( const QgsGeometry* geometry ) const;
478 
480  QgsGeometry* symDifference( const QgsGeometry* geometry ) const;
481 
486  QString exportToWkt( const int &precision = 17 ) const;
487 
494  QString exportToGeoJSON( const int &precision = 17 ) const;
495 
502  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false ) const;
503 
504  /* Accessor functions for getting geometry data */
505 
508  QgsPoint asPoint() const;
509 
512  QgsPolyline asPolyline() const;
513 
516  QgsPolygon asPolygon() const;
517 
520  QgsMultiPoint asMultiPoint() const;
521 
524  QgsMultiPolyline asMultiPolyline() const;
525 
528  QgsMultiPolygon asMultiPolygon() const;
529 
532  QList<QgsGeometry*> asGeometryCollection() const;
533 
538  QPointF asQPointF() const;
539 
545  QPolygonF asQPolygonF() const;
546 
551  bool deleteRing( int ringNum, int partNum = 0 );
552 
556  bool deletePart( int partNum );
557 
566  bool convertToMultiType();
567 
577  bool convertToSingleType();
578 
587  int avoidIntersections( const QMap<QgsVectorLayer*, QSet<QgsFeatureId> >& ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
588 
589  class Error
590  {
591  QString message;
592  QgsPoint location;
593  bool hasLocation;
594  public:
595  Error() : message( "none" ), hasLocation( false ) {}
596  explicit Error( const QString& m ) : message( m ), hasLocation( false ) {}
597  Error( const QString& m, const QgsPoint& p ) : message( m ), location( p ), hasLocation( true ) {}
598 
599  QString what() { return message; }
600  QgsPoint where() { return location; }
601  bool hasWhere() { return hasLocation; }
602  };
603 
608  void validateGeometry( QList<Error> &errors );
609 
614  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
615 
620  void convertToStraightSegment();
621 
627  bool requiresConversionToStraightSegments() const;
628 
633  void mapToPixel( const QgsMapToPixel& mtp );
634 
635  // not implemented for 2.10
636  /* Clips the geometry using the specified rectangle
637  * @param rect clip rectangle
638  * @note added in QGIS 2.10
639  */
640  // void clip( const QgsRectangle& rect );
641 
646  void draw( QPainter& p ) const;
647 
655  bool vertexIdFromVertexNr( int nr, QgsVertexId& id ) const;
656 
663  int vertexNrFromVertexId( const QgsVertexId& i ) const;
664 
669  static GEOSContextHandle_t getGEOSHandler();
670 
675  static QgsGeometry* fromQPointF( const QPointF& point );
676 
683  static QgsGeometry* fromQPolygonF( const QPolygonF& polygon );
684 
690  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
691 
697  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
698 
707  static bool compare( const QgsPolyline& p1, const QgsPolyline& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
708 
717  static bool compare( const QgsPolygon& p1, const QgsPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
718 
728  static bool compare( const QgsMultiPolygon& p1, const QgsMultiPolygon& p2, double epsilon = 4 * std::numeric_limits<double>::epsilon() );
729 
739  QgsGeometry* smooth( const unsigned int iterations = 1, const double offset = 0.25 ) const;
740 
742  QgsPolygon smoothPolygon( const QgsPolygon &polygon, const unsigned int iterations = 1, const double offset = 0.25 ) const;
744  QgsPolyline smoothLine( const QgsPolyline &polyline, const unsigned int iterations = 1, const double offset = 0.25 ) const;
745 
748  static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometryV2* geometry );
749 
754  static void convertPointList( const QList<QgsPoint>& input, QList<QgsPointV2>& output );
755 
760  static void convertPointList( const QList<QgsPointV2>& input, QList<QgsPoint>& output );
761 
762  private:
763 
764  QgsGeometryPrivate* d; //implicitely shared data pointer
765 
766  void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
767  void removeWkbGeos();
768 
769  static void convertToPolyline( const QList<QgsPointV2>& input, QgsPolyline& output );
770  static void convertPolygon( const QgsPolygonV2& input, QgsPolygon& output );
771 
773  QgsGeometry* convertToPoint( bool destMultipart ) const;
775  QgsGeometry* convertToLine( bool destMultipart ) const;
777  QgsGeometry* convertToPolygon( bool destMultipart ) const;
778 }; // class QgsGeometry
779 
781 
783 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry );
785 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry );
786 
787 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsPoint where()
Definition: qgsgeometry.h:600
Error(const QString &m)
Definition: qgsgeometry.h:596
QVector< QgsPoint > QgsPolyline
Polyline is represented as a vector of points.
Definition: qgsgeometry.h:44
Abstract base class for all geometries.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
GeometryType
Definition: qgis.h:104
WkbType
Used for symbology operations.
Definition: qgis.h:56
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:365
Point geometry type.
Definition: qgspointv2.h:29
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:63
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry.
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 'thepoint' is left or right of the line from 'p1' to 'p2'.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
Represents a vector layer which manages a vector based data sets.
Q_DECLARE_METATYPE(QgsGeometry)
Error(const QString &m, const QgsPoint &p)
Definition: qgsgeometry.h:597