QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
23 void QgsGeometryContainedCheck::collectErrors( const QMap<QString, QgsFeaturePool *> &featurePools, QList<QgsGeometryCheckError *> &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids ) const
24 {
25  QMap<QString, QgsFeatureIds> featureIds = ids.isEmpty() ? allLayerFeatureIds( featurePools ) : ids.toMap();
26  QgsGeometryCheckerUtils::LayerFeatures layerFeaturesA( featurePools, featureIds, compatibleGeometryTypes(), feedback, mContext );
27  for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeatureA : layerFeaturesA )
28  {
29  QgsRectangle bboxA = layerFeatureA.geometry().boundingBox();
30  std::unique_ptr< QgsGeometryEngine > geomEngineA = QgsGeometryCheckerUtils::createGeomEngine( 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  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 = QgsGeometryCheckerUtils::createGeomEngine( 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 
63 void 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 ) ||
72  !featurePoolB->getFeature( containerError->containingFeature().second, featureB ) )
73  {
74  error->setObsolete();
75  return;
76  }
77 
78  // Check if error still applies
79  QgsGeometryCheckerUtils::LayerFeature layerFeatureA( featurePoolA, featureA, mContext, true );
80  QgsGeometryCheckerUtils::LayerFeature layerFeatureB( featurePoolB, featureB, mContext, true );
81 
82  std::unique_ptr< QgsGeometryEngine > geomEngineA = QgsGeometryCheckerUtils::createGeomEngine( layerFeatureA.geometry().constGet(), mContext->tolerance );
83  std::unique_ptr< QgsGeometryEngine > geomEngineB = QgsGeometryCheckerUtils::createGeomEngine( layerFeatureB.geometry().constGet(), mContext->tolerance );
84 
85  if ( !( geomEngineB->contains( layerFeatureA.geometry().constGet() ) && !geomEngineA->contains( layerFeatureB.geometry().constGet() ) ) )
86  {
87  error->setObsolete();
88  return;
89  }
90 
91  // Fix error
92  if ( method == NoChange )
93  {
94  error->setFixed( method );
95  }
96  else if ( method == Delete )
97  {
98  changes[error->layerId()][featureA.id()].append( Change( ChangeFeature, ChangeRemoved ) );
99  featurePoolA->deleteFeature( featureA.id() );
100  error->setFixed( method );
101  }
102  else
103  {
104  error->setFixFailed( tr( "Unknown method" ) );
105  }
106 }
107 
109 {
110  static QStringList methods = QStringList()
111  << tr( "Delete feature" )
112  << tr( "No action" );
113  return methods;
114 }
QgsGeometryContainedCheck::resolutionMethods
Q_DECL_DEPRECATED QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
Definition: qgsgeometrycontainedcheck.cpp:108
QgsGeometryCheckError::setFixFailed
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
Definition: qgsgeometrycheckerror.cpp:109
QgsGeometryCheck::Change
Descripts a change to fix a geometry.
Definition: qgsgeometrycheck.h:177
QgsGeometryCheck::ChangeFeature
@ ChangeFeature
This change happens on feature level.
Definition: qgsgeometrycheck.h:132
QgsGeometryContainedCheck::compatibleGeometryTypes
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
A list of geometry types for which this check can be performed.
Definition: qgsgeometrycontainedcheck.h:62
QgsGeometryContainedCheck::fixError
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.
Definition: qgsgeometrycontainedcheck.cpp:63
QgsGeometryCheckError::setObsolete
void setObsolete()
Set the error status to obsolete.
Definition: qgsgeometrycheckerror.h:166
QgsGeometryCheckError::featureId
QgsFeatureId featureId() const
The id of the feature on which this error has been detected.
Definition: qgsgeometrycheckerror.h:90
QgsGeometryCheck::Changes
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
Definition: qgsgeometrycheck.h:214
QgsGeometryContainedCheckError::containingFeature
const QPair< QString, QgsFeatureId > & containingFeature() const
Definition: qgsgeometrycontainedcheck.h:39
QgsGeometryCheck::allLayerFeatureIds
QMap< QString, QgsFeatureIds > allLayerFeatureIds(const QMap< QString, QgsFeaturePool * > &featurePools) const
Returns all layers and feature ids.
Definition: qgsgeometrycheck.cpp:82
QgsGeometryCheckerUtils::LayerFeature::geometry
QgsGeometry geometry() const
Returns the geometry of this feature.
Definition: qgsgeometrycheckerutils.cpp:70
QgsGeometryCheck::LayerFeatureIds::isEmpty
bool isEmpty() const
Definition: qgsgeometrycheck.h:118
qgsgeometrycontainedcheck.h
QgsFeaturePool::getFeature
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
Definition: qgsfeaturepool.cpp:39
QgsRectangle
Definition: qgsrectangle.h:41
QgsGeometryCheck::LayerFeatureIds::toMap
QMap< QString, QgsFeatureIds > toMap() const
Definition: qgsgeometrycheck.h:113
qgsfeaturepool.h
QgsGeometryCheck::ChangeRemoved
@ ChangeRemoved
Something has been removed.
Definition: qgsgeometrycheck.h:146
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
qgsgeometryengine.h
qgsgeometrycheckcontext.h
QgsFeaturePool::deleteFeature
virtual void deleteFeature(QgsFeatureId fid)=0
Removes a feature from this pool.
QgsGeometryCheckError::setFixed
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
Definition: qgsgeometrycheckerror.cpp:98
QgsFeedback
Definition: qgsfeedback.h:43
QgsGeometryCheckerUtils::LayerFeature
Definition: qgsgeometrycheckerutils.h:51
QgsGeometryContainedCheck::collectErrors
void collectErrors(const QMap< QString, QgsFeaturePool * > &featurePools, QList< QgsGeometryCheckError * > &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids=LayerFeatureIds()) const override
The main worker method.
Definition: qgsgeometrycontainedcheck.cpp:23
QgsGeometryCheck::LayerFeatureIds
A list of layers and feature ids for each of these layers.
Definition: qgsgeometrycheck.h:105
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsGeometryCheck::mContext
const QgsGeometryCheckContext * mContext
Definition: qgsgeometrycheck.h:359
qgsvectorlayer.h
QgsGeometryCheckerUtils::LayerFeatures
Definition: qgsgeometrycheckerutils.h:112
QgsGeometryCheckContext::tolerance
const double tolerance
The tolerance to allow for in geometry checks.
Definition: qgsgeometrycheckcontext.h:61
QgsGeometryCheckerUtils::createGeomEngine
static std::unique_ptr< QgsGeometryEngine > createGeomEngine(const QgsAbstractGeometry *geometry, double tolerance)
Definition: qgsgeometrycheckerutils.cpp:263
QgsGeometryContainedCheck::NoChange
@ NoChange
Definition: qgsgeometrycontainedcheck.h:58
QgsFeature
Definition: qgsfeature.h:55
QgsGeometryCheckError::layerId
const QString & layerId() const
The id of the layer on which this error has been detected.
Definition: qgsgeometrycheckerror.h:85
QgsGeometryCheckError
Definition: qgsgeometrycheckerror.h:35
QgsGeometryContainedCheck::Delete
@ Delete
Definition: qgsgeometrycontainedcheck.h:58
QgsGeometryContainedCheckError
Definition: qgsgeometrycontainedcheck.h:28
QgsFeaturePool
Definition: qgsfeaturepool.h:37