QGIS API Documentation  2.2.0-Valmiera
 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 
400  /* Accessor functions for getting geometry data */
401 
404  QgsPoint asPoint() const;
405 
408  QgsPolyline asPolyline() const;
409 
412  QgsPolygon asPolygon() const;
413 
416  QgsMultiPoint asMultiPoint() const;
417 
420  QgsMultiPolyline asMultiPolyline() const;
421 
424  QgsMultiPolygon asMultiPolygon() const;
425 
428  QList<QgsGeometry*> asGeometryCollection() const;
429 
434  bool deleteRing( int ringNum, int partNum = 0 );
435 
439  bool deletePart( int partNum );
440 
444  bool convertToMultiType();
445 
454  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
455 
456  class Error
457  {
458  QString message;
461  public:
462  Error() : message( "none" ), hasLocation( false ) {}
463  Error( QString m ) : message( m ), hasLocation( false ) {}
464  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
465 
466  QString what() { return message; };
467  QgsPoint where() { return location; }
468  bool hasWhere() { return hasLocation; }
469  };
470 
475  void validateGeometry( QList<Error> &errors );
476 
477  private:
478  // Private variables
479 
480  // All of these are mutable since there may be on-the-fly
481  // conversions between WKB, GEOS and Wkt;
482  // However the intent is the const functions do not
483  // semantically change the value that this object represents.
484 
488  mutable unsigned char * mGeometry;
489 
491  mutable size_t mGeometrySize;
492 
494  mutable GEOSGeometry* mGeos;
495 
497  mutable bool mDirtyWkb;
498 
500  mutable bool mDirtyGeos;
501 
502 
503  // Private functions
504 
508  bool exportWkbToGeos() const;
509 
513  bool exportGeosToWkb() const;
514 
528  bool insertVertex( double x, double y,
529  int beforeVertex,
530  const GEOSCoordSequence* old_sequence,
531  GEOSCoordSequence** new_sequence );
532 
538  void translateVertex( QgsWkbPtr &wkbPtr, double dx, double dy, bool hasZValue );
539 
544  void transformVertex( QgsWkbPtr &wkbPtr, const QgsCoordinateTransform& ct, bool hasZValue );
545 
546  //helper functions for geometry splitting
547 
552  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
555  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
558  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
559 
565  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
566 
572  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
573 
576  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
577 
580  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
581 
586  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
587 
589  static bool geomInDegrees( const GEOSGeometry* geom );
590 
592  int numberOfGeometries( GEOSGeometry* g ) const;
593 
594  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
595 
597  QgsPoint asPoint( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
598 
600  QgsPolyline asPolyline( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
601 
603  QgsPolygon asPolygon( QgsConstWkbPtr &wkbPtr, bool hasZValue ) const;
604 
605  static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ),
606  const QgsGeometry* a, const QgsGeometry* b );
607 
609  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
610 
611  static inline bool moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &y, int atVertex, bool hasZValue, int &pointIndex, bool isRing );
612  static inline bool deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int atVertex, bool hasZValue, int &pointIndex, bool isRing, bool lastItem );
613  static inline bool insertVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int beforeVertex, const double &x, const double &y, bool hasZValue, int &pointIndex, bool isRing );
614 }; // class QgsGeometry
615 
617 
618 class CORE_EXPORT QgsWkbPtr
619 {
620  mutable unsigned char *mP;
621 
622  public:
623  QgsWkbPtr( unsigned char *p ) { mP = p; }
624 
625  inline const QgsWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
626  inline const QgsWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
627  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
628  inline const QgsWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
629  inline const QgsWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
630 
631  inline QgsWkbPtr &operator<<( const double &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
632  inline QgsWkbPtr &operator<<( const int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
633  inline QgsWkbPtr &operator<<( const unsigned int &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
634  inline QgsWkbPtr &operator<<( const char &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
635  inline QgsWkbPtr &operator<<( const QGis::WkbType &v ) { memcpy( mP, &v, sizeof( v ) ); mP += sizeof( v ); return *this; }
636 
637  inline void operator+=( int n ) { mP += n; }
638 
639  inline operator unsigned char *() const { return mP; }
640 };
641 
642 class CORE_EXPORT QgsConstWkbPtr
643 {
644  mutable unsigned char *mP;
645 
646  public:
647  QgsConstWkbPtr( const unsigned char *p ) { mP = ( unsigned char * ) p; }
648 
649  inline const QgsConstWkbPtr &operator>>( double &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
650  inline const QgsConstWkbPtr &operator>>( int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
651  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
652  inline const QgsConstWkbPtr &operator>>( char &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
653  inline const QgsConstWkbPtr &operator>>( QGis::WkbType &v ) const { memcpy( &v, mP, sizeof( v ) ); mP += sizeof( v ); return *this; }
654 
655  inline void operator+=( int n ) { mP += n; }
656 
657  inline operator const unsigned char *() const { return mP; }
658 };
659 
660 #endif