QGIS API Documentation  2.4.0-Chugiak
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 *
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
27 #if defined(GEOS_VERSION_MAJOR) && (GEOS_VERSION_MAJOR<3)
28 #define GEOSGeometry struct GEOSGeom_t
29 #define GEOSCoordSequence struct GEOSCoordSeq_t
30 #endif
31
32 #include "qgspoint.h"
33 #include "qgscoordinatetransform.h"
34 #include "qgsfeature.h"
35
36 #include <QSet>
37
39
41 typedef QVector<QgsPoint> QgsPolyline;
42
44 typedef QVector<QgsPolyline> QgsPolygon;
45
47 typedef QVector<QgsPoint> QgsMultiPoint;
48
50 typedef QVector<QgsPolyline> QgsMultiPolyline;
51
53 typedef QVector<QgsPolygon> QgsMultiPolygon;
54
55 class QgsRectangle;
56
71 class QgsConstWkbPtr;
72 class QgsWkbPtr;
73
74 class CORE_EXPORT QgsGeometry
75 {
76  public:
78  QgsGeometry();
79
81  QgsGeometry( const QgsGeometry & );
82
86  QgsGeometry & operator=( QgsGeometry const & rhs );
87
89  ~QgsGeometry();
90
92  static QgsGeometry* fromWkt( QString wkt );
93
95  static QgsGeometry* fromPoint( const QgsPoint& point );
97  static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
99  static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
101  static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
103  static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
105  static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
107  static QgsGeometry* fromRect( const QgsRectangle& rect );
113  void fromGeos( GEOSGeometry* geos );
118  void fromWkb( unsigned char * wkb, size_t length );
119
124  const unsigned char* asWkb() const;
125
129  size_t wkbSize() const;
130
135  const GEOSGeometry* asGeos() const;
136
138  QGis::WkbType wkbType() const;
139
141  QGis::GeometryType type();
142
144  bool isMultipart();
145
149  bool isGeosEqual( QgsGeometry & );
150
154  bool isGeosValid();
155
159  bool isGeosEmpty();
160
164  double area();
165
169  double length();
170
171  double distance( QgsGeometry& geom );
172
177  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
178
191  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
192
204  bool insertVertex( double x, double y, int beforeVertex );
205
212  bool moveVertex( double x, double y, int atVertex );
213
224  bool deleteVertex( int atVertex );
225
231  QgsPoint vertexAt( int atVertex );
232
238  double sqrDistToVertexAt( QgsPoint& point, int atVertex );
239
246  double closestVertexWithContext( const QgsPoint& point, int& atVertex );
247
258  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON );
259
263  int addRing( const QList<QgsPoint>& ring );
264
268  int addPart( const QList<QgsPoint> &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
269
275  int addPart( GEOSGeometry *newPart );
276
282  int addPart( QgsGeometry *newPart );
283
286  int translate( double dx, double dy );
287
290  int transform( const QgsCoordinateTransform& ct );
291
299  int splitGeometry( const QList<QgsPoint>& splitLine,
300  QList<QgsGeometry*>&newGeometries,
301  bool topological,
302  QList<QgsPoint> &topologyTestPoints );
303
307  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
308
312  int makeDifference( QgsGeometry* other );
313
315  QgsRectangle boundingBox();
316
318  bool intersects( const QgsRectangle& r ) const;
319
321  bool intersects( const QgsGeometry* geometry ) const;
322
324  bool contains( const QgsPoint* p ) const;
325
328  bool contains( const QgsGeometry* geometry ) const;
329
332  bool disjoint( const QgsGeometry* geometry ) const;
333
336  bool equals( const QgsGeometry* geometry ) const;
337
340  bool touches( const QgsGeometry* geometry ) const;
341
344  bool overlaps( const QgsGeometry* geometry ) const;
345
348  bool within( const QgsGeometry* geometry ) const;
349
352  bool crosses( const QgsGeometry* geometry ) const;
353
356  QgsGeometry* buffer( double distance, int segments );
357
367  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit );
368
374  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit );
375
377  QgsGeometry* simplify( double tolerance );
378
382  QgsGeometry* centroid();
383
385  QgsGeometry* pointOnSurface();
386
388  QgsGeometry* convexHull();
389
390  /* Return interpolated point on line at distance
391  * @note added in 1.9
392  */
393  QgsGeometry* interpolate( double distance );
394
396  QgsGeometry* intersection( QgsGeometry* geometry );
397
401  QgsGeometry* combine( QgsGeometry* geometry );
402
404  QgsGeometry* difference( QgsGeometry* geometry );
405
407  QgsGeometry* symDifference( QgsGeometry* geometry );
408
412  QString exportToWkt() const;
413
419  QString exportToGeoJSON() const;
420
427  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false );
428
429
430  /* Accessor functions for getting geometry data */
431
434  QgsPoint asPoint() const;
435
438  QgsPolyline asPolyline() const;
439
442  QgsPolygon asPolygon() const;
443
446  QgsMultiPoint asMultiPoint() const;
447
450  QgsMultiPolyline asMultiPolyline() const;
451
454  QgsMultiPolygon asMultiPolygon() const;
455
458  QList<QgsGeometry*> asGeometryCollection() const;
459
464  bool deleteRing( int ringNum, int partNum = 0 );
465
469  bool deletePart( int partNum );
470
474  bool convertToMultiType();
475
484  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
485
486  class Error
487  {
488  QString message;
491  public:
492  Error() : message( "none" ), hasLocation( false ) {}
493  Error( QString m ) : message( m ), hasLocation( false ) {}
494  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
495
496  QString what() { return message; };
497  QgsPoint where() { return location; }
498  bool hasWhere() { return hasLocation; }
499  };
500
505  void validateGeometry( QList<Error> &errors );
506
511  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
512
513  private:
514  // Private variables
515
516  // All of these are mutable since there may be on-the-fly
517  // conversions between WKB, GEOS and Wkt;
518  // However the intent is the const functions do not
519  // semantically change the value that this object represents.
520
524  mutable unsigned char * mGeometry;
525
527  mutable size_t mGeometrySize;
528
530  mutable GEOSGeometry* mGeos;
531
533  mutable bool mDirtyWkb;
534
536  mutable bool mDirtyGeos;
537
538
539  // Private functions
540
544  bool exportWkbToGeos() const;
545
549  bool exportGeosToWkb() const;
550
564  bool insertVertex( double x, double y,
565  int beforeVertex,
566  const GEOSCoordSequence* old_sequence,
567  GEOSCoordSequence** new_sequence );
568
574  void translateVertex( QgsWkbPtr &wkbPtr, double dx, double dy, bool hasZValue );
575
580  void transformVertex( QgsWkbPtr &wkbPtr, const QgsCoordinateTransform& ct, bool hasZValue );
581
582  //helper functions for geometry splitting
583
588  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
591  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
593  GEOSGeometry* linePointDifference( GEOSGeometry* GEOSsplitPoint );
594
597  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
598
604  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
605
611  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
612
615  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
616
619  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
620
625  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
626
628  static int geomDigits( const GEOSGeometry* geom );
629
631  int numberOfGeometries( GEOSGeometry* g ) const;
632
633  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
634
636  QgsPoint asPoint( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
637
639  QgsPolyline asPolyline( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
640
642  QgsPolygon asPolygon( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
643
644  static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ),
645  const QgsGeometry* a, const QgsGeometry* b );
646
648  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
649
650  static inline bool moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &y, int atVertex, bool hasZValue, int &pointIndex, bool isRing );
651  static inline int deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int atVertex, bool hasZValue, int &pointIndex, bool isRing, bool lastItem );
652  static inline bool insertVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int beforeVertex, const double &x, const double &y, bool hasZValue, int &pointIndex, bool isRing );
653
655  QgsGeometry* convertToPoint( bool destMultipart );
657  QgsGeometry* convertToLine( bool destMultipart );
659  QgsGeometry* convertToPolygon( bool destMultipart );
660 }; // class QgsGeometry
661
663
664 class CORE_EXPORT QgsWkbPtr
665 {
666  mutable unsigned char *mP;
667
668  public:
669  QgsWkbPtr( unsigned char *p ) { mP = p; }
670
671  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
672  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
673  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
674  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
675  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
676
677  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
678  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
679  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
680  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
681  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
682
683  inline void operator+=( int n ) { mP += n; }
684
685  inline operator unsigned char *() const { return mP; }
686 };
687
688 class CORE_EXPORT QgsConstWkbPtr
689 {
690  mutable unsigned char *mP;
691
692  public:
693  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
694
695  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
696  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
697  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
698  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
699  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
700
701  inline void operator+=( int n ) { mP += n; }
702
703  inline operator const unsigned char *() const { return mP; }
704 };
705
706 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsWkbPtr(unsigned char *p)
Definition: qgsgeometry.h:669
Error(QString m)
Definition: qgsgeometry.h:493
GeometryType
Definition: qgis.h:155
bool mDirtyWkb
If the geometry has been set since the last conversion to WKB.
Definition: qgsgeometry.h:533
unsigned char * mP
Definition: qgsgeometry.h:690
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:698
QgsPoint where()
Definition: qgsgeometry.h:497
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:38
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:695
const QgsWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:672
WkbType
Used for symbology operations.
Definition: qgis.h:53
void operator+=(int n)
Definition: qgsgeometry.h:683
size_t mGeometrySize
size of geometry
Definition: qgsgeometry.h:527
unsigned char * mP
Definition: qgsgeometry.h:666
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:697
QgsWkbPtr & operator<<(const int &v)
Definition: qgsgeometry.h:678
const double DEFAULT_SEGMENT_EPSILON
default snapping tolerance for segments (
Definition: qgis.h:414
const QgsConstWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:699
QgsWkbPtr & operator<<(const double &v)
Definition: qgsgeometry.h:677
QVector< QgsPolygon > QgsMultiPolygon
a collection of QgsPolygons that share a common collection of attributes
Definition: qgsgeometry.h:53
QVector< QgsPoint > QgsMultiPoint
a collection of QgsPoints that share a common collection of attributes
Definition: qgsgeometry.h:47
QgsWkbPtr & operator<<(const QGis::WkbType &v)
Definition: qgsgeometry.h:681
const QgsWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:671
QVector< QgsPolyline > QgsPolygon
polygon: first item of the list is outer ring, inner rings (if any) start from second item ...
Definition: qgsgeometry.h:44
A class to represent a point geometry.
Definition: qgspoint.h:63
const QgsWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:674
bool mDirtyGeos
If the geometry has been set since the last conversion to GEOS.
Definition: qgsgeometry.h:536
QVector< QgsPolyline > QgsMultiPolyline
a collection of QgsPolylines that share a common collection of attributes
Definition: qgsgeometry.h:50
const QgsWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:675
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:696
unsigned char * mGeometry
pointer to geometry in binary WKB format This is the class' native implementation ...
Definition: qgsgeometry.h:524
QgsWkbPtr & operator<<(const unsigned int &v)
Definition: qgsgeometry.h:679
Class for doing transforms between two map coordinate systems.
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:673
double ANALYSIS_EXPORT leftOf(Point3D *thepoint, Point3D *p1, Point3D *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'.
void operator+=(int n)
Definition: qgsgeometry.h:701
Represents a vector layer which manages a vector based data sets.
Q_DECLARE_METATYPE(QgsGeometry)
QgsWkbPtr & operator<<(const char &v)
Definition: qgsgeometry.h:680
QgsConstWkbPtr(const unsigned char *p)
Definition: qgsgeometry.h:693
Error(QString m, QgsPoint p)
Definition: qgsgeometry.h:494
GEOSGeometry * mGeos
cached GEOS version of this geometry
Definition: qgsgeometry.h:530