QGIS API Documentation  2.12.0-Lyon
qgsgeos.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeos.h
3  -------------------------------------------------------------------
4 Date : 22 Sept 2014
5 Copyright : (C) 2014 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
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 QGSGEOS_H
17 #define QGSGEOS_H
18 
19 #include "qgsgeometryengine.h"
20 #include "qgspointv2.h"
21 #include <geos_c.h>
22 
23 class QgsLineStringV2;
24 class QgsPolygonV2;
25 
29 class CORE_EXPORT QgsGeos: public QgsGeometryEngine
30 {
31  public:
36  QgsGeos( const QgsAbstractGeometryV2* geometry, double precision = 0 );
37  ~QgsGeos();
38 
40  void geometryChanged() override;
41  void prepareGeometry() override;
42 
43  QgsAbstractGeometryV2* intersection( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
44  QgsAbstractGeometryV2* difference( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
45  QgsAbstractGeometryV2* combine( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
46  QgsAbstractGeometryV2* combine( const QList<const QgsAbstractGeometryV2*>&, QString* errorMsg = 0 ) const override;
47  QgsAbstractGeometryV2* symDifference( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
48  QgsAbstractGeometryV2* buffer( double distance, int segments, QString* errorMsg = 0 ) const override;
49  QgsAbstractGeometryV2* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit, QString* errorMsg = 0 ) const override;
50  QgsAbstractGeometryV2* simplify( double tolerance, QString* errorMsg = 0 ) const override;
51  QgsAbstractGeometryV2* interpolate( double distance, QString* errorMsg = 0 ) const override;
52  QgsAbstractGeometryV2* envelope( QString* errorMsg = 0 ) const override;
53  bool centroid( QgsPointV2& pt, QString* errorMsg = 0 ) const override;
54  bool pointOnSurface( QgsPointV2& pt, QString* errorMsg = 0 ) const override;
55  QgsAbstractGeometryV2* convexHull( QString* errorMsg = 0 ) const override;
56  double distance( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
57  bool intersects( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
58  bool touches( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
59  bool crosses( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
60  bool within( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
61  bool overlaps( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
62  bool contains( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
63  bool disjoint( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
64  QString relate( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
65  double area( QString* errorMsg = 0 ) const override;
66  double length( QString* errorMsg = 0 ) const override;
67  bool isValid( QString* errorMsg = 0 ) const override;
68  bool isEqual( const QgsAbstractGeometryV2& geom, QString* errorMsg = 0 ) const override;
69  bool isEmpty( QString* errorMsg = 0 ) const override;
70 
78  int splitGeometry( const QgsLineStringV2& splitLine,
79  QList<QgsAbstractGeometryV2*>& newGeometries,
80  bool topological,
81  QList<QgsPointV2> &topologyTestPoints,
82  QString* errorMsg = 0 ) const override;
83 
84  QgsAbstractGeometryV2* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit, QString* errorMsg = 0 ) const override;
85  QgsAbstractGeometryV2* reshapeGeometry( const QgsLineStringV2& reshapeWithLine, int* errorCode, QString* errorMsg = 0 ) const;
86 
90  static QgsAbstractGeometryV2* fromGeos( const GEOSGeometry* geos );
91  static QgsPolygonV2* fromGeosPolygon( const GEOSGeometry* geos );
92  static GEOSGeometry* asGeos( const QgsAbstractGeometryV2* geom , double precision = 0 );
93  static QgsPointV2 coordSeqPoint( const GEOSCoordSequence* cs, int i, bool hasZ, bool hasM );
94 
95  static GEOSContextHandle_t getGEOSHandler();
96 
97  private:
98  mutable GEOSGeometry* mGeos;
99  const GEOSPreparedGeometry* mGeosPrepared;
100  double mPrecision;
101 
102  enum Overlay
103  {
104  INTERSECTION,
105  DIFFERENCE,
106  UNION,
107  SYMDIFFERENCE
108  };
109 
110  enum Relation
111  {
112  INTERSECTS,
113  TOUCHES,
114  CROSSES,
115  WITHIN,
116  OVERLAPS,
117  CONTAINS,
118  DISJOINT
119  };
120 
121  //geos util functions
122  void cacheGeos() const;
123  QgsAbstractGeometryV2* overlay( const QgsAbstractGeometryV2& geom, Overlay op, QString* errorMsg = 0 ) const;
124  bool relation( const QgsAbstractGeometryV2& geom, Relation r, QString* errorMsg = 0 ) const;
125  static GEOSCoordSequence* createCoordinateSequence( const QgsCurveV2* curve , double precision );
126  static QgsLineStringV2* sequenceToLinestring( const GEOSGeometry* geos, bool hasZ, bool hasM );
127  static int numberOfGeometries( GEOSGeometry* g );
128  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
129  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult ) const;
130 
133  static GEOSGeometry* createGeosCollection( int typeId, const QVector<GEOSGeometry*>& geoms );
134 
135  static GEOSGeometry* createGeosPoint( const QgsAbstractGeometryV2* point, int coordDims , double precision );
136  static GEOSGeometry* createGeosLinestring( const QgsAbstractGeometryV2* curve, double precision );
137  static GEOSGeometry* createGeosPolygon( const QgsAbstractGeometryV2* poly, double precision );
138 
139  //utils for geometry split
140  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPointV2>& testPoints, QString* errorMsg = 0 ) const;
141  GEOSGeometry* linePointDifference( GEOSGeometry* GEOSsplitPoint ) const;
142  int splitLinearGeometry( GEOSGeometry* splitLine, QList<QgsAbstractGeometryV2*>& newGeometries ) const;
143  int splitPolygonGeometry( GEOSGeometry* splitLine, QList<QgsAbstractGeometryV2*>& newGeometries ) const;
144 
145  //utils for reshape
146  static GEOSGeometry* reshapeLine( const GEOSGeometry* line, const GEOSGeometry* reshapeLineGeos, double precision );
147  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos , double precision );
148  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
149  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
150  static int geomDigits( const GEOSGeometry* geom );
151 };
152 
154 
155 class GEOSException
156 {
157  public:
158  explicit GEOSException( const QString& theMsg )
159  {
160  if ( theMsg == "Unknown exception thrown" && lastMsg().isNull() )
161  {
162  msg = theMsg;
163  }
164  else
165  {
166  msg = theMsg;
167  lastMsg() = msg;
168  }
169  }
170 
171  // copy constructor
172  GEOSException( const GEOSException &rhs )
173  {
174  *this = rhs;
175  }
176 
177  ~GEOSException()
178  {
179  if ( lastMsg() == msg )
180  lastMsg() = QString::null;
181  }
182 
183  QString what()
184  {
185  return msg;
186  }
187 
188  private:
189  QString msg;
190  static QString& lastMsg() { static QString _lastMsg; return _lastMsg; }
191 };
192 
194 
195 #endif // QGSGEOS_H
virtual bool centroid(QgsPointV2 &pt, QString *errorMsg=0) const =0
virtual QgsAbstractGeometryV2 * buffer(double distance, int segments, QString *errorMsg=0) const =0
virtual QgsAbstractGeometryV2 * envelope(QString *errorMsg=0) const =0
virtual bool within(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual int splitGeometry(const QgsLineStringV2 &splitLine, QList< QgsAbstractGeometryV2 * > &newGeometries, bool topological, QList< QgsPointV2 > &topologyTestPoints, QString *errorMsg=0) const
virtual QString relate(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
Returns the Dimensional Extended 9 Intersection Model (DE-9IM) representation of the relationship bet...
Abstract base class for all geometries.
virtual bool pointOnSurface(QgsPointV2 &pt, QString *errorMsg=0) const =0
virtual double area(QString *errorMsg=0) const =0
virtual bool disjoint(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual void prepareGeometry()=0
virtual QgsAbstractGeometryV2 * intersection(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual bool isEmpty(QString *errorMsg) const =0
virtual QgsAbstractGeometryV2 * offsetCurve(double distance, int segments, int joinStyle, double mitreLimit, QString *errorMsg=0) const =0
virtual QgsAbstractGeometryV2 * simplify(double tolerance, QString *errorMsg=0) const =0
Polygon geometry type.
Definition: qgspolygonv2.h:29
virtual bool isValid(QString *errorMsg=0) const =0
virtual QgsAbstractGeometryV2 * combine(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual QgsAbstractGeometryV2 * difference(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual double length(QString *errorMsg=0) const =0
Line string geometry type.
Point geometry type.
Definition: qgspointv2.h:29
virtual QgsAbstractGeometryV2 * convexHull(QString *errorMsg=0) const =0
Does vector analysis using the geos library and handles import, export, exception handling*...
Definition: qgsgeos.h:29
virtual QgsAbstractGeometryV2 * symDifference(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual QgsAbstractGeometryV2 * interpolate(double distance, QString *errorMsg=0) const =0
virtual bool crosses(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual bool touches(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual bool isEqual(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual void geometryChanged()=0
Contains geometry relation and modification algorithms.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
virtual bool intersects(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual bool overlaps(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
virtual double distance(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0
bool isNull(const QVariant &v)
virtual bool contains(const QgsAbstractGeometryV2 &geom, QString *errorMsg=0) const =0