QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsgeometrycheckerutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * qgsgeometrycheckerutils.cpp *
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
18
19#include "qgsfeaturepool.h"
20#include "qgsfeedback.h"
21#include "qgsgeometry.h"
22#include "qgsgeometrycheck.h"
25#include "qgsgeometryutils.h"
26#include "qgsgeos.h"
27#include "qgspolygon.h"
28#include "qgssurface.h"
29#include "qgsvectorlayer.h"
30
31#include <QString>
32#include <qmath.h>
33
34using namespace Qt::StringLiterals;
35
37 : mFeaturePool( pool )
38 , mFeature( feature )
39 , mGeometry( feature.geometry() )
40 , mMapCrs( useMapCrs )
41{
42 const QgsCoordinateTransform transform( pool->crs(), context->mapCrs, context->transformContext );
43 if ( useMapCrs && context->mapCrs.isValid() && !transform.isShortCircuited() )
44 {
45 try
46 {
47 mGeometry.transform( transform );
48 }
49 catch ( const QgsCsException & )
50 {
51 QgsDebugError( u"Shrug. What shall we do with a geometry that cannot be converted?"_s );
52 }
53 }
54}
55
60
65
66QPointer<QgsVectorLayer> QgsGeometryCheckerUtils::LayerFeature::layer() const
67{
68 return mFeaturePool->layerPtr();
69}
70
72{
73 return mFeaturePool->layerId();
74}
75
80
82{
83 return u"%1:%2"_s.arg( mFeaturePool->layerName() ).arg( mFeature.id() );
84}
85
87{
88 return layerId() == other.layerId() && mFeature.id() == other.mFeature.id();
89}
90
92{
93 return layerId() != other.layerId() || mFeature.id() != other.mFeature.id();
94}
95
97
98QgsGeometryCheckerUtils::LayerFeatures::iterator::iterator( const QStringList::const_iterator &layerIt, const LayerFeatures *parent )
99 : mLayerIt( layerIt )
100 , mFeatureIt( QgsFeatureIds::const_iterator() )
101 , mParent( parent )
102{
103 nextLayerFeature( true );
104}
105
107 : mLayerIt( rh.mLayerIt )
108 , mFeatureIt( rh.mFeatureIt )
109 , mParent( rh.mParent )
110 , mCurrentFeature( std::make_unique<LayerFeature>( *rh.mCurrentFeature.get() ) )
111{
112}
113
115{
116 return mMapCrs;
117}
121
128
130{
131 Q_ASSERT( mCurrentFeature );
132 return *mCurrentFeature;
133}
134
136{
137 return mLayerIt != other.mLayerIt || mFeatureIt != other.mFeatureIt;
138}
139
145bool QgsGeometryCheckerUtils::LayerFeatures::iterator::nextLayerFeature( bool begin )
146{
147 if ( !begin && nextFeature( false ) )
148 {
149 return true;
150 }
151 while ( nextLayer( begin ) )
152 {
153 begin = false;
154 if ( nextFeature( true ) )
155 {
156 return true;
157 }
158 }
159 // End
160 mFeatureIt = QgsFeatureIds::const_iterator();
161 mCurrentFeature.reset();
162 return false;
163}
164
165bool QgsGeometryCheckerUtils::LayerFeatures::iterator::nextLayer( bool begin )
166{
167 if ( !begin )
168 {
169 ++mLayerIt;
170 }
171 while ( true )
172 {
173 if ( mLayerIt == mParent->mLayerIds.end() )
174 {
175 break;
176 }
177 if ( mParent->mGeometryTypes.contains( mParent->mFeaturePools[*mLayerIt]->geometryType() ) )
178 {
179 mFeatureIt = mParent->mFeatureIds[*mLayerIt].constBegin();
180 return true;
181 }
182 ++mLayerIt;
183 }
184 return false;
185}
186
187bool QgsGeometryCheckerUtils::LayerFeatures::iterator::nextFeature( bool begin )
188{
189 QgsFeaturePool *featurePool = mParent->mFeaturePools[*mLayerIt];
190 const QgsFeatureIds &featureIds = mParent->mFeatureIds[*mLayerIt];
191 if ( !begin )
192 {
193 ++mFeatureIt;
194 }
195 while ( true )
196 {
197 if ( mFeatureIt == featureIds.end() )
198 {
199 break;
200 }
201 if ( mParent->mFeedback )
202 mParent->mFeedback->setProgress( mParent->mFeedback->progress() + 1.0 );
203 QgsFeature feature;
204 if ( featurePool->getFeature( *mFeatureIt, feature ) && !feature.geometry().isNull() )
205 {
206 mCurrentFeature = std::make_unique<LayerFeature>( featurePool, feature, mParent->mContext, mParent->mUseMapCrs );
207 return true;
208 }
209 ++mFeatureIt;
210 }
211 return false;
212}
213
215
216QgsGeometryCheckerUtils::LayerFeatures::LayerFeatures( const QMap<QString, QgsFeaturePool *> &featurePools, const QMap<QString, QgsFeatureIds> &featureIds, const QList<Qgis::GeometryType> &geometryTypes, QgsFeedback *feedback, const QgsGeometryCheckContext *context, bool useMapCrs )
217 : mFeaturePools( featurePools )
218 , mFeatureIds( featureIds )
219 , mLayerIds( featurePools.keys() )
220 , mGeometryTypes( geometryTypes )
221 , mFeedback( feedback )
222 , mContext( context )
223 , mUseMapCrs( useMapCrs )
224{}
225
226QgsGeometryCheckerUtils::LayerFeatures::LayerFeatures( const QMap<QString, QgsFeaturePool *> &featurePools, const QList<QString> &layerIds, const QgsRectangle &extent, const QList<Qgis::GeometryType> &geometryTypes, const QgsGeometryCheckContext *context )
227 : mFeaturePools( featurePools )
228 , mLayerIds( layerIds )
229 , mExtent( extent )
230 , mGeometryTypes( geometryTypes )
231 , mContext( context )
232{
233 for ( const QString &layerId : layerIds )
234 {
235 const QgsFeaturePool *featurePool = featurePools[layerId];
236 if ( geometryTypes.contains( featurePool->geometryType() ) )
237 {
238 const QgsCoordinateTransform ct( featurePool->crs(), context->mapCrs, context->transformContext );
239 mFeatureIds.insert( layerId, featurePool->getIntersects( ct.transform( extent, Qgis::TransformDirection::Reverse ) ) );
240 }
241 else
242 {
243 mFeatureIds.insert( layerId, QgsFeatureIds() );
244 }
245 }
246}
247
252
257
259
261{
263 {
264 return collection->geometryN( partIdx );
265 }
266 return geom;
267}
268
270{
272 {
273 return collection->geometryN( partIdx );
274 }
275 return geom;
276}
277
278QList<const QgsLineString *> QgsGeometryCheckerUtils::polygonRings( const QgsPolygon *polygon )
279{
280 QList<const QgsLineString *> rings;
281 if ( const QgsLineString *exterior = qgsgeometry_cast<const QgsLineString *>( polygon->exteriorRing() ) )
282 {
283 rings.append( exterior );
284 }
285 for ( int iInt = 0, nInt = polygon->numInteriorRings(); iInt < nInt; ++iInt )
286 {
287 if ( const QgsLineString *interior = qgsgeometry_cast<const QgsLineString *>( polygon->interiorRing( iInt ) ) )
288 {
289 rings.append( interior );
290 }
291 }
292 return rings;
293}
294
296{
298 {
299 for ( int nParts = geom->partCount(), iPart = nParts - 1; iPart >= 0; --iPart )
300 {
301 if ( !qgsgeometry_cast<QgsSurface *>( geomCollection->geometryN( iPart ) ) )
302 {
303 geomCollection->removeGeometry( iPart );
304 }
305 }
306 }
307}
308
309double pointLineDist( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q )
310{
311 const double nom = std::fabs( ( p2.y() - p1.y() ) * q.x() - ( p2.x() - p1.x() ) * q.y() + p2.x() * p1.y() - p2.y() * p1.x() );
312 const double dx = p2.x() - p1.x();
313 const double dy = p2.y() - p1.y();
314 return nom / std::sqrt( dx * dx + dy * dy );
315}
316
317bool QgsGeometryCheckerUtils::pointOnLine( const QgsPoint &p, const QgsLineString *line, double tol, bool excludeExtremities )
318{
319 const int nVerts = line->vertexCount();
320 for ( int i = 0 + excludeExtremities; i < nVerts - 1 - excludeExtremities; ++i )
321 {
322 const QgsPoint p1 = line->vertexAt( QgsVertexId( 0, 0, i ) );
323 const QgsPoint p2 = line->vertexAt( QgsVertexId( 0, 0, i + 1 ) );
324 const double dist = pointLineDist( p1, p2, p );
325 if ( dist < tol )
326 {
327 return true;
328 }
329 }
330 return false;
331}
332
333QList<QgsPoint> QgsGeometryCheckerUtils::lineIntersections( const QgsLineString *line1, const QgsLineString *line2, double tol )
334{
335 QList<QgsPoint> intersections;
336 QgsPoint inter;
337 bool intersection = false;
338 for ( int i = 0, n = line1->vertexCount() - 1; i < n; ++i )
339 {
340 for ( int j = 0, m = line2->vertexCount() - 1; j < m; ++j )
341 {
342 const QgsPoint p1 = line1->vertexAt( QgsVertexId( 0, 0, i ) );
343 const QgsPoint p2 = line1->vertexAt( QgsVertexId( 0, 0, i + 1 ) );
344 const QgsPoint q1 = line2->vertexAt( QgsVertexId( 0, 0, j ) );
345 const QgsPoint q2 = line2->vertexAt( QgsVertexId( 0, 0, j + 1 ) );
346 if ( QgsGeometryUtils::segmentIntersection( p1, p2, q1, q2, inter, intersection, tol ) )
347 {
348 intersections.append( inter );
349 }
350 }
351 }
352 return intersections;
353}
354
356{
357 double len = 0;
358
359 // Test every pair of segments for shared edges
360 for ( int iPart1 = 0, nParts1 = geom1->partCount(); iPart1 < nParts1; ++iPart1 )
361 {
362 for ( int iRing1 = 0, nRings1 = geom1->ringCount( iPart1 ); iRing1 < nRings1; ++iRing1 )
363 {
364 for ( int iVert1 = 0, jVert1 = 1, nVerts1 = geom1->vertexCount( iPart1, iRing1 ); jVert1 < nVerts1; iVert1 = jVert1++ )
365 {
366 const QgsPoint p1 = geom1->vertexAt( QgsVertexId( iPart1, iRing1, iVert1 ) );
367 const QgsPoint p2 = geom1->vertexAt( QgsVertexId( iPart1, iRing1, jVert1 ) );
368 const double lambdap1 = 0.;
369 const double lambdap2 = std::sqrt( QgsGeometryUtils::sqrDistance2D( p1, p2 ) );
370 QgsVector d;
371 try
372 {
373 d = QgsVector( p2.x() - p1.x(), p2.y() - p1.y() ).normalized();
374 }
375 catch ( const QgsException & )
376 {
377 // Edge has zero length, skip
378 continue;
379 }
380
381 for ( int iPart2 = 0, nParts2 = geom2->partCount(); iPart2 < nParts2; ++iPart2 )
382 {
383 for ( int iRing2 = 0, nRings2 = geom2->ringCount( iPart2 ); iRing2 < nRings2; ++iRing2 )
384 {
385 for ( int iVert2 = 0, jVert2 = 1, nVerts2 = geom2->vertexCount( iPart2, iRing2 ); jVert2 < nVerts2; iVert2 = jVert2++ )
386 {
387 const QgsPoint q1 = geom2->vertexAt( QgsVertexId( iPart2, iRing2, iVert2 ) );
388 const QgsPoint q2 = geom2->vertexAt( QgsVertexId( iPart2, iRing2, jVert2 ) );
389
390 // Check whether q1 and q2 are on the line p1, p
391 if ( pointLineDist( p1, p2, q1 ) <= tol && pointLineDist( p1, p2, q2 ) <= tol )
392 {
393 // Get length common edge
394 double lambdaq1 = QgsVector( q1.x() - p1.x(), q1.y() - p1.y() ) * d;
395 double lambdaq2 = QgsVector( q2.x() - p1.x(), q2.y() - p1.y() ) * d;
396 if ( lambdaq1 > lambdaq2 )
397 {
398 std::swap( lambdaq1, lambdaq2 );
399 }
400 const double lambda1 = std::max( lambdaq1, lambdap1 );
401 const double lambda2 = std::min( lambdaq2, lambdap2 );
402 len += std::max( 0., lambda2 - lambda1 );
403 }
404 }
405 }
406 }
407 }
408 }
409 }
410 return len;
411}
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2731
Abstract base class for all geometries.
virtual int ringCount(int part=0) const =0
Returns the number of rings of which this geometry is built.
virtual int vertexCount(int part=0, int ring=0) const =0
Returns the number of vertices of which this geometry is built.
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
virtual int partCount() const =0
Returns count of parts contained in the geometry.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Handles coordinate transforms between two coordinate systems.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
Custom exception class for Coordinate Reference System related exceptions.
int numInteriorRings() const
Returns the number of interior rings contained with the curve polygon.
const QgsCurve * exteriorRing() const
Returns the curve polygon's exterior ring.
const QgsCurve * interiorRing(int i) const
Retrieves an interior ring from the curve polygon.
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition qgscurve.cpp:181
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
Definition qgscurve.cpp:199
Defines a QGIS exception class.
A feature pool is based on a vector layer and caches features.
Qgis::GeometryType geometryType() const
The geometry type of this layer.
QgsCoordinateReferenceSystem crs() const
The coordinate reference system of this layer.
QgsFeatureIds getIntersects(const QgsRectangle &rect) const
Gets all feature ids in the bounding box rect.
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Base configuration for geometry checks.
const QgsCoordinateTransformContext transformContext
The coordinate transform context with which transformations will be done.
const QgsCoordinateReferenceSystem mapCrs
The coordinate system in which calculations should be done.
A layer feature combination to uniquely identify and access a feature in a set of layers.
LayerFeature(const QgsFeaturePool *pool, const QgsFeature &feature, const QgsGeometryCheckContext *context, bool useMapCrs)
Create a new layer/feature combination.
QgsGeometry geometry() const
Returns the geometry of this feature.
QString id() const
Returns a combination of the layerId and the feature id.
bool operator==(const QgsGeometryCheckerUtils::LayerFeature &other) const
QgsFeature feature() const
Returns the feature.
QPointer< QgsVectorLayer > layer() const
The layer.
bool operator!=(const QgsGeometryCheckerUtils::LayerFeature &other) const
bool useMapCrs() const
Returns if the geometry is reprojected to the map CRS or not.
QgsCoordinateReferenceSystem layerCrs() const
The layer CRS.
An iterator over all features in a QgsGeometryCheckerUtils::LayerFeatures.
const iterator & operator++()
Increments the item the iterator currently points to by one and returns the new iterator.
const QgsGeometryCheckerUtils::LayerFeature & operator*() const
Dereferences the item at the current iterator location.
iterator(const QStringList::const_iterator &layerIt, const LayerFeatures *parent)
Creates a new iterator.
iterator end() const
One after the last feature to stop iterating.
LayerFeatures(const QMap< QString, QgsFeaturePool * > &featurePools, const QMap< QString, QgsFeatureIds > &featureIds, const QList< Qgis::GeometryType > &geometryTypes, QgsFeedback *feedback, const QgsGeometryCheckContext *context, bool useMapCrs=false)
Creates a new set of layer and features.
iterator begin() const
The first feature to start iterating.
static void filter1DTypes(QgsAbstractGeometry *geom)
static QList< const QgsLineString * > polygonRings(const QgsPolygon *polygon)
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
static double sharedEdgeLength(const QgsAbstractGeometry *geom1, const QgsAbstractGeometry *geom2, double tol)
static QList< QgsPoint > lineIntersections(const QgsLineString *line1, const QgsLineString *line2, double tol)
static bool pointOnLine(const QgsPoint &p, const QgsLineString *line, double tol, bool excludeExtremities=false)
static bool segmentIntersection(const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q1, const QgsPoint &q2, QgsPoint &intersectionPoint, bool &isIntersection, double tolerance=1e-8, bool acceptImproperIntersection=false)
Compute the intersection between two segments.
static Q_DECL_DEPRECATED double sqrDistance2D(double x1, double y1, double x2, double y2)
Returns the squared 2D distance between (x1, y1) and (x2, y2).
A geometry is the spatial representation of a feature.
Line string geometry type, with support for z-dimension and m-values.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double x
Definition qgspoint.h:56
double y
Definition qgspoint.h:57
Polygon geometry type.
Definition qgspolygon.h:37
A rectangle specified with double values.
Represent a 2-dimensional vector.
Definition qgsvector.h:34
QgsVector normalized() const
Returns the vector's normalized (or "unit") vector (ie same angle but length of 1....
Definition qgsvector.cpp:33
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QSet< QgsFeatureId > QgsFeatureIds
double pointLineDist(const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q)
#define QgsDebugError(str)
Definition qgslogger.h:59
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34