QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsgeometrycontainedcheck.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometrycontainedcheck.cpp
3 ---------------------
4 begin : September 2015
5 copyright : (C) 2014 by Sandro Mani / Sourcepole AG
6 email : smani at sourcepole dot ch
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
17#include "qgsgeometryengine.h"
19#include "qgsfeaturepool.h"
20#include "qgsvectorlayer.h"
21
22
23void QgsGeometryContainedCheck::collectErrors( const QMap<QString, QgsFeaturePool *> &featurePools, QList<QgsGeometryCheckError *> &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids ) const
24{
25 const QMap<QString, QgsFeatureIds> featureIds = ids.isEmpty() ? allLayerFeatureIds( featurePools ) : ids.toMap();
26 const QgsGeometryCheckerUtils::LayerFeatures layerFeaturesA( featurePools, featureIds, compatibleGeometryTypes(), feedback, mContext );
27 for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeatureA : layerFeaturesA )
28 {
29 const QgsRectangle bboxA = layerFeatureA.geometry().boundingBox();
30 std::unique_ptr<QgsGeometryEngine> geomEngineA( QgsGeometry::createGeometryEngine( layerFeatureA.geometry().constGet(), mContext->tolerance ) );
31 if ( !geomEngineA->isValid() )
32 {
33 messages.append( tr( "Contained check failed for (%1): the geometry is invalid" ).arg( layerFeatureA.id() ) );
34 continue;
35 }
36 const QgsGeometryCheckerUtils::LayerFeatures layerFeaturesB( featurePools, featureIds.keys(), bboxA, compatibleGeometryTypes(), mContext );
37 for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeatureB : layerFeaturesB )
38 {
39 if ( layerFeatureA == layerFeatureB )
40 {
41 continue;
42 }
43 std::unique_ptr<QgsGeometryEngine> geomEngineB( QgsGeometry::createGeometryEngine( layerFeatureB.geometry().constGet(), mContext->tolerance ) );
44 if ( !geomEngineB->isValid() )
45 {
46 messages.append( tr( "Contained check failed for (%1): the geometry is invalid" ).arg( layerFeatureB.id() ) );
47 continue;
48 }
49 QString errMsg;
50 // If A contains B and B contains A, it would mean that the geometries are identical, which is covered by the duplicate check
51 if ( geomEngineA->contains( layerFeatureB.geometry().constGet(), &errMsg ) && !geomEngineB->contains( layerFeatureA.geometry().constGet(), &errMsg ) && errMsg.isEmpty() )
52 {
53 errors.append( new QgsGeometryContainedCheckError( this, layerFeatureB, layerFeatureB.geometry().constGet()->centroid(), layerFeatureA ) );
54 }
55 else if ( !errMsg.isEmpty() )
56 {
57 messages.append( tr( "Contained check failed for (%1, %2): %3" ).arg( layerFeatureB.id(), layerFeatureA.id(), errMsg ) );
58 }
59 }
60 }
61}
62
63void QgsGeometryContainedCheck::fixError( const QMap<QString, QgsFeaturePool *> &featurePools, QgsGeometryCheckError *error, int method, const QMap<QString, int> & /*mergeAttributeIndices*/, Changes &changes ) const
64{
65 QgsGeometryContainedCheckError *containerError = static_cast<QgsGeometryContainedCheckError *>( error );
66 QgsFeaturePool *featurePoolA = featurePools[error->layerId()];
67 QgsFeaturePool *featurePoolB = featurePools[containerError->containingFeature().first];
68
69 QgsFeature featureA;
70 QgsFeature featureB;
71 if ( !featurePoolA->getFeature( error->featureId(), featureA ) || !featurePoolB->getFeature( containerError->containingFeature().second, featureB ) )
72 {
73 error->setObsolete();
74 return;
75 }
76
77 // Check if error still applies
78 const QgsGeometryCheckerUtils::LayerFeature layerFeatureA( featurePoolA, featureA, mContext, true );
79 const QgsGeometryCheckerUtils::LayerFeature layerFeatureB( featurePoolB, featureB, mContext, true );
80
81 std::unique_ptr<QgsGeometryEngine> geomEngineA( QgsGeometry::createGeometryEngine( layerFeatureA.geometry().constGet(), mContext->tolerance ) );
82 std::unique_ptr<QgsGeometryEngine> geomEngineB( QgsGeometry::createGeometryEngine( layerFeatureB.geometry().constGet(), mContext->tolerance ) );
83
84 if ( !( geomEngineB->contains( layerFeatureA.geometry().constGet() ) && !geomEngineA->contains( layerFeatureB.geometry().constGet() ) ) )
85 {
86 error->setObsolete();
87 return;
88 }
89
90 // Fix error
91 if ( method == NoChange )
92 {
93 error->setFixed( method );
94 }
95 else if ( method == Delete )
96 {
97 changes[error->layerId()][featureA.id()].append( Change( ChangeFeature, ChangeRemoved ) );
98 featurePoolA->deleteFeature( featureA.id() );
99 error->setFixed( method );
100 }
101 else
102 {
103 error->setFixFailed( tr( "Unknown method" ) );
104 }
105}
106
108{
109 static const QStringList methods = QStringList()
110 << tr( "Delete feature" )
111 << tr( "No action" );
112 return methods;
113}
A feature pool is based on a vector layer and caches features.
virtual void deleteFeature(QgsFeatureId fid)=0
Removes a feature from this pool.
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:58
QgsFeatureId id
Definition qgsfeature.h:66
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
const double tolerance
The tolerance to allow for in geometry checks.
This represents an error reported by a geometry check.
QgsFeatureId featureId() const
The id of the feature on which this error has been detected.
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
void setObsolete()
Set the error status to obsolete.
const QString & layerId() const
The id of the layer on which this error has been detected.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
const QgsGeometryCheckContext * mContext
@ ChangeFeature
This change happens on feature level.
QMap< QString, QgsFeatureIds > allLayerFeatureIds(const QMap< QString, QgsFeaturePool * > &featurePools) const
Returns all layers and feature ids.
@ ChangeRemoved
Something has been removed.
A layer feature combination to uniquely identify and access a feature in a set of layers.
QgsGeometry geometry() const
Returns the geometry of this feature.
Contains a set of layers and feature ids in those layers to pass to a geometry check.
const QPair< QString, QgsFeatureId > & containingFeature() const
QList< Qgis::GeometryType > compatibleGeometryTypes() const override
A list of geometry types for which this check can be performed.
void fixError(const QMap< QString, QgsFeaturePool * > &featurePools, QgsGeometryCheckError *error, int method, const QMap< QString, int > &mergeAttributeIndices, Changes &changes) const override
Fixes the error error with the specified method.
void collectErrors(const QMap< QString, QgsFeaturePool * > &featurePools, QList< QgsGeometryCheckError * > &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids=LayerFeatureIds()) const override
The main worker method.
Q_DECL_DEPRECATED QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
A rectangle specified with double values.
Descripts a change to fix a geometry.
A list of layers and feature ids for each of these layers.
QMap< QString, QgsFeatureIds > toMap() const