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() const;
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 
299  int transform( const QTransform& ct );
300 
306  int rotate( double rotation, const QgsPoint& center );
307 
315  int splitGeometry( const QList<QgsPoint>& splitLine,
316  QList<QgsGeometry*>&newGeometries,
317  bool topological,
318  QList<QgsPoint> &topologyTestPoints );
319 
322  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
323 
327  int makeDifference( QgsGeometry* other );
328 
330  QgsRectangle boundingBox();
331 
333  bool intersects( const QgsRectangle& r ) const;
334 
336  bool intersects( const QgsGeometry* geometry ) const;
337 
339  bool contains( const QgsPoint* p ) const;
340 
342  bool contains( const QgsGeometry* geometry ) const;
343 
345  bool disjoint( const QgsGeometry* geometry ) const;
346 
348  bool equals( const QgsGeometry* geometry ) const;
349 
351  bool touches( const QgsGeometry* geometry ) const;
352 
354  bool overlaps( const QgsGeometry* geometry ) const;
355 
357  bool within( const QgsGeometry* geometry ) const;
358 
360  bool crosses( const QgsGeometry* geometry ) const;
361 
364  QgsGeometry* buffer( double distance, int segments );
365 
375  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit );
376 
382  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit );
383 
385  QgsGeometry* simplify( double tolerance );
386 
390  QgsGeometry* centroid();
391 
393  QgsGeometry* pointOnSurface();
394 
397 
398  /* Return interpolated point on line at distance */
399  QgsGeometry* interpolate( double distance );
400 
402  QgsGeometry* intersection( QgsGeometry* geometry );
403 
407  QgsGeometry* combine( QgsGeometry* geometry );
408 
410  QgsGeometry* difference( QgsGeometry* geometry );
411 
413  QgsGeometry* symDifference( QgsGeometry* geometry );
414 
419  QString exportToWkt( const int &precision = 17 ) const;
420 
425  QString exportToGeoJSON( const int &precision = 17 ) const;
426 
433  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false );
434 
435  /* Accessor functions for getting geometry data */
436 
439  QgsPoint asPoint() const;
440 
443  QgsPolyline asPolyline() const;
444 
447  QgsPolygon asPolygon() const;
448 
451  QgsMultiPoint asMultiPoint() const;
452 
455  QgsMultiPolyline asMultiPolyline() const;
456 
459  QgsMultiPolygon asMultiPolygon() const;
460 
462  QList<QgsGeometry*> asGeometryCollection() const;
463 
468  QPointF asQPointF() const;
469 
475  QPolygonF asQPolygonF() const;
476 
480  bool deleteRing( int ringNum, int partNum = 0 );
481 
484  bool deletePart( int partNum );
485 
489  bool convertToMultiType();
490 
498  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
499 
500  class Error
501  {
502  QString message;
503  QgsPoint location;
504  bool hasLocation;
505  public:
506  Error() : message( "none" ), hasLocation( false ) {}
507  Error( QString m ) : message( m ), hasLocation( false ) {}
508  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
509 
510  QString what() { return message; };
511  QgsPoint where() { return location; }
512  bool hasWhere() { return hasLocation; }
513  };
514 
516  void validateGeometry( QList<Error> &errors );
517 
522  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
523 
524  private:
525  // Private variables
526 
527  // All of these are mutable since there may be on-the-fly
528  // conversions between WKB, GEOS and Wkt;
529  // However the intent is the const functions do not
530  // semantically change the value that this object represents.
531 
535  mutable unsigned char * mGeometry;
536 
538  mutable size_t mGeometrySize;
539 
541  mutable GEOSGeometry* mGeos;
542 
544  mutable bool mDirtyWkb;
545 
547  mutable bool mDirtyGeos;
548 
549 
550  // Private functions
551 
555  bool exportWkbToGeos() const;
556 
560  bool exportGeosToWkb() const;
561 
575  bool insertVertex( double x, double y,
576  int beforeVertex,
577  const GEOSCoordSequence* old_sequence,
578  GEOSCoordSequence** new_sequence );
579 
584  void transformVertex( QgsWkbPtr &wkbPtr, const QTransform& trans, bool hasZValue );
585 
590  void transformVertex( QgsWkbPtr &wkbPtr, const QgsCoordinateTransform& ct, bool hasZValue );
591 
592  //helper functions for geometry splitting
593 
598  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
601  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
603  GEOSGeometry* linePointDifference( GEOSGeometry* GEOSsplitPoint );
604 
607  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
608 
614  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
615 
621  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
622 
625  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
626 
629  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
630 
635  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
636 
638  static int geomDigits( const GEOSGeometry* geom );
639 
641  int numberOfGeometries( GEOSGeometry* g ) const;
642 
643  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
644 
646  QgsPoint asPoint( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
647 
649  QgsPolyline asPolyline( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
650 
652  QgsPolygon asPolygon( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
653 
654  static bool geosRelOp( char( *op )( GEOSContextHandle_t handle, const GEOSGeometry*, const GEOSGeometry * ),
655  const QgsGeometry* a, const QgsGeometry* b );
656 
658  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
659 
660  static inline bool moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &y, int atVertex, bool hasZValue, int &pointIndex, bool isRing );
661  static inline int deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int atVertex, bool hasZValue, int &pointIndex, bool isRing, bool lastItem );
662  static inline bool insertVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int beforeVertex, const double &x, const double &y, bool hasZValue, int &pointIndex, bool isRing );
663 
665  QgsGeometry* convertToPoint( bool destMultipart );
667  QgsGeometry* convertToLine( bool destMultipart );
669  QgsGeometry* convertToPolygon( bool destMultipart );
670 
671  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
672  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
673 }; // class QgsGeometry
674 
676 
677 class CORE_EXPORT QgsWkbPtr
678 {
679  mutable unsigned char *mP;
680 
681  public:
682  QgsWkbPtr( unsigned char *p ) { mP = p; }
683 
684  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
685  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
686  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
687  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
688  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
689 
690  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
691  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
692  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
693  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
694  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
695 
696  inline void operator+=( int n ) { mP += n; }
697 
698  inline operator unsigned char *() const { return mP; }
699 };
700 
701 class CORE_EXPORT QgsConstWkbPtr
702 {
703  mutable unsigned char *mP;
704 
705  public:
706  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
707 
708  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
709  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
710  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
711  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
712  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
713 
714  inline void operator+=( int n ) { mP += n; }
715 
716  inline operator const unsigned char *() const { return mP; }
717 };
718 
719 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsWkbPtr(unsigned char *p)
Definition: qgsgeometry.h:682
Error(QString m)
Definition: qgsgeometry.h:507
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:711
QgsPoint where()
Definition: qgsgeometry.h:511
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:33
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:708
const QgsWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:685
void operator+=(int n)
Definition: qgsgeometry.h:696
GeometryType
Definition: qgis.h:155
WkbType
Used for symbology operations.
Definition: qgis.h:53
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:710
QgsWkbPtr & operator<<(const int &v)
Definition: qgsgeometry.h:691
const double DEFAULT_SEGMENT_EPSILON
default snapping tolerance for segments
Definition: qgis.h:414
const QgsConstWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:712
QgsWkbPtr & operator<<(const double &v)
Definition: qgsgeometry.h:690
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:694
const QgsWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:684
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.
Definition: qgspoint.h:63
const QgsWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:687
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:688
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:709
QgsWkbPtr & operator<<(const unsigned int &v)
Definition: qgsgeometry.h:692
Class for doing transforms between two map coordinate systems.
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:686
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:714
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:693
QgsConstWkbPtr(const unsigned char *p)
Definition: qgsgeometry.h:706
Error(QString m, QgsPoint p)
Definition: qgsgeometry.h:508