QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
27 #include "qgspoint.h"
28 #include "qgscoordinatetransform.h"
29 #include "qgsfeature.h"
30 
31 #include <QSet>
32 
34 
36 typedef QVector<QgsPoint> QgsPolyline;
37 
39 typedef QVector<QgsPolyline> QgsPolygon;
40 
42 typedef QVector<QgsPoint> QgsMultiPoint;
43 
45 typedef QVector<QgsPolyline> QgsMultiPolyline;
46 
48 typedef QVector<QgsPolygon> QgsMultiPolygon;
49 
50 class QgsRectangle;
51 
66 class QgsConstWkbPtr;
67 class QgsWkbPtr;
68 
69 class CORE_EXPORT QgsGeometry
70 {
71  public:
73  QgsGeometry();
74 
76  QgsGeometry( const QgsGeometry & );
77 
81  QgsGeometry & operator=( QgsGeometry const & rhs );
82 
84  ~QgsGeometry();
85 
90  static GEOSContextHandle_t getGEOSHandler();
91 
93  static QgsGeometry* fromWkt( QString wkt );
94 
96  static QgsGeometry* fromPoint( const QgsPoint& point );
98  static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
100  static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
102  static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
104  static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
106  static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
108  static QgsGeometry* fromRect( const QgsRectangle& rect );
113  static QgsGeometry* fromQPointF( const QPointF& point );
114 
121  static QgsGeometry* fromQPolygonF( const QPolygonF& polygon );
122 
128  void fromGeos( GEOSGeometry* geos );
133  void fromWkb( unsigned char * wkb, size_t length );
134 
139  const unsigned char* asWkb() const;
140 
144  size_t wkbSize() const;
145 
149  const GEOSGeometry* asGeos() const;
150 
152  QGis::WkbType wkbType() const;
153 
155  QGis::GeometryType type();
156 
158  bool isMultipart();
159 
161  bool isGeosEqual( QgsGeometry & );
162 
164  bool isGeosValid();
165 
167  bool isGeosEmpty();
168 
170  double area();
171 
173  double length();
174 
175  double distance( QgsGeometry& geom );
176 
181  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
182 
195  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
196 
208  bool insertVertex( double x, double y, int beforeVertex );
209 
216  bool moveVertex( double x, double y, int atVertex );
217 
228  bool deleteVertex( int atVertex );
229 
235  QgsPoint vertexAt( int atVertex );
236 
242  double sqrDistToVertexAt( QgsPoint& point, int atVertex );
243 
250  double closestVertexWithContext( const QgsPoint& point, int& atVertex );
251 
262  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON );
263 
267  int addRing( const QList<QgsPoint>& ring );
268 
272  int addPart( const QList<QgsPoint> &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
273 
279  int addPart( GEOSGeometry *newPart );
280 
286  int addPart( QgsGeometry *newPart );
287 
290  int translate( double dx, double dy );
291 
294  int transform( const QgsCoordinateTransform& ct );
295 
303  int splitGeometry( const QList<QgsPoint>& splitLine,
304  QList<QgsGeometry*>&newGeometries,
305  bool topological,
306  QList<QgsPoint> &topologyTestPoints );
307 
310  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
311 
315  int makeDifference( QgsGeometry* other );
316 
318  QgsRectangle boundingBox();
319 
321  bool intersects( const QgsRectangle& r ) const;
322 
324  bool intersects( const QgsGeometry* geometry ) const;
325 
327  bool contains( const QgsPoint* p ) const;
328 
330  bool contains( const QgsGeometry* geometry ) const;
331 
333  bool disjoint( const QgsGeometry* geometry ) const;
334 
336  bool equals( const QgsGeometry* geometry ) const;
337 
339  bool touches( const QgsGeometry* geometry ) const;
340 
342  bool overlaps( const QgsGeometry* geometry ) const;
343 
345  bool within( const QgsGeometry* geometry ) const;
346 
348  bool crosses( const QgsGeometry* geometry ) const;
349 
352  QgsGeometry* buffer( double distance, int segments );
353 
363  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit );
364 
370  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit );
371 
373  QgsGeometry* simplify( double tolerance );
374 
378  QgsGeometry* centroid();
379 
381  QgsGeometry* pointOnSurface();
382 
385 
386  /* Return interpolated point on line at distance */
387  QgsGeometry* interpolate( double distance );
388 
390  QgsGeometry* intersection( QgsGeometry* geometry );
391 
395  QgsGeometry* combine( QgsGeometry* geometry );
396 
398  QgsGeometry* difference( QgsGeometry* geometry );
399 
401  QgsGeometry* symDifference( QgsGeometry* geometry );
402 
407  QString exportToWkt( const int &precision = 17 ) const;
408 
413  QString exportToGeoJSON( const int &precision = 17 ) const;
414 
421  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false );
422 
423 
424  /* Accessor functions for getting geometry data */
425 
428  QgsPoint asPoint() const;
429 
432  QgsPolyline asPolyline() const;
433 
436  QgsPolygon asPolygon() const;
437 
440  QgsMultiPoint asMultiPoint() const;
441 
444  QgsMultiPolyline asMultiPolyline() const;
445 
448  QgsMultiPolygon asMultiPolygon() const;
449 
451  QList<QgsGeometry*> asGeometryCollection() const;
452 
457  QPointF asQPointF() const;
458 
464  QPolygonF asQPolygonF() const;
465 
470  bool deleteRing( int ringNum, int partNum = 0 );
471 
475  bool deletePart( int partNum );
476 
480  bool convertToMultiType();
481 
489  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
490 
491  class Error
492  {
493  QString message;
494  QgsPoint location;
495  bool hasLocation;
496  public:
497  Error() : message( "none" ), hasLocation( false ) {}
498  Error( QString m ) : message( m ), hasLocation( false ) {}
499  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
500 
501  QString what() { return message; };
502  QgsPoint where() { return location; }
503  bool hasWhere() { return hasLocation; }
504  };
505 
507  void validateGeometry( QList<Error> &errors );
508 
513  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
514 
515  private:
516  // Private variables
517 
518  // All of these are mutable since there may be on-the-fly
519  // conversions between WKB, GEOS and Wkt;
520  // However the intent is the const functions do not
521  // semantically change the value that this object represents.
522 
526  mutable unsigned char * mGeometry;
527 
529  mutable size_t mGeometrySize;
530 
532  mutable GEOSGeometry* mGeos;
533 
535  mutable bool mDirtyWkb;
536 
538  mutable bool mDirtyGeos;
539 
540 
541  // Private functions
542 
546  bool exportWkbToGeos() const;
547 
551  bool exportGeosToWkb() const;
552 
566  bool insertVertex( double x, double y,
567  int beforeVertex,
568  const GEOSCoordSequence* old_sequence,
569  GEOSCoordSequence** new_sequence );
570 
576  void translateVertex( QgsWkbPtr &wkbPtr, double dx, double dy, bool hasZValue );
577 
582  void transformVertex( QgsWkbPtr &wkbPtr, const QgsCoordinateTransform& ct, bool hasZValue );
583 
584  //helper functions for geometry splitting
585 
590  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
593  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
595  GEOSGeometry* linePointDifference( GEOSGeometry* GEOSsplitPoint );
596 
599  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
600 
606  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
607 
613  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
614 
617  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
618 
621  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
622 
627  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
628 
630  static int geomDigits( const GEOSGeometry* geom );
631 
633  int numberOfGeometries( GEOSGeometry* g ) const;
634 
635  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
636 
638  QgsPoint asPoint( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
639 
641  QgsPolyline asPolyline( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
642 
644  QgsPolygon asPolygon( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
645 
646  static bool geosRelOp( char( *op )( GEOSContextHandle_t handle, const GEOSGeometry*, const GEOSGeometry * ),
647  const QgsGeometry* a, const QgsGeometry* b );
648 
650  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
651 
652  static inline bool moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &y, int atVertex, bool hasZValue, int &pointIndex, bool isRing );
653  static inline int deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int atVertex, bool hasZValue, int &pointIndex, bool isRing, bool lastItem );
654  static inline bool insertVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int beforeVertex, const double &x, const double &y, bool hasZValue, int &pointIndex, bool isRing );
655 
657  QgsGeometry* convertToPoint( bool destMultipart );
659  QgsGeometry* convertToLine( bool destMultipart );
661  QgsGeometry* convertToPolygon( bool destMultipart );
662 
663  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
664  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
665 }; // class QgsGeometry
666 
668 
669 class CORE_EXPORT QgsWkbPtr
670 {
671  mutable unsigned char *mP;
672 
673  public:
674  QgsWkbPtr( unsigned char *p ) { mP = p; }
675 
676  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
677  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
678  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
679  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
680  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
681 
682  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
683  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
684  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
685  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
686  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
687 
688  inline void operator+=( int n ) { mP += n; }
689 
690  inline operator unsigned char *() const { return mP; }
691 };
692 
693 class CORE_EXPORT QgsConstWkbPtr
694 {
695  mutable unsigned char *mP;
696 
697  public:
698  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
699 
700  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
701  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
702  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
703  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
704  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
705 
706  inline void operator+=( int n ) { mP += n; }
707 
708  inline operator const unsigned char *() const { return mP; }
709 };
710 
711 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsWkbPtr(unsigned char *p)
Definition: qgsgeometry.h:674
Error(QString m)
Definition: qgsgeometry.h:498
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:703
QgsPoint where()
Definition: qgsgeometry.h:502
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:33
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:700
const QgsWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:677
void operator+=(int n)
Definition: qgsgeometry.h:688
GeometryType
Definition: qgis.h:155
WkbType
Used for symbology operations.
Definition: qgis.h:53
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:702
QgsWkbPtr & operator<<(const int &v)
Definition: qgsgeometry.h:683
const double DEFAULT_SEGMENT_EPSILON
default snapping tolerance for segments
Definition: qgis.h:410
const QgsConstWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:704
QgsWkbPtr & operator<<(const double &v)
Definition: qgsgeometry.h:682
QVector< QgsPolygon > QgsMultiPolygon
a collection of QgsPolygons that share a common collection of attributes
Definition: qgsgeometry.h:48
QVector< QgsPoint > QgsMultiPoint
a collection of QgsPoints that share a common collection of attributes
Definition: qgsgeometry.h:42
QgsWkbPtr & operator<<(const QGis::WkbType &v)
Definition: qgsgeometry.h:686
const QgsWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:676
QVector< QgsPolyline > QgsPolygon
polygon: first item of the list is outer ring, inner rings (if any) start from second item ...
Definition: qgsgeometry.h:39
A class to represent a point geometry.
Definition: qgspoint.h:63
const QgsWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:679
QVector< QgsPolyline > QgsMultiPolyline
a collection of QgsPolylines that share a common collection of attributes
Definition: qgsgeometry.h:45
const QgsWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:680
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:701
QgsWkbPtr & operator<<(const unsigned int &v)
Definition: qgsgeometry.h:684
Class for doing transforms between two map coordinate systems.
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:678
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:706
int convexHull(int *id, const double *const x, const double *const y, int n)
Represents a vector layer which manages a vector based data sets.
Q_DECLARE_METATYPE(QgsGeometry)
QgsWkbPtr & operator<<(const char &v)
Definition: qgsgeometry.h:685
QgsConstWkbPtr(const unsigned char *p)
Definition: qgsgeometry.h:698
Error(QString m, QgsPoint p)
Definition: qgsgeometry.h:499