QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 #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 
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 
413  QString exportToWkt( const int &precision = 17 ) const;
414 
421  QString exportToGeoJSON( const int &precision = 17 ) const;
422 
429  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false );
430 
431 
432  /* Accessor functions for getting geometry data */
433 
436  QgsPoint asPoint() const;
437 
440  QgsPolyline asPolyline() const;
441 
444  QgsPolygon asPolygon() const;
445 
448  QgsMultiPoint asMultiPoint() const;
449 
452  QgsMultiPolyline asMultiPolyline() const;
453 
456  QgsMultiPolygon asMultiPolygon() const;
457 
460  QList<QgsGeometry*> asGeometryCollection() const;
461 
466  bool deleteRing( int ringNum, int partNum = 0 );
467 
471  bool deletePart( int partNum );
472 
476  bool convertToMultiType();
477 
486  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
487 
488  class Error
489  {
490  QString message;
493  public:
494  Error() : message( "none" ), hasLocation( false ) {}
495  Error( QString m ) : message( m ), hasLocation( false ) {}
496  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
497 
498  QString what() { return message; };
499  QgsPoint where() { return location; }
500  bool hasWhere() { return hasLocation; }
501  };
502 
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 )( 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 }; // class QgsGeometry
663 
665 
666 class CORE_EXPORT QgsWkbPtr
667 {
668  mutable unsigned char *mP;
669 
670  public:
671  QgsWkbPtr( unsigned char *p ) { mP = p; }
672 
673  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
674  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
675  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
676  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
677  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
678 
679  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
680  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
681  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
682  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
683  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
684 
685  inline void operator+=( int n ) { mP += n; }
686 
687  inline operator unsigned char *() const { return mP; }
688 };
689 
690 class CORE_EXPORT QgsConstWkbPtr
691 {
692  mutable unsigned char *mP;
693 
694  public:
695  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
696 
697  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
698  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
699  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
700  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
701  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
702 
703  inline void operator+=( int n ) { mP += n; }
704 
705  inline operator const unsigned char *() const { return mP; }
706 };
707 
708 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsWkbPtr(unsigned char *p)
Definition: qgsgeometry.h:671
Error(QString m)
Definition: qgsgeometry.h:495
bool mDirtyWkb
If the geometry has been set since the last conversion to WKB.
Definition: qgsgeometry.h:535
unsigned char * mP
Definition: qgsgeometry.h:692
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgsgeometry.h:700
QgsPoint where()
Definition: qgsgeometry.h:499
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:38
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:697
const QgsWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:674
void operator+=(int n)
Definition: qgsgeometry.h:685
GeometryType
Definition: qgis.h:155
WkbType
Used for symbology operations.
Definition: qgis.h:53
size_t mGeometrySize
size of geometry
Definition: qgsgeometry.h:529
unsigned char * mP
Definition: qgsgeometry.h:668
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:699
QgsWkbPtr & operator<<(const int &v)
Definition: qgsgeometry.h:680
const double DEFAULT_SEGMENT_EPSILON
default snapping tolerance for segments (
Definition: qgis.h:414
const QgsConstWkbPtr & operator>>(QGis::WkbType &v) const
Definition: qgsgeometry.h:701
QgsWkbPtr & operator<<(const double &v)
Definition: qgsgeometry.h:679
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:683
const QgsWkbPtr & operator>>(double &v) const
Definition: qgsgeometry.h:673
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:676
bool mDirtyGeos
If the geometry has been set since the last conversion to GEOS.
Definition: qgsgeometry.h:538
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:677
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgsgeometry.h:698
unsigned char * mGeometry
pointer to geometry in binary WKB format This is the class' native implementation ...
Definition: qgsgeometry.h:526
QgsWkbPtr & operator<<(const unsigned int &v)
Definition: qgsgeometry.h:681
Class for doing transforms between two map coordinate systems.
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgsgeometry.h:675
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:703
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:682
QgsConstWkbPtr(const unsigned char *p)
Definition: qgsgeometry.h:695
Error(QString m, QgsPoint p)
Definition: qgsgeometry.h:496
GEOSGeometry * mGeos
cached GEOS version of this geometry
Definition: qgsgeometry.h:532