QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsgeometry.h"
22 #include <geos_c.h>
23 
24 class QgsLineStringV2;
25 class QgsPolygonV2;
26 
32 class CORE_EXPORT QgsGeos: public QgsGeometryEngine
33 {
34  public:
39  QgsGeos( const QgsAbstractGeometryV2* geometry, double precision = 0 );
40  ~QgsGeos();
41 
43  void geometryChanged() override;
44  void prepareGeometry() override;
45 
46  QgsAbstractGeometryV2* intersection( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
47  QgsAbstractGeometryV2* difference( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
48  QgsAbstractGeometryV2* combine( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
49  QgsAbstractGeometryV2* combine( const QList< QgsAbstractGeometryV2*>&, QString* errorMsg = nullptr ) const override;
50  QgsAbstractGeometryV2* symDifference( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
51  QgsAbstractGeometryV2* buffer( double distance, int segments, QString* errorMsg = nullptr ) const override;
52  QgsAbstractGeometryV2* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit, QString* errorMsg = nullptr ) const override;
53  QgsAbstractGeometryV2* simplify( double tolerance, QString* errorMsg = nullptr ) const override;
54  QgsAbstractGeometryV2* interpolate( double distance, QString* errorMsg = nullptr ) const override;
55  QgsAbstractGeometryV2* envelope( QString* errorMsg = nullptr ) const override;
56  bool centroid( QgsPointV2& pt, QString* errorMsg = nullptr ) const override;
57  bool pointOnSurface( QgsPointV2& pt, QString* errorMsg = nullptr ) const override;
58  QgsAbstractGeometryV2* convexHull( QString* errorMsg = nullptr ) const override;
59  double distance( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
60  bool intersects( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
61  bool touches( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
62  bool crosses( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
63  bool within( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
64  bool overlaps( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
65  bool contains( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
66  bool disjoint( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
67  QString relate( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
68  bool relatePattern( const QgsAbstractGeometryV2& geom, const QString& pattern, QString* errorMsg = nullptr ) const override;
69  double area( QString* errorMsg = nullptr ) const override;
70  double length( QString* errorMsg = nullptr ) const override;
71  bool isValid( QString* errorMsg = nullptr ) const override;
72  bool isEqual( const QgsAbstractGeometryV2& geom, QString* errorMsg = nullptr ) const override;
73  bool isEmpty( QString* errorMsg = nullptr ) const override;
74 
82  int splitGeometry( const QgsLineStringV2& splitLine,
83  QList<QgsAbstractGeometryV2*>& newGeometries,
84  bool topological,
85  QgsPointSequenceV2 &topologyTestPoints,
86  QString* errorMsg = nullptr ) const override;
87 
88  QgsAbstractGeometryV2* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit, QString* errorMsg = nullptr ) const override;
89  QgsAbstractGeometryV2* reshapeGeometry( const QgsLineStringV2& reshapeWithLine, int* errorCode, QString* errorMsg = nullptr ) const;
90 
99  QgsGeometry mergeLines( QString* errorMsg = nullptr ) const;
100 
105  QgsGeometry closestPoint( const QgsGeometry& other, QString* errorMsg = nullptr ) const;
106 
111  QgsGeometry shortestLine( const QgsGeometry& other, QString* errorMsg = nullptr ) const;
112 
122  double lineLocatePoint( const QgsPointV2& point, QString* errorMsg = nullptr ) const;
123 
127  static QgsAbstractGeometryV2* fromGeos( const GEOSGeometry* geos );
128  static QgsPolygonV2* fromGeosPolygon( const GEOSGeometry* geos );
129  static GEOSGeometry* asGeos( const QgsAbstractGeometryV2* geom , double precision = 0 );
130  static QgsPointV2 coordSeqPoint( const GEOSCoordSequence* cs, int i, bool hasZ, bool hasM );
131 
132  static GEOSContextHandle_t getGEOSHandler();
133 
134  private:
135  mutable GEOSGeometry* mGeos;
136  const GEOSPreparedGeometry* mGeosPrepared;
137  double mPrecision;
138 
139  enum Overlay
140  {
141  INTERSECTION,
142  DIFFERENCE,
143  UNION,
144  SYMDIFFERENCE
145  };
146 
147  enum Relation
148  {
149  INTERSECTS,
150  TOUCHES,
151  CROSSES,
152  WITHIN,
153  OVERLAPS,
154  CONTAINS,
155  DISJOINT
156  };
157 
158  //geos util functions
159  void cacheGeos() const;
160  QgsAbstractGeometryV2* overlay( const QgsAbstractGeometryV2& geom, Overlay op, QString* errorMsg = nullptr ) const;
161  bool relation( const QgsAbstractGeometryV2& geom, Relation r, QString* errorMsg = nullptr ) const;
162  static GEOSCoordSequence* createCoordinateSequence( const QgsCurveV2* curve , double precision, bool forceClose = false );
163  static QgsLineStringV2* sequenceToLinestring( const GEOSGeometry* geos, bool hasZ, bool hasM );
164  static int numberOfGeometries( GEOSGeometry* g );
165  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *geom );
166  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult ) const;
167 
170  static GEOSGeometry* createGeosCollection( int typeId, const QVector<GEOSGeometry*>& geoms );
171 
172  static GEOSGeometry* createGeosPoint( const QgsAbstractGeometryV2* point, int coordDims , double precision );
173  static GEOSGeometry* createGeosLinestring( const QgsAbstractGeometryV2* curve, double precision );
174  static GEOSGeometry* createGeosPolygon( const QgsAbstractGeometryV2* poly, double precision );
175 
176  //utils for geometry split
177  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QgsPointSequenceV2 &testPoints, QString* errorMsg = nullptr ) const;
178  GEOSGeometry* linePointDifference( GEOSGeometry* GEOSsplitPoint ) const;
179  int splitLinearGeometry( GEOSGeometry* splitLine, QList<QgsAbstractGeometryV2*>& newGeometries ) const;
180  int splitPolygonGeometry( GEOSGeometry* splitLine, QList<QgsAbstractGeometryV2*>& newGeometries ) const;
181 
182  //utils for reshape
183  static GEOSGeometry* reshapeLine( const GEOSGeometry* line, const GEOSGeometry* reshapeLineGeos, double precision );
184  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos , double precision );
185  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
186  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
187  static int geomDigits( const GEOSGeometry* geom );
188 };
189 
191 
192 class GEOSException
193 {
194  public:
195  explicit GEOSException( const QString& theMsg )
196  {
197  if ( theMsg == "Unknown exception thrown" && lastMsg().isNull() )
198  {
199  msg = theMsg;
200  }
201  else
202  {
203  msg = theMsg;
204  lastMsg() = msg;
205  }
206  }
207 
208  // copy constructor
209  GEOSException( const GEOSException &rhs )
210  {
211  *this = rhs;
212  }
213 
214  ~GEOSException()
215  {
216  if ( lastMsg() == msg )
217  lastMsg() = QString::null;
218  }
219 
220  QString what()
221  {
222  return msg;
223  }
224 
225  private:
226  QString msg;
227  static QString& lastMsg() { static QString _lastMsg; return _lastMsg; }
228 };
229 
231 
232 #endif // QGSGEOS_H
virtual QgsAbstractGeometryV2 * symDifference(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual QgsAbstractGeometryV2 * difference(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual bool intersects(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual bool isEqual(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual double distance(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual int splitGeometry(const QgsLineStringV2 &splitLine, QList< QgsAbstractGeometryV2 *> &newGeometries, bool topological, QgsPointSequenceV2 &topologyTestPoints, QString *errorMsg=nullptr) const
virtual bool touches(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
Abstract base class for all geometries.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
virtual double length(QString *errorMsg=nullptr) const =0
virtual bool overlaps(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual void prepareGeometry()=0
virtual bool isEmpty(QString *errorMsg) const =0
virtual bool centroid(QgsPointV2 &pt, QString *errorMsg=nullptr) const =0
Polygon geometry type.
Definition: qgspolygonv2.h:29
virtual QgsAbstractGeometryV2 * buffer(double distance, int segments, QString *errorMsg=nullptr) const =0
Line string geometry type, with support for z-dimension and m-values.
virtual QgsAbstractGeometryV2 * offsetCurve(double distance, int segments, int joinStyle, double mitreLimit, QString *errorMsg=nullptr) const =0
virtual QString relate(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
Returns the Dimensional Extended 9 Intersection Model (DE-9IM) representation of the relationship bet...
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
virtual bool crosses(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual bool contains(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual double area(QString *errorMsg=nullptr) const =0
virtual QgsAbstractGeometryV2 * simplify(double tolerance, QString *errorMsg=nullptr) const =0
Does vector analysis using the geos library and handles import, export, exception handling*...
Definition: qgsgeos.h:32
virtual QgsAbstractGeometryV2 * convexHull(QString *errorMsg=nullptr) const =0
virtual void geometryChanged()=0
virtual QgsAbstractGeometryV2 * intersection(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual bool relatePattern(const QgsAbstractGeometryV2 &geom, const QString &pattern, QString *errorMsg=nullptr) const =0
Tests whether two geometries are related by a specified Dimensional Extended 9 Intersection Model (DE...
virtual bool isValid(QString *errorMsg=nullptr) const =0
virtual bool pointOnSurface(QgsPointV2 &pt, QString *errorMsg=nullptr) const =0
Contains geometry relation and modification algorithms.
virtual bool within(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
virtual bool disjoint(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
virtual QgsAbstractGeometryV2 * combine(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const =0
bool isNull(const QVariant &v)
virtual QgsAbstractGeometryV2 * interpolate(double distance, QString *errorMsg=nullptr) const =0
virtual QgsAbstractGeometryV2 * envelope(QString *errorMsg=nullptr) const =0