QGIS API Documentation  2.5.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 );
114  void fromGeos( GEOSGeometry* geos );
119  void fromWkb( unsigned char * wkb, size_t length );
120 
125  const unsigned char* asWkb() const;
126 
130  size_t wkbSize() const;
131 
136  const GEOSGeometry* asGeos() const;
137 
139  QGis::WkbType wkbType() const;
140 
142  QGis::GeometryType type();
143 
145  bool isMultipart();
146 
150  bool isGeosEqual( QgsGeometry & );
151 
155  bool isGeosValid();
156 
160  bool isGeosEmpty();
161 
165  double area();
166 
170  double length();
171 
172  double distance( QgsGeometry& geom );
173 
178  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
179 
192  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
193 
205  bool insertVertex( double x, double y, int beforeVertex );
206 
213  bool moveVertex( double x, double y, int atVertex );
214 
225  bool deleteVertex( int atVertex );
226 
232  QgsPoint vertexAt( int atVertex );
233 
239  double sqrDistToVertexAt( QgsPoint& point, int atVertex );
240 
247  double closestVertexWithContext( const QgsPoint& point, int& atVertex );
248 
259  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON );
260 
264  int addRing( const QList<QgsPoint>& ring );
265 
269  int addPart( const QList<QgsPoint> &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
270 
276  int addPart( GEOSGeometry *newPart );
277 
283  int addPart( QgsGeometry *newPart );
284 
287  int translate( double dx, double dy );
288 
291  int transform( const QgsCoordinateTransform& ct );
292 
300  int splitGeometry( const QList<QgsPoint>& splitLine,
301  QList<QgsGeometry*>&newGeometries,
302  bool topological,
303  QList<QgsPoint> &topologyTestPoints );
304 
308  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
309 
313  int makeDifference( QgsGeometry* other );
314 
316  QgsRectangle boundingBox();
317 
319  bool intersects( const QgsRectangle& r ) const;
320 
322  bool intersects( const QgsGeometry* geometry ) const;
323 
325  bool contains( const QgsPoint* p ) const;
326 
329  bool contains( const QgsGeometry* geometry ) const;
330 
333  bool disjoint( const QgsGeometry* geometry ) const;
334 
337  bool equals( const QgsGeometry* geometry ) const;
338 
341  bool touches( const QgsGeometry* geometry ) const;
342 
345  bool overlaps( const QgsGeometry* geometry ) const;
346 
349  bool within( const QgsGeometry* geometry ) const;
350 
353  bool crosses( const QgsGeometry* geometry ) const;
354 
357  QgsGeometry* buffer( double distance, int segments );
358 
368  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit );
369 
375  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit );
376 
378  QgsGeometry* simplify( double tolerance );
379 
383  QgsGeometry* centroid();
384 
386  QgsGeometry* pointOnSurface();
387 
390 
391  /* Return interpolated point on line at distance
392  * @note added in 1.9
393  */
394  QgsGeometry* interpolate( double distance );
395 
397  QgsGeometry* intersection( QgsGeometry* geometry );
398 
402  QgsGeometry* combine( QgsGeometry* geometry );
403 
405  QgsGeometry* difference( QgsGeometry* geometry );
406 
408  QgsGeometry* symDifference( QgsGeometry* geometry );
409 
414  QString exportToWkt( const int &precision = 17 ) const;
415 
422  QString exportToGeoJSON( const int &precision = 17 ) const;
423 
430  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false );
431 
432 
433  /* Accessor functions for getting geometry data */
434 
437  QgsPoint asPoint() const;
438 
441  QgsPolyline asPolyline() const;
442 
445  QgsPolygon asPolygon() const;
446 
449  QgsMultiPoint asMultiPoint() const;
450 
453  QgsMultiPolyline asMultiPolyline() const;
454 
457  QgsMultiPolygon asMultiPolygon() const;
458 
461  QList<QgsGeometry*> asGeometryCollection() const;
462 
467  bool deleteRing( int ringNum, int partNum = 0 );
468 
472  bool deletePart( int partNum );
473 
477  bool convertToMultiType();
478 
487  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
488 
489  class Error
490  {
491  QString message;
492  QgsPoint location;
493  bool hasLocation;
494  public:
495  Error() : message( "none" ), hasLocation( false ) {}
496  Error( QString m ) : message( m ), hasLocation( false ) {}
497  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
498 
499  QString what() { return message; };
500  QgsPoint where() { return location; }
501  bool hasWhere() { return hasLocation; }
502  };
503 
508  void validateGeometry( QList<Error> &errors );
509 
514  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
515 
516  private:
517  // Private variables
518 
519  // All of these are mutable since there may be on-the-fly
520  // conversions between WKB, GEOS and Wkt;
521  // However the intent is the const functions do not
522  // semantically change the value that this object represents.
523 
527  mutable unsigned char * mGeometry;
528 
530  mutable size_t mGeometrySize;
531 
533  mutable GEOSGeometry* mGeos;
534 
536  mutable bool mDirtyWkb;
537 
539  mutable bool mDirtyGeos;
540 
541 
542  // Private functions
543 
547  bool exportWkbToGeos() const;
548 
552  bool exportGeosToWkb() const;
553 
567  bool insertVertex( double x, double y,
568  int beforeVertex,
569  const GEOSCoordSequence* old_sequence,
570  GEOSCoordSequence** new_sequence );
571 
577  void translateVertex( QgsWkbPtr &wkbPtr, double dx, double dy, bool hasZValue );
578 
583  void transformVertex( QgsWkbPtr &wkbPtr, const QgsCoordinateTransform& ct, bool hasZValue );
584 
585  //helper functions for geometry splitting
586 
591  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
594  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
596  GEOSGeometry* linePointDifference( GEOSGeometry* GEOSsplitPoint );
597 
600  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
601 
607  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
608 
614  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
615 
618  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
619 
622  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
623 
628  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
629 
631  static int geomDigits( const GEOSGeometry* geom );
632 
634  int numberOfGeometries( GEOSGeometry* g ) const;
635 
636  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
637 
639  QgsPoint asPoint( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
640 
642  QgsPolyline asPolyline( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
643 
645  QgsPolygon asPolygon( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
646 
647  static bool geosRelOp( char( *op )( GEOSContextHandle_t handle, const GEOSGeometry*, const GEOSGeometry * ),
648  const QgsGeometry* a, const QgsGeometry* b );
649 
651  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
652 
653  static inline bool moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &y, int atVertex, bool hasZValue, int &pointIndex, bool isRing );
654  static inline int deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int atVertex, bool hasZValue, int &pointIndex, bool isRing, bool lastItem );
655  static inline bool insertVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int beforeVertex, const double &x, const double &y, bool hasZValue, int &pointIndex, bool isRing );
656 
658  QgsGeometry* convertToPoint( bool destMultipart );
660  QgsGeometry* convertToLine( bool destMultipart );
662  QgsGeometry* convertToPolygon( bool destMultipart );
663 }; // class QgsGeometry
664 
666 
667 class CORE_EXPORT QgsWkbPtr
668 {
669  mutable unsigned char *mP;
670 
671  public:
672  QgsWkbPtr( unsigned char *p ) { mP = p; }
673 
674  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
675  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
676  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
677  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
678  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
679 
680  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
681  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
682  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
683  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
684  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
685 
686  inline void operator+=( int n ) { mP += n; }
687 
688  inline operator unsigned char *() const { return mP; }
689 };
690 
691 class CORE_EXPORT QgsConstWkbPtr
692 {
693  mutable unsigned char *mP;
694 
695  public:
696  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
697 
698  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
699  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
700  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
701  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
702  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
703 
704  inline void operator+=( int n ) { mP += n; }
705 
706  inline operator const unsigned char *() const { return mP; }
707 };
708 
709 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsWkbPtr(unsigned char *p)
Definition: qgsgeometry.h:672
Error(QString m)
Definition: qgsgeometry.h:496
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:701
QgsPoint where()
Definition: qgsgeometry.h:500
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:33
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:698
const QgsWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:675
Definition: qgsgeometry.h:69
void operator+=(int n)
Definition: qgsgeometry.h:686
GeometryType
Definition: qgis.h:155
WkbType
Used for symbology operations.
Definition: qgis.h:53
bool hasWhere()
Definition: qgsgeometry.h:501
Definition: qgis.h:160
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:700
QgsWkbPtr & operator<<(const int &v)
Definition: qgsgeometry.h:681
const double DEFAULT_SEGMENT_EPSILON
default snapping tolerance for segments (
Definition: qgis.h:414
const QgsConstWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:702
Error()
Definition: qgsgeometry.h:495
QgsWkbPtr & operator<<(const double &v)
Definition: qgsgeometry.h:680
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:684
const QgsWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:674
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:677
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:678
Definition: qgsgeometry.h:667
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:699
QgsWkbPtr & operator<<(const unsigned int &v)
Definition: qgsgeometry.h:682
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:52
Definition: qgsgeometry.h:489
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:676
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:704
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.
Definition: qgsvectorlayer.h:470
Definition: qgsgeometry.h:691
Q_DECLARE_METATYPE(QgsGeometry)
QgsWkbPtr & operator<<(const char &v)
Definition: qgsgeometry.h:683
QgsConstWkbPtr(const unsigned char *p)
Definition: qgsgeometry.h:696
Error(QString m, QgsPoint p)
Definition: qgsgeometry.h:497
QString what()
Definition: qgsgeometry.h:499