QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 }
QgsFeatureId id
Definition: qgsfeature.h:64
A rectangle specified with double values.
Definition: qgsrectangle.h:41
const QPair< QString, QgsFeatureId > & containingFeature() const
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.
QMap< QString, QgsFeatureIds > toMap() const
Contains a set of layers and feature ids in those layers to pass to a geometry check.
QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
void setObsolete()
Set the error status to obsolete.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
Something has been removed.
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
A layer feature combination to uniquely identify and access a feature in a set of layers...
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
A list of geometry types for which this check can be performed.
QMap< QString, QgsFeatureIds > allLayerFeatureIds(const QMap< QString, QgsFeaturePool *> &featurePools) const
Returns all layers and feature ids.
const QString & layerId() const
The id of the layer on which this error has been detected.
const QgsGeometryCheckContext * mContext
A list of layers and feature ids for each of these layers.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
void collectErrors(const QMap< QString, QgsFeaturePool *> &featurePools, QList< QgsGeometryCheckError *> &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids=LayerFeatureIds()) const override
The main worker method.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
const double tolerance
The tolerance to allow for in geometry checks.
A feature pool is based on a vector layer and caches features.
Descripts a change to fix a geometry.
QgsGeometry geometry() const
Returns the geometry of this feature.
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
virtual void deleteFeature(QgsFeatureId fid)=0
Removes a feature from this pool.
QgsFeatureId featureId() const
The id of the feature on which this error has been detected.
This represents an error reported by a geometry check.
static std::unique_ptr< QgsGeometryEngine > createGeomEngine(const QgsAbstractGeometry *geometry, double tolerance)
This change happens on feature level.