QGIS API Documentation  2.11.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 "qgsabstractgeometryv2.h"
33 #include "qgspoint.h"
34 #include "qgscoordinatetransform.h"
35 #include "qgsfeature.h"
36 
37 #include <QSet>
38 
39 class QgsGeometryEngine;
40 class QgsVectorLayer;
41 class QgsMapToPixel;
42 class QPainter;
44 
47 
50 
53 
56 
59 
60 class QgsRectangle;
61 
62 class QgsConstWkbPtr;
63 
64 struct QgsGeometryPrivate;
65 
75 class CORE_EXPORT QgsGeometry
76 {
77  public:
79  QgsGeometry();
80 
82  QgsGeometry( const QgsGeometry & );
83 
87  QgsGeometry & operator=( QgsGeometry const & rhs );
88 
94 
96  ~QgsGeometry();
97 
102  const QgsAbstractGeometryV2* geometry() const;
103 
108  void setGeometry( QgsAbstractGeometryV2* geometry );
109 
115  bool isEmpty() const;
116 
118  static QgsGeometry* fromWkt( QString wkt );
120  static QgsGeometry* fromPoint( const QgsPoint& point );
122  static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
124  static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
126  static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
128  static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
130  static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
132  static QgsGeometry* fromRect( const QgsRectangle& rect );
133 
139  void fromGeos( GEOSGeometry* geos );
144  void fromWkb( unsigned char * wkb, size_t length );
145 
151  const unsigned char* asWkb() const;
152 
157  size_t wkbSize() const;
158 
163  const GEOSGeometry* asGeos() const;
164 
168  QGis::WkbType wkbType() const;
169 
173  QGis::GeometryType type() const;
174 
176  bool isMultipart() const;
177 
181  bool isGeosEqual( const QgsGeometry& ) const;
182 
186  bool isGeosValid() const;
187 
191  bool isGeosEmpty() const;
192 
196  double area() const;
197 
201  double length() const;
202 
206  double distance( const QgsGeometry& geom ) const;
207 
212  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist ) const;
213 
226  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex ) const;
227 
239  bool insertVertex( double x, double y, int beforeVertex );
240 
247  bool moveVertex( double x, double y, int atVertex );
248 
255  bool moveVertex( const QgsPointV2& p, int atVertex );
256 
267  bool deleteVertex( int atVertex );
268 
274  QgsPoint vertexAt( int atVertex ) const;
275 
281  double sqrDistToVertexAt( QgsPoint& point, int atVertex ) const;
282 
289  double closestVertexWithContext( const QgsPoint& point, int& atVertex ) const;
290 
301  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
302 
306  int addRing( const QList<QgsPoint>& ring );
307 
311  int addRing( QgsCurveV2* ring );
312 
316  int addPart( const QList<QgsPoint> &points, QGis::GeometryType geomType = QGis::UnknownGeometry );
317 
321  int addPart( QgsAbstractGeometryV2* part );
322 
328  int addPart( GEOSGeometry *newPart );
329 
335  int addPart( const QgsGeometry *newPart );
336 
339  int translate( double dx, double dy );
340 
343  int transform( const QgsCoordinateTransform& ct );
344 
348  int transform( const QTransform& ct );
349 
355  int rotate( double rotation, const QgsPoint& center );
356 
363  int splitGeometry( const QList<QgsPoint>& splitLine,
364  QList<QgsGeometry*>&newGeometries,
365  bool topological,
366  QList<QgsPoint> &topologyTestPoints );
367 
371  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
372 
376  int makeDifference( const QgsGeometry* other );
377 
379  QgsRectangle boundingBox() const;
380 
382  bool intersects( const QgsRectangle& r ) const;
383 
385  bool intersects( const QgsGeometry* geometry ) const;
386 
388  bool contains( const QgsPoint* p ) const;
389 
392  bool contains( const QgsGeometry* geometry ) const;
393 
396  bool disjoint( const QgsGeometry* geometry ) const;
397 
400  bool equals( const QgsGeometry* geometry ) const;
401 
404  bool touches( const QgsGeometry* geometry ) const;
405 
408  bool overlaps( const QgsGeometry* geometry ) const;
409 
412  bool within( const QgsGeometry* geometry ) const;
413 
416  bool crosses( const QgsGeometry* geometry ) const;
417 
420  QgsGeometry* buffer( double distance, int segments ) const;
421 
431  QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const;
432 
438  QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const;
439 
441  QgsGeometry* simplify( double tolerance ) const;
442 
446  QgsGeometry* centroid() const;
447 
449  QgsGeometry* pointOnSurface() const;
450 
452  QgsGeometry* convexHull() const;
453 
454  /* Return interpolated point on line at distance
455  * @note added in 1.9
456  */
457  QgsGeometry* interpolate( double distance ) const;
458 
460  QgsGeometry* intersection( const QgsGeometry* geometry ) const;
461 
465  QgsGeometry* combine( const QgsGeometry* geometry ) const;
466 
468  QgsGeometry* difference( const QgsGeometry* geometry ) const;
469 
471  QgsGeometry* symDifference( const QgsGeometry* geometry ) const;
472 
477  QString exportToWkt( const int &precision = 17 ) const;
478 
485  QString exportToGeoJSON( const int &precision = 17 ) const;
486 
493  QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false ) const;
494 
495  /* Accessor functions for getting geometry data */
496 
499  QgsPoint asPoint() const;
500 
503  QgsPolyline asPolyline() const;
504 
507  QgsPolygon asPolygon() const;
508 
511  QgsMultiPoint asMultiPoint() const;
512 
515  QgsMultiPolyline asMultiPolyline() const;
516 
519  QgsMultiPolygon asMultiPolygon() const;
520 
523  QList<QgsGeometry*> asGeometryCollection() const;
524 
529  QPointF asQPointF() const;
530 
536  QPolygonF asQPolygonF() const;
537 
542  bool deleteRing( int ringNum, int partNum = 0 );
543 
547  bool deletePart( int partNum );
548 
557  bool convertToMultiType();
558 
567  int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) );
568 
569  class Error
570  {
571  QString message;
572  QgsPoint location;
573  bool hasLocation;
574  public:
575  Error() : message( "none" ), hasLocation( false ) {}
576  Error( QString m ) : message( m ), hasLocation( false ) {}
577  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
578 
579  QString what() { return message; }
580  QgsPoint where() { return location; }
581  bool hasWhere() { return hasLocation; }
582  };
583 
588  void validateGeometry( QList<Error> &errors );
589 
594  static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList );
595 
600  void convertToStraightSegment();
601 
607  bool requiresConversionToStraightSegments() const;
608 
613  void mapToPixel( const QgsMapToPixel& mtp );
614 
615  // not implemented for 2.10
616  /* Clips the geometry using the specified rectangle
617  * @param rect clip rectangle
618  * @note added in QGIS 2.10
619  */
620  // void clip( const QgsRectangle& rect );
621 
626  void draw( QPainter& p ) const;
627 
635  bool vertexIdFromVertexNr( int nr, QgsVertexId& id ) const;
636 
643  int vertexNrFromVertexId( const QgsVertexId& i ) const;
644 
649  static GEOSContextHandle_t getGEOSHandler();
650 
655  static QgsGeometry* fromQPointF( const QPointF& point );
656 
663  static QgsGeometry* fromQPolygonF( const QPolygonF& polygon );
664 
670  static QgsPolyline createPolylineFromQPolygonF( const QPolygonF &polygon );
671 
677  static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon );
678 
687  static bool compare( const QgsPolyline& p1, const QgsPolyline& p2, double epsilon = 4 * DBL_EPSILON );
688 
697  static bool compare( const QgsPolygon& p1, const QgsPolygon& p2, double epsilon = 4 * DBL_EPSILON );
698 
708  static bool compare( const QgsMultiPolygon& p1, const QgsMultiPolygon& p2, double epsilon = 4 * DBL_EPSILON );
709 
719  QgsGeometry* smooth( const unsigned int iterations = 1, const double offset = 0.25 ) const;
720 
722  QgsPolygon smoothPolygon( const QgsPolygon &polygon, const unsigned int iterations = 1, const double offset = 0.25 ) const;
724  QgsPolyline smoothLine( const QgsPolyline &polyline, const unsigned int iterations = 1, const double offset = 0.25 ) const;
725 
728  static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometryV2* geometry );
729 
730  private:
731 
732  QgsGeometryPrivate* d; //implicitely shared data pointer
733 
734  void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
735  void removeWkbGeos();
736 
737  //convert point list from v1 to v2
738  static void convertPointList( const QList<QgsPoint>& input, QList<QgsPointV2>& output );
739  static void convertPointList( const QList<QgsPointV2>& input, QList<QgsPoint>& output );
740  static void convertToPolyline( const QList<QgsPointV2>& input, QgsPolyline& output );
741  static void convertPolygon( const QgsPolygonV2& input, QgsPolygon& output );
742 
744  QgsGeometry* convertToPoint( bool destMultipart ) const;
746  QgsGeometry* convertToLine( bool destMultipart ) const;
748  QgsGeometry* convertToPolygon( bool destMultipart ) const;
749 }; // class QgsGeometry
750 
752 
754 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsGeometry& geometry );
756 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry );
757 
758 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Error(QString m)
Definition: qgsgeometry.h:576
GeometryType
Definition: qgis.h:155
QgsPoint where()
Definition: qgsgeometry.h:580
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:43
Abstract base class for all geometries.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:75
WkbType
Used for symbology operations.
Definition: qgis.h:53
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsGeometry &geometry)
Writes the geometry to stream out.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
Polygon geometry type.
Definition: qgspolygonv2.h:29
Utility class for identifying a unique vertex within a geometry.
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:439
Point geometry type.
Definition: qgspointv2.h:29
QVector< QgsPolygon > QgsMultiPolygon
a collection of QgsPolygons that share a common collection of attributes
Definition: qgsgeometry.h:58
QVector< QgsPoint > QgsMultiPoint
a collection of QgsPoints that share a common collection of attributes
Definition: qgsgeometry.h:52
QVector< QgsPolyline > QgsPolygon
polygon: first item of the list is outer ring, inner rings (if any) start from second item ...
Definition: qgsgeometry.h:49
A class to represent a point.
Definition: qgspoint.h:63
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry.
QVector< QgsPolyline > QgsMultiPolyline
a collection of QgsPolylines that share a common collection of attributes
Definition: qgsgeometry.h:55
Class for doing transforms between two map coordinate systems.
Contains geometry relation and modification algorithms.
double ANALYSIS_EXPORT leftOf(Point3D *thepoint, Point3D *p1, Point3D *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
Represents a vector layer which manages a vector based data sets.
Q_DECLARE_METATYPE(QgsGeometry)
Error(QString m, QgsPoint p)
Definition: qgsgeometry.h:577