QGIS API Documentation  2.0.1-Dufour
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 *
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
72 class CORE_EXPORT QgsGeometry
73 {
74  public:
76  QgsGeometry();
77
79  QgsGeometry( const QgsGeometry & );
80
84  QgsGeometry & operator=( QgsGeometry const & rhs );
85
87  ~QgsGeometry();
88
90  static QgsGeometry* fromWkt( QString wkt );
91
93  static QgsGeometry* fromPoint( const QgsPoint& point );
95  static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
97  static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
99  static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
101  static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
103  static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
105  static QgsGeometry* fromRect( const QgsRectangle& rect );
111  void fromGeos( GEOSGeometry* geos );
116  void fromWkb( unsigned char * wkb, size_t length );
117
122  const unsigned char* asWkb() const;
123
127  size_t wkbSize() const;
128
133  const GEOSGeometry* asGeos() const;
134
136  QGis::WkbType wkbType() const;
137
139  QGis::GeometryType type();
140
142  bool isMultipart();
143
147  bool isGeosEqual( QgsGeometry & );
148
152  bool isGeosValid();
153
157  bool isGeosEmpty();
158
162  double area();
163
167  double length();
168
169  double distance( QgsGeometry& geom );
170
175  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
176
177
190  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
191
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 );
269
272  int translate( double dx, double dy );
273
276  int transform( const QgsCoordinateTransform& ct );
277
285  int splitGeometry( const QList<QgsPoint>& splitLine,
286  QList<QgsGeometry*>&newGeometries,
287  bool topological,
288  QList<QgsPoint> &topologyTestPoints );
289
293  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
294
298  int makeDifference( QgsGeometry* other );
299
301  QgsRectangle boundingBox();
302
304  bool intersects( const QgsRectangle& r ) const;
305
307  bool intersects( const QgsGeometry* geometry ) const;
308
310  bool contains( const QgsPoint* p ) const;
311
314  bool contains( const QgsGeometry* geometry ) const;
315
318  bool disjoint( const QgsGeometry* geometry ) const;
319
322  bool equals( const QgsGeometry* geometry ) const;
323
326  bool touches( const QgsGeometry* geometry ) const;
327
330  bool overlaps( const QgsGeometry* geometry ) const;
331
334  bool within( const QgsGeometry* geometry ) const;
335
338  bool crosses( const QgsGeometry* geometry ) const;
339
342  QgsGeometry* buffer( double distance, int segments );
343
345  QgsGeometry* simplify( double tolerance );
346
350  QgsGeometry* centroid();
351
353  QgsGeometry* convexHull();
354
355  /* Return interpolated point on line at distance
356  * @note added in 1.9
357  */
358  QgsGeometry* interpolate( double distance );
359
361  QgsGeometry* intersection( QgsGeometry* geometry );
362
366  QgsGeometry* combine( QgsGeometry* geometry );
367
369  QgsGeometry* difference( QgsGeometry* geometry );
370
372  QgsGeometry* symDifference( QgsGeometry* geometry );
373
377  QString exportToWkt() const;
378
384  QString exportToGeoJSON() const;
385
386  /* Accessor functions for getting geometry data */
387
390  QgsPoint asPoint() const;
391
394  QgsPolyline asPolyline() const;
395
398  QgsPolygon asPolygon() const;
399
402  QgsMultiPoint asMultiPoint() const;
403
406  QgsMultiPolyline asMultiPolyline() const;
407
410  QgsMultiPolygon asMultiPolygon() const;
411
414  QList<QgsGeometry*> asGeometryCollection() const;
415
420  bool deleteRing( int ringNum, int partNum = 0 );
421
425  bool deletePart( int partNum );
426
430  bool convertToMultiType();
431
440  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
441
442  class Error
443  {
444  QString message;
447  public:
448  Error() : message( "none" ), hasLocation( false ) {}
449  Error( QString m ) : message( m ), hasLocation( false ) {}
450  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
451
452  QString what() { return message; };
453  QgsPoint where() { return location; }
454  bool hasWhere() { return hasLocation; }
455  };
456
461  void validateGeometry( QList<Error> &errors );
462
463  private:
464  // Private variables
465
466  // All of these are mutable since there may be on-the-fly
467  // conversions between WKB, GEOS and Wkt;
468  // However the intent is the const functions do not
469  // semantically change the value that this object represents.
470
474  mutable unsigned char * mGeometry;
475
477  mutable size_t mGeometrySize;
478
480  mutable GEOSGeometry* mGeos;
481
483  mutable bool mDirtyWkb;
484
486  mutable bool mDirtyGeos;
487
488
489  // Private functions
490
494  bool exportWkbToGeos() const;
495
499  bool exportGeosToWkb() const;
500
514  bool insertVertex( double x, double y,
515  int beforeVertex,
516  const GEOSCoordSequence* old_sequence,
517  GEOSCoordSequence** new_sequence );
518
524  void translateVertex( int& wkbPosition, double dx, double dy, bool hasZValue );
525
530  void transformVertex( int& wkbPosition, const QgsCoordinateTransform& ct, bool hasZValue );
531
532  //helper functions for geometry splitting
533
538  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
541  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
544  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
545
551  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
552
558  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
559
562  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
563
566  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
567
572  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
573
575  static bool geomInDegrees( const GEOSGeometry* geom );
576
578  int numberOfGeometries( GEOSGeometry* g ) const;
579
580  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
581
583  QgsPoint asPoint( unsigned char*& ptr, bool hasZValue ) const;
584
586  QgsPolyline asPolyline( unsigned char*& ptr, bool hasZValue ) const;
587
589  QgsPolygon asPolygon( unsigned char*& ptr, bool hasZValue ) const;
590
591  static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ),
592  const QgsGeometry* a, const QgsGeometry* b );
593
595  double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
596 }; // class QgsGeometry
597
599
600 #endif