QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsgeometryduplicatecheck.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryduplicatecheck.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 "qgsspatialindex.h"
20 #include "qgsgeometry.h"
21 #include "qgsfeaturepool.h"
22 #include "qgsvectorlayer.h"
23 
24 QString QgsGeometryDuplicateCheckError::duplicatesString( const QMap<QString, QgsFeaturePool *> &featurePools, const QMap<QString, QList<QgsFeatureId>> &duplicates )
25 {
26  QStringList str;
27  for ( auto it = duplicates.constBegin(); it != duplicates.constEnd(); ++it )
28  {
29  str.append( featurePools[it.key()]->layer()->name() + ":" );
30  QStringList ids;
31  ids.reserve( it.value().length() );
32  for ( QgsFeatureId id : it.value() )
33  {
34  ids.append( QString::number( id ) );
35  }
36  str.back() += ids.join( ',' );
37  }
38  return str.join( QLatin1String( "; " ) );
39 }
40 
41 
42 void QgsGeometryDuplicateCheck::collectErrors( const QMap<QString, QgsFeaturePool *> &featurePools, QList<QgsGeometryCheckError *> &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids ) const
43 {
44  QMap<QString, QgsFeatureIds> featureIds = ids.isEmpty() ? allLayerFeatureIds( featurePools ) : ids.toMap();
45  QgsGeometryCheckerUtils::LayerFeatures layerFeaturesA( featurePools, featureIds, compatibleGeometryTypes(), feedback, mContext, true );
46  QList<QString> layerIds = featureIds.keys();
47  for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeatureA : layerFeaturesA )
48  {
49  // Ensure each pair of layers only gets compared once: remove the current layer from the layerIds, but add it to the layerList for layerFeaturesB
50  layerIds.removeOne( layerFeatureA.layer()->id() );
51 
52  QgsGeometry geomA = layerFeatureA.geometry();
53  QgsRectangle bboxA = geomA.boundingBox();
54  std::unique_ptr< QgsGeometryEngine > geomEngineA = QgsGeometryCheckerUtils::createGeomEngine( geomA.constGet(), mContext->tolerance );
55  if ( !geomEngineA->isValid() )
56  {
57  messages.append( tr( "Duplicate check failed for (%1): the geometry is invalid" ).arg( layerFeatureA.id() ) );
58  continue;
59  }
60  QMap<QString, QList<QgsFeatureId>> duplicates;
61 
62  QgsWkbTypes::GeometryType geomType = geomA.type();
63  QgsGeometryCheckerUtils::LayerFeatures layerFeaturesB( featurePools, QList<QString>() << layerFeatureA.layer()->id() << layerIds, bboxA, {geomType}, mContext );
64  for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeatureB : layerFeaturesB )
65  {
66  // > : only report overlaps within same layer once
67  if ( layerFeatureA.layer()->id() == layerFeatureB.layer()->id() && layerFeatureB.feature().id() >= layerFeatureA.feature().id() )
68  {
69  continue;
70  }
71  QString errMsg;
72  QgsGeometry geomB = layerFeatureB.geometry();
73  std::unique_ptr<QgsAbstractGeometry> diffGeom( geomEngineA->symDifference( geomB.constGet(), &errMsg ) );
74  if ( errMsg.isEmpty() && ( !diffGeom || diffGeom->isEmpty() ) )
75  {
76  duplicates[layerFeatureB.layer()->id()].append( layerFeatureB.feature().id() );
77  }
78  else if ( !errMsg.isEmpty() )
79  {
80  messages.append( tr( "Duplicate check failed for (%1, %2): %3" ).arg( layerFeatureA.id(), layerFeatureB.id(), errMsg ) );
81  }
82  }
83  if ( !duplicates.isEmpty() )
84  {
85  errors.append( new QgsGeometryDuplicateCheckError( this, layerFeatureA, geomA.constGet()->centroid(), featurePools, duplicates ) );
86  }
87  }
88 }
89 
90 void QgsGeometryDuplicateCheck::fixError( const QMap<QString, QgsFeaturePool *> &featurePools, QgsGeometryCheckError *error, int method, const QMap<QString, int> & /*mergeAttributeIndices*/, Changes &changes ) const
91 {
92  QgsFeaturePool *featurePoolA = featurePools[ error->layerId() ];
93  QgsFeature featureA;
94  if ( !featurePoolA->getFeature( error->featureId(), featureA ) )
95  {
96  error->setObsolete();
97  return;
98  }
99 
100  if ( method == NoChange )
101  {
102  error->setFixed( method );
103  }
104  else if ( method == RemoveDuplicates )
105  {
106  QgsGeometryCheckerUtils::LayerFeature layerFeatureA( featurePoolA, featureA, mContext, true );
107  std::unique_ptr< QgsGeometryEngine > geomEngineA = QgsGeometryCheckerUtils::createGeomEngine( layerFeatureA.geometry().constGet(), mContext->tolerance );
108 
109  QgsGeometryDuplicateCheckError *duplicateError = static_cast<QgsGeometryDuplicateCheckError *>( error );
110  const QMap<QString, QList<QgsFeatureId>> duplicates = duplicateError->duplicates();
111  for ( auto it = duplicates.constBegin(); it != duplicates.constEnd(); ++it )
112  {
113  const QString layerIdB = it.key();
114  QgsFeaturePool *featurePoolB = featurePools[ layerIdB ];
115  const QList< QgsFeatureId > ids = it.value();
116  for ( QgsFeatureId idB : ids )
117  {
118  QgsFeature featureB;
119  if ( !featurePoolB->getFeature( idB, featureB ) )
120  {
121  continue;
122  }
123  QgsGeometryCheckerUtils::LayerFeature layerFeatureB( featurePoolB, featureB, mContext, true );
124  std::unique_ptr< QgsAbstractGeometry > diffGeom( geomEngineA->symDifference( layerFeatureB.geometry().constGet() ) );
125  if ( !diffGeom || diffGeom->isEmpty() )
126  {
127  featurePoolB->deleteFeature( featureB.id() );
128  changes[layerIdB][idB].append( Change( ChangeFeature, ChangeRemoved ) );
129  }
130  }
131  }
132  error->setFixed( method );
133  }
134  else
135  {
136  error->setFixFailed( tr( "Unknown method" ) );
137  }
138 }
139 
141 {
142  static QStringList methods = QStringList()
143  << tr( "No action" )
144  << tr( "Remove duplicates" );
145  return methods;
146 }
147 
149 {
150  return QStringLiteral( "QgsGeometryDuplicateCheck" );
151 }
152 
154 {
156 }
virtual QgsPoint centroid() const
Returns the centroid of the geometry.
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 id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
const double tolerance
The tolerance to allow for in geometry checks.
This represents an error reported by a geometry check.
const QString & layerId() const
The id of the layer on which this error has been detected.
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.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
const QgsGeometryCheckContext * mContext
@ ChangeFeature
This change happens on feature level.
CheckType
The type of a check.
@ FeatureCheck
The check controls geometries as a whole.
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.
static std::unique_ptr< QgsGeometryEngine > createGeomEngine(const QgsAbstractGeometry *geometry, double tolerance)
A duplicate geometry check error.
QMap< QString, QList< QgsFeatureId > > duplicates() const
static QgsGeometryCheck::CheckType factoryCheckType()
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
A list of geometry types for which this check can be performed.
Q_DECL_DEPRECATED QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
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.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsWkbTypes::GeometryType type
Definition: qgsgeometry.h:127
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
Descripts a change to fix a geometry.
A list of layers and feature ids for each of these layers.
QMap< QString, QgsFeatureIds > toMap() const