17#include <QtConcurrentMap>
18#include <QFutureWatcher>
25#include "moc_qgsgeometrychecker.cpp"
37 , mFeaturePools( featurePools )
39 for (
auto it =
featurePools.constBegin(); it != mFeaturePools.constEnd(); ++it )
41 if ( it.value()->layer() )
43 it.value()->layer()->setReadOnly(
true );
45 it.value()->layer()->dataProvider()->enterUpdateMode();
52 qDeleteAll( mCheckErrors );
53 qDeleteAll( mChecks );
54 for (
auto it = mFeaturePools.constBegin(); it != mFeaturePools.constEnd(); ++it )
56 if ( it.value()->layer() )
58 it.value()->layer()->dataProvider()->leaveUpdateMode();
59 it.value()->layer()->setReadOnly(
false );
73 for (
auto it = mFeaturePools.constBegin(); it != mFeaturePools.constEnd(); ++it )
77 *totalSteps += check->isCompatible( it.value()->layer() ) ? it.value()->allFeatureIds().size() : 0;
86 QTimer *timer =
new QTimer();
87 connect( timer, &QTimer::timeout,
this, &QgsGeometryChecker::emitProgressValue );
88 QFutureWatcher<void> *watcher =
new QFutureWatcher<void>();
89 connect( watcher, &QFutureWatcherBase::finished, timer, &QObject::deleteLater );
90 connect( watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater );
92 QFuture<void> future = QtConcurrent::map( mChecks, RunCheckWrapper(
this ) );
93 watcher->setFuture( future );
99void QgsGeometryChecker::emitProgressValue()
118 error->
check()->
fixError( mFeaturePools, error, method, mMergeAttributeIndices, changes );
121 static QVector<QString> strChangeWhat = {
"ChangeFeature",
"ChangePart",
"ChangeRing",
"ChangeNode" };
122 static QVector<QString> strChangeType = {
"ChangeAdded",
"ChangeRemoved",
"ChangeChanged" };
123 for (
const QString &layerId : changes.keys() )
125 for (
const QgsFeatureId &fid : changes[layerId].keys() )
129 QTextStream( stdout ) <<
" * Change: " << layerId <<
":" << fid <<
" :: " << strChangeWhat[change.what] <<
":" << strChangeType[change.type] <<
":(" << change.vidx.part <<
"," << change.vidx.ring <<
"," << change.vidx.vertex <<
")" << endl;
141 if ( changes.isEmpty() )
148 QMap<QString, QSet<QgsFeatureId>> recheckFeatures;
149 for (
auto it = changes.constBegin(); it != changes.constEnd(); ++it )
151 const QMap<QgsFeatureId, QList<QgsGeometryCheck::Change>> &layerChanges = it.value();
155 for (
auto layerChangeIt = layerChanges.constBegin(); layerChangeIt != layerChanges.constEnd(); ++layerChangeIt )
157 bool removed =
false;
169 if ( featurePool->
getFeature( layerChangeIt.key(), f ) )
171 recheckFeatures[it.key()].insert( layerChangeIt.key() );
182 if ( err->affectedAreaBBox().intersects( recheckArea ) )
189 QMap<QString, QgsFeatureIds> recheckAreaFeatures;
190 for (
auto it = mFeaturePools.constBegin(); it != mFeaturePools.constEnd(); it++ )
198 QList<QgsGeometryCheckError *> recheckErrors;
203 if ( !recheckAreaFeatures.isEmpty() )
205 check->collectErrors( mFeaturePools, recheckErrors, mMessages,
nullptr, recheckAreaFeatures );
210 if ( !recheckFeatures.isEmpty() )
212 check->collectErrors( mFeaturePools, recheckErrors, mMessages,
nullptr, recheckFeatures );
227 bool handled = err->handleChanges( changes );
234 if ( recheckErr->isEqual( err ) || recheckErr->closeMatch( err ) )
237 matchErr = recheckErr;
241 if ( nMatch == 1 && matchErr )
245 recheckErrors.removeAll( matchErr );
271 mCheckErrors.append( recheckErr );
274 if ( triggerRepaint )
276 for (
auto itChange = changes.constBegin(); itChange != changes.constEnd(); itChange++ )
278 mFeaturePools[itChange.key()]->layer()->triggerRepaint();
285void QgsGeometryChecker::runCheck(
const QMap<QString, QgsFeaturePool *> &featurePools,
const QgsGeometryCheck *check )
288 QList<QgsGeometryCheckError *> errors;
289 QStringList messages;
291 mErrorListMutex.lock();
292 mCheckErrors.append( errors );
293 mMessages.append( messages );
294 mErrorListMutex.unlock();
302 : mInstance( instance )
306void QgsGeometryChecker::RunCheckWrapper::operator()(
const QgsGeometryCheck *check )
308 mInstance->runCheck( mInstance->mFeaturePools, check );
A feature pool is based on a vector layer and caches features.
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...
double progress() const
Returns the current progress reported by the feedback object.
Base configuration for geometry checks.
const QgsCoordinateReferenceSystem mapCrs
The coordinate system in which calculations should be done.
const double tolerance
The tolerance to allow for in geometry checks.
This represents an error reported by a geometry check.
Status
The status of an error.
@ StatusFixed
The error is fixed.
@ StatusObsolete
The error is obsolete because of other modifications.
Status status() const
The status of the error.
QString resolutionMessage() const
A message with details, how the error has been resolved.
const QgsVertexId & vidx() const
The id of the affected vertex.
QgsFeatureId featureId() const
The id of the feature on which this error has been detected.
virtual void update(const QgsGeometryCheckError *other)
Update this error with the information from other.
virtual QString description() const
The error description.
QVariant value() const
An additional value for the error.
void setObsolete()
Set the error status to obsolete.
const QgsGeometryCheck * check() const
The geometry check that created this error.
const QString & layerId() const
The id of the layer on which this error has been detected.
virtual QgsRectangle affectedAreaBBox() const
The bounding box of the affected area of the error.
const QgsPointXY & location() const
The location of the error in map units.
This class implements a geometry check.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
virtual void collectErrors(const QMap< QString, QgsFeaturePool * > &featurePools, QList< QgsGeometryCheckError * > &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids=QgsGeometryCheck::LayerFeatureIds()) const =0
The main worker method.
@ ChangeFeature
This change happens on feature level.
@ LayerCheck
The check controls a whole layer (topology checks)
@ FeatureCheck
The check controls geometries as a whole.
@ ChangeRemoved
Something has been removed.
virtual void fixError(const QMap< QString, QgsFeaturePool * > &featurePools, QgsGeometryCheckError *error, int method, const QMap< QString, int > &mergeAttributeIndices, Changes &changes) const
Fixes the error error with the specified method.
Manages and runs a set of geometry checks.
QFuture< void > execute(int *totalSteps=nullptr)
~QgsGeometryChecker() override
void progressValue(int value)
bool fixError(QgsGeometryCheckError *error, int method, bool triggerRepaint=false)
QgsGeometryChecker(const QList< QgsGeometryCheck * > &checks, QgsGeometryCheckContext *context, const QMap< QString, QgsFeaturePool * > &featurePools)
void errorAdded(QgsGeometryCheckError *error)
void errorUpdated(QgsGeometryCheckError *error, bool statusChanged)
const QMap< QString, QgsFeaturePool * > & featurePools() const
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
void grow(double delta)
Grows the rectangle in place by the specified amount.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Descripts a change to fix a geometry.