QGIS API Documentation  2.6.0-Brighton
 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 #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 
135  const GEOSGeometry* asGeos() const;
136 
138  QGis::WkbType wkbType() const;
139 
141  QGis::GeometryType type();
142 
144  bool isMultipart();
145 
147  bool isGeosEqual( QgsGeometry & );
148 
150  bool isGeosValid();
151 
153  bool isGeosEmpty();
154 
156  double area();
157 
159  double length();
160 
161  double distance( QgsGeometry& geom );
162 
167  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
168 
181  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
182 
194  bool insertVertex( double x, double y, int beforeVertex );
195 
202  bool moveVertex( double x, double y, int atVertex );
203 
214  bool deleteVertex( int atVertex );
215 
221  QgsPoint vertexAt( int atVertex );
222 
228  double sqrDistToVertexAt( QgsPoint& point, int atVertex );
229 
236  double closestVertexWithContext( const QgsPoint& point, int& atVertex );
237 
248  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON );
249 
253  int addRing( const QList<QgsPoint>& ring );
254 
258  int addPart( const QList<QgsPoint> &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
259 
265  int addPart( GEOSGeometry *newPart );
266 
272  int addPart( QgsGeometry *newPart );
273 
276  int translate( double dx, double dy );
277 
280  int transform( const QgsCoordinateTransform& ct );
281 
289  int splitGeometry( const QList<QgsPoint>& splitLine,
290  QList<QgsGeometry*>&newGeometries,
291  bool topological,
292  QList<QgsPoint> &topologyTestPoints );
293 
296  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
297 
301  int makeDifference( QgsGeometry* other );
302 
304  QgsRectangle boundingBox();
305 
307  bool intersects( const QgsRectangle& r ) const;
308 
310  bool intersects( const QgsGeometry* geometry ) const;
311 
313  bool contains( const QgsPoint* p ) const;
314 
316  bool contains( const QgsGeometry* geometry ) const;
317 
319  bool disjoint( const QgsGeometry* geometry ) const;
320 
322  bool equals( const QgsGeometry* geometry ) const;
323 
325  bool touches( const QgsGeometry* geometry ) const;
326 
328  bool overlaps( const QgsGeometry* geometry ) const;
329 
331  bool within( const QgsGeometry* geometry ) const;
332 
334  bool crosses( const QgsGeometry* geometry ) const;
335 
338  QgsGeometry* buffer( double distance, int segments );
339 
349  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit );
350 
356  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit );
357 
359  QgsGeometry* simplify( double tolerance );
360 
364  QgsGeometry* centroid();
365 
367  QgsGeometry* pointOnSurface();
368 
371 
372  /* Return interpolated point on line at distance */
373  QgsGeometry* interpolate( double distance );
374 
376  QgsGeometry* intersection( QgsGeometry* geometry );
377 
381  QgsGeometry* combine( QgsGeometry* geometry );
382 
384  QgsGeometry* difference( QgsGeometry* geometry );
385 
387  QgsGeometry* symDifference( QgsGeometry* geometry );
388 
393  QString exportToWkt( const int &precision = 17 ) const;
394 
399  QString exportToGeoJSON( const int &precision = 17 ) const;
400 
407  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false );
408 
409 
410  /* Accessor functions for getting geometry data */
411 
414  QgsPoint asPoint() const;
415 
418  QgsPolyline asPolyline() const;
419 
422  QgsPolygon asPolygon() const;
423 
426  QgsMultiPoint asMultiPoint() const;
427 
430  QgsMultiPolyline asMultiPolyline() const;
431 
434  QgsMultiPolygon asMultiPolygon() const;
435 
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 
462  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
463 
464  class Error
465  {
466  QString message;
467  QgsPoint location;
468  bool hasLocation;
469  public:
470  Error() : message( "none" ), hasLocation( false ) {}
471  Error( QString m ) : message( m ), hasLocation( false ) {}
472  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
473 
474  QString what() { return message; };
475  QgsPoint where() { return location; }
476  bool hasWhere() { return hasLocation; }
477  };
478 
480  void validateGeometry( QList<Error> &errors );
481 
486  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
487 
488  private:
489  // Private variables
490 
491  // All of these are mutable since there may be on-the-fly
492  // conversions between WKB, GEOS and Wkt;
493  // However the intent is the const functions do not
494  // semantically change the value that this object represents.
495 
499  mutable unsigned char * mGeometry;
500 
502  mutable size_t mGeometrySize;
503 
505  mutable GEOSGeometry* mGeos;
506 
508  mutable bool mDirtyWkb;
509 
511  mutable bool mDirtyGeos;
512 
513 
514  // Private functions
515 
519  bool exportWkbToGeos() const;
520 
524  bool exportGeosToWkb() const;
525 
539  bool insertVertex( double x, double y,
540  int beforeVertex,
541  const GEOSCoordSequence* old_sequence,
542  GEOSCoordSequence** new_sequence );
543 
549  void translateVertex( QgsWkbPtr &wkbPtr, double dx, double dy, bool hasZValue );
550 
555  void transformVertex( QgsWkbPtr &wkbPtr, const QgsCoordinateTransform& ct, bool hasZValue );
556 
557  //helper functions for geometry splitting
558 
563  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
566  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
568  GEOSGeometry* linePointDifference( GEOSGeometry* GEOSsplitPoint );
569 
572  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
573 
579  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
580 
586  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
587 
590  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
591 
594  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
595 
600  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
601 
603  static int geomDigits( const GEOSGeometry* geom );
604 
606  int numberOfGeometries( GEOSGeometry* g ) const;
607 
608  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
609 
611  QgsPoint asPoint( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
612 
614  QgsPolyline asPolyline( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
615 
617  QgsPolygon asPolygon( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
618 
619  static bool geosRelOp( char( *op )( GEOSContextHandle_t handle, const GEOSGeometry*, const GEOSGeometry * ),
620  const QgsGeometry* a, const QgsGeometry* b );
621 
623  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
624 
625  static inline bool moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &y, int atVertex, bool hasZValue, int &pointIndex, bool isRing );
626  static inline int deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int atVertex, bool hasZValue, int &pointIndex, bool isRing, bool lastItem );
627  static inline bool insertVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int beforeVertex, const double &x, const double &y, bool hasZValue, int &pointIndex, bool isRing );
628 
630  QgsGeometry* convertToPoint( bool destMultipart );
632  QgsGeometry* convertToLine( bool destMultipart );
634  QgsGeometry* convertToPolygon( bool destMultipart );
635 }; // class QgsGeometry
636 
638 
639 class CORE_EXPORT QgsWkbPtr
640 {
641  mutable unsigned char *mP;
642 
643  public:
644  QgsWkbPtr( unsigned char *p ) { mP = p; }
645 
646  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
647  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
648  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
649  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
650  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
651 
652  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
653  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
654  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
655  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
656  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
657 
658  inline void operator+=( int n ) { mP += n; }
659 
660  inline operator unsigned char *() const { return mP; }
661 };
662 
663 class CORE_EXPORT QgsConstWkbPtr
664 {
665  mutable unsigned char *mP;
666 
667  public:
668  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
669 
670  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
671  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
672  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
673  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
674  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
675 
676  inline void operator+=( int n ) { mP += n; }
677 
678  inline operator const unsigned char *() const { return mP; }
679 };
680 
681 #endif