QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 #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 
359  QgsGeometry* simplify( double tolerance );
360 
364  QgsGeometry* centroid();
365 
367  QgsGeometry* convexHull();
368 
369  /* Return interpolated point on line at distance
370  * @note added in 1.9
371  */
372  QgsGeometry* interpolate( double distance );
373 
375  QgsGeometry* intersection( QgsGeometry* geometry );
376 
380  QgsGeometry* combine( QgsGeometry* geometry );
381 
383  QgsGeometry* difference( QgsGeometry* geometry );
384 
386  QgsGeometry* symDifference( QgsGeometry* geometry );
387 
391  QString exportToWkt() const;
392 
398  QString exportToGeoJSON() const;
399 
406  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false );
407 
408 
409  /* Accessor functions for getting geometry data */
410 
413  QgsPoint asPoint() const;
414 
417  QgsPolyline asPolyline() const;
418 
421  QgsPolygon asPolygon() const;
422 
425  QgsMultiPoint asMultiPoint() const;
426 
429  QgsMultiPolyline asMultiPolyline() const;
430 
433  QgsMultiPolygon asMultiPolygon() const;
434 
437  QList<QgsGeometry*> asGeometryCollection() const;
438 
443  bool deleteRing( int ringNum, int partNum = 0 );
444 
448  bool deletePart( int partNum );
449 
453  bool convertToMultiType();
454 
463  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
464 
465  class Error
466  {
467  QString message;
470  public:
471  Error() : message( "none" ), hasLocation( false ) {}
472  Error( QString m ) : message( m ), hasLocation( false ) {}
473  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
474 
475  QString what() { return message; };
476  QgsPoint where() { return location; }
477  bool hasWhere() { return hasLocation; }
478  };
479 
484  void validateGeometry( QList<Error> &errors );
485 
486  private:
487  // Private variables
488 
489  // All of these are mutable since there may be on-the-fly
490  // conversions between WKB, GEOS and Wkt;
491  // However the intent is the const functions do not
492  // semantically change the value that this object represents.
493 
497  mutable unsigned char * mGeometry;
498 
500  mutable size_t mGeometrySize;
501 
503  mutable GEOSGeometry* mGeos;
504 
506  mutable bool mDirtyWkb;
507 
509  mutable bool mDirtyGeos;
510 
511 
512  // Private functions
513 
517  bool exportWkbToGeos() const;
518 
522  bool exportGeosToWkb() const;
523 
537  bool insertVertex( double x, double y,
538  int beforeVertex,
539  const GEOSCoordSequence* old_sequence,
540  GEOSCoordSequence** new_sequence );
541 
547  void translateVertex( QgsWkbPtr &wkbPtr, double dx, double dy, bool hasZValue );
548 
553  void transformVertex( QgsWkbPtr &wkbPtr, const QgsCoordinateTransform& ct, bool hasZValue );
554 
555  //helper functions for geometry splitting
556 
561  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
564  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
567  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
568 
574  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
575 
581  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
582 
585  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
586 
589  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
590 
595  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
596 
598  static bool geomInDegrees( const GEOSGeometry* geom );
599 
601  int numberOfGeometries( GEOSGeometry* g ) const;
602 
603  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
604 
606  QgsPoint asPoint( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
607 
609  QgsPolyline asPolyline( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
610 
612  QgsPolygon asPolygon( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
613 
614  static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ),
615  const QgsGeometry* a, const QgsGeometry* b );
616 
618  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
619 
620  static inline bool moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &y, int atVertex, bool hasZValue, int &pointIndex, bool isRing );
621  static inline bool deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int atVertex, bool hasZValue, int &pointIndex, bool isRing, bool lastItem );
622  static inline bool insertVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int beforeVertex, const double &x, const double &y, bool hasZValue, int &pointIndex, bool isRing );
623 
625  QgsGeometry* convertToPoint( bool destMultipart );
627  QgsGeometry* convertToLine( bool destMultipart );
629  QgsGeometry* convertToPolygon( bool destMultipart );
630 }; // class QgsGeometry
631 
633 
634 class CORE_EXPORT QgsWkbPtr
635 {
636  mutable unsigned char *mP;
637 
638  public:
639  QgsWkbPtr( unsigned char *p ) { mP = p; }
640 
641  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
642  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
643  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
644  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
645  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
646 
647  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
648  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
649  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
650  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
651  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
652 
653  inline void operator+=( int n ) { mP += n; }
654 
655  inline operator unsigned char *() const { return mP; }
656 };
657 
658 class CORE_EXPORT QgsConstWkbPtr
659 {
660  mutable unsigned char *mP;
661 
662  public:
663  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
664 
665  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
666  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
667  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
668  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
669  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
670 
671  inline void operator+=( int n ) { mP += n; }
672 
673  inline operator const unsigned char *() const { return mP; }
674 };
675 
676 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsWkbPtr(unsigned char *p)
Definition: qgsgeometry.h:639
Error(QString m)
Definition: qgsgeometry.h:472
GeometryType
Definition: qgis.h:155
bool mDirtyWkb
If the geometry has been set since the last conversion to WKB.
Definition: qgsgeometry.h:506
unsigned char * mP
Definition: qgsgeometry.h:660
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:668
QgsPoint where()
Definition: qgsgeometry.h:476
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:38
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:665
const QgsWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:642
WkbType
Used for symbology operations.
Definition: qgis.h:53
void operator+=(int n)
Definition: qgsgeometry.h:653
size_t mGeometrySize
size of geometry
Definition: qgsgeometry.h:500
unsigned char * mP
Definition: qgsgeometry.h:636
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:667
QgsWkbPtr & operator<<(const int &v)
Definition: qgsgeometry.h:648
const double DEFAULT_SEGMENT_EPSILON
default snapping tolerance for segments (
Definition: qgis.h:414
const QgsConstWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:669
QgsWkbPtr & operator<<(const double &v)
Definition: qgsgeometry.h:647
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:651
const QgsWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:641
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:644
bool mDirtyGeos
If the geometry has been set since the last conversion to GEOS.
Definition: qgsgeometry.h:509
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:645
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:666
unsigned char * mGeometry
pointer to geometry in binary WKB format This is the class' native implementation ...
Definition: qgsgeometry.h:497
QgsWkbPtr & operator<<(const unsigned int &v)
Definition: qgsgeometry.h:649
Class for doing transforms between two map coordinate systems.
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:643
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:671
Represents a vector layer which manages a vector based data sets.
Q_DECLARE_METATYPE(QgsGeometry)
QgsWkbPtr & operator<<(const char &v)
Definition: qgsgeometry.h:650
QgsConstWkbPtr(const unsigned char *p)
Definition: qgsgeometry.h:663
Error(QString m, QgsPoint p)
Definition: qgsgeometry.h:473
GEOSGeometry * mGeos
cached GEOS version of this geometry
Definition: qgsgeometry.h:503