QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsgeometrycheckerutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  * qgsgeometrycheckerutils.h *
3  * ------------------- *
4  * copyright : (C) 2014 by Sandro Mani / Sourcepole AG *
5  * email : [email protected] *
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGS_GEOMETRYCHECKERUTILS_H
18 #define QGS_GEOMETRYCHECKERUTILS_H
19 
20 #include "qgis_analysis.h"
21 #include "qgsfeature.h"
23 #include "geometry/qgspoint.h"
25 #include <qmath.h>
26 
27 class QgsGeometryEngine;
28 class QgsFeaturePool;
29 class QgsFeedback;
30 
39 class ANALYSIS_EXPORT QgsGeometryCheckerUtils
40 {
41  public:
42 
51  class ANALYSIS_EXPORT LayerFeature
52  {
53  public:
54 
61  LayerFeature( const QgsFeaturePool *pool, const QgsFeature &feature, const QgsGeometryCheckContext *context, bool useMapCrs );
62 
67  QgsFeature feature() const;
68 
72  QPointer<QgsVectorLayer> layer() const SIP_SKIP;
73 
77  QString layerId() const;
78 
84  QgsGeometry geometry() const;
85 
89  QString id() const;
90  bool operator==( const QgsGeometryCheckerUtils::LayerFeature &other ) const;
91  bool operator!=( const QgsGeometryCheckerUtils::LayerFeature &other ) const;
92 
96  bool useMapCrs() const;
97 
98  private:
99  const QgsFeaturePool *mFeaturePool;
100  QgsFeature mFeature;
101  QgsGeometry mGeometry;
102  bool mMapCrs;
103  };
104 
112  class ANALYSIS_EXPORT LayerFeatures
113  {
114  public:
115 #ifndef SIP_RUN
116 
120  LayerFeatures( const QMap<QString, QgsFeaturePool *> &featurePools,
121  const QMap<QString, QgsFeatureIds> &featureIds,
122  const QList<QgsWkbTypes::GeometryType> &geometryTypes,
123  QgsFeedback *feedback,
124  const QgsGeometryCheckContext *context,
125  bool useMapCrs = false );
126 
130  LayerFeatures( const QMap<QString, QgsFeaturePool *> &featurePools,
131  const QList<QString> &layerIds, const QgsRectangle &extent,
132  const QList<QgsWkbTypes::GeometryType> &geometryTypes,
133  const QgsGeometryCheckContext *context );
134 
142  class iterator
143  {
144  public:
145 
149  iterator( const QStringList::const_iterator &layerIt, const LayerFeatures *parent );
150 
154  iterator( const iterator &rh );
155  ~iterator();
156 
161  const iterator &operator++();
162 
167  iterator operator++( int n );
168 
173  bool operator!=( const iterator &other );
174 
175  private:
176  bool nextLayerFeature( bool begin );
177  bool nextLayer( bool begin );
178  bool nextFeature( bool begin );
179  QList<QString>::const_iterator mLayerIt;
180  QgsFeatureIds::const_iterator mFeatureIt;
181  const LayerFeatures *mParent = nullptr;
182  std::unique_ptr<QgsGeometryCheckerUtils::LayerFeature> mCurrentFeature;
183  };
184 
188  iterator begin() const;
189 
193  iterator end() const;
194 
195 #endif
196 
197  private:
198 #ifdef SIP_RUN
199  LayerFeatures();
200 #endif
201  QMap<QString, QgsFeaturePool *> mFeaturePools;
202  QMap<QString, QgsFeatureIds> mFeatureIds;
203  QList<QString> mLayerIds;
204  QgsRectangle mExtent;
205  QList<QgsWkbTypes::GeometryType> mGeometryTypes;
206  QgsFeedback *mFeedback = nullptr;
207  const QgsGeometryCheckContext *mContext = nullptr;
208  bool mUseMapCrs = true;
209  };
210 
211 #ifndef SIP_RUN
212 
213  static std::unique_ptr<QgsGeometryEngine> createGeomEngine( const QgsAbstractGeometry *geometry, double tolerance );
214 
215  static QgsAbstractGeometry *getGeomPart( QgsAbstractGeometry *geom, int partIdx );
216  static const QgsAbstractGeometry *getGeomPart( const QgsAbstractGeometry *geom, int partIdx );
217 
218  static QList <const QgsLineString *> polygonRings( const QgsPolygon *polygon );
219 
220  static void filter1DTypes( QgsAbstractGeometry *geom );
221 
226  static inline int polyLineSize( const QgsAbstractGeometry *geom, int iPart, int iRing, bool *isClosed = nullptr )
227  {
228  if ( !geom->isEmpty() )
229  {
230  int nVerts = geom->vertexCount( iPart, iRing );
231  QgsPoint front = geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) );
232  QgsPoint back = geom->vertexAt( QgsVertexId( iPart, iRing, nVerts - 1 ) );
233  bool closed = back == front;
234  if ( isClosed )
235  *isClosed = closed;
236  return closed ? nVerts - 1 : nVerts;
237  }
238  else
239  {
240  if ( isClosed )
241  *isClosed = true;
242  return 0;
243  }
244  }
245 
246  static bool pointOnLine( const QgsPoint &p, const QgsLineString *line, double tol, bool excludeExtremities = false );
247 
248  static QList<QgsPoint> lineIntersections( const QgsLineString *line1, const QgsLineString *line2, double tol );
249 
250  static double sharedEdgeLength( const QgsAbstractGeometry *geom1, const QgsAbstractGeometry *geom2, double tol );
251 
259  static inline bool pointsFuzzyEqual( const QgsPointXY &p1, const QgsPointXY &p2, double tol )
260  {
261  double dx = p1.x() - p2.x(), dy = p1.y() - p2.y();
262  return ( dx * dx + dy * dy ) < tol * tol;
263  }
264 
265  static inline bool canDeleteVertex( const QgsAbstractGeometry *geom, int iPart, int iRing )
266  {
267  int nVerts = geom->vertexCount( iPart, iRing );
268  QgsPoint front = geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) );
269  QgsPoint back = geom->vertexAt( QgsVertexId( iPart, iRing, nVerts - 1 ) );
270  bool closed = back == front;
271  return closed ? nVerts > 4 : nVerts > 2;
272  }
273 
274 #endif
275 
276 }; // QgsGeometryCheckerUtils
277 
278 #endif // QGS_GEOMETRYCHECKERUTILS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
static bool pointsFuzzyEqual(const QgsPointXY &p1, const QgsPointXY &p2, double tol)
Determine whether two points are equal up to the specified tolerance.
virtual bool isEmpty() const
Returns true if the geometry is empty.
Contains utilities required for geometry checks.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double y
Definition: qgspointxy.h:48
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A class to represent a 2D point.
Definition: qgspointxy.h:43
Base configuration for geometry checks.
Contains a set of layers and feature ids in those layers to pass to a geometry check.
static int polyLineSize(const QgsAbstractGeometry *geom, int iPart, int iRing, bool *isClosed=nullptr)
Returns the number of points in a polyline, accounting for duplicate start and end point if the polyl...
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
Utility class for identifying a unique vertex within a geometry.
An iterator over all features in a QgsGeometryCheckerUtils::LayerFeatures.
#define SIP_SKIP
Definition: qgis_sip.h:126
A layer feature combination to uniquely identify and access a feature in a set of layers...
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
double x
Definition: qgspointxy.h:47
static bool canDeleteVertex(const QgsAbstractGeometry *geom, int iPart, int iRing)
A feature pool is based on a vector layer and caches features.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
virtual int vertexCount(int part=0, int ring=0) const =0
Returns the number of vertices of which this geometry is built.
Contains geometry relation and modification algorithms.
QgsMargins operator*(const QgsMargins &margins, double factor)
Returns a QgsMargins object that is formed by multiplying each component of the given margins by fact...
Definition: qgsmargins.h:242
Polygon geometry type.
Definition: qgspolygon.h:31
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.