bool QgsGeometrySelfIntersectionCheckError::handleChanges( const QgsGeometryCheck::Changes &changes ) { if ( !QgsGeometryCheckError::handleChanges( changes ) ) { return false; } Q_FOREACH ( const QgsGeometryCheck::Change &change, changes.value( featureId() ) ) { if ( change.vidx.vertex == mInter.segment1 || change.vidx.vertex == mInter.segment1 + 1 || change.vidx.vertex == mInter.segment2 || change.vidx.vertex == mInter.segment2 + 1 ) { return false; } else if ( change.vidx.vertex >= 0 ) { if ( change.vidx.vertex < mInter.segment1 ) { mInter.segment1 += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1; } if ( change.vidx.vertex < mInter.segment2 ) { mInter.segment2 += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1; } } } return true; }
bool QgsGeometryCheckError::handleChanges( const QgsGeometryCheck::Changes &changes ) { if ( status() == StatusObsolete ) { return false; } for ( const QgsGeometryCheck::Change &change : changes.value( layerId() ).value( featureId() ) ) { if ( change.what == QgsGeometryCheck::ChangeFeature ) { if ( change.type == QgsGeometryCheck::ChangeRemoved ) { return false; } else if ( change.type == QgsGeometryCheck::ChangeChanged ) { // If the check is checking the feature at geometry nodes level, the // error almost certainly invalid after a geometry change. In the other // cases, it might likely still be valid. return mCheck->getCheckType() != QgsGeometryCheck::FeatureNodeCheck; } } else if ( change.what == QgsGeometryCheck::ChangePart ) { if ( mVidx.part == change.vidx.part ) { return false; } else if ( mVidx.part > change.vidx.part ) { mVidx.part += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1; } } else if ( change.what == QgsGeometryCheck::ChangeRing ) { if ( mVidx.partEqual( change.vidx ) ) { if ( mVidx.ring == change.vidx.ring ) { return false; } else if ( mVidx.ring > change.vidx.ring ) { mVidx.ring += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1; } } } else if ( change.what == QgsGeometryCheck::ChangeNode ) { if ( mVidx.ringEqual( change.vidx ) ) { if ( mVidx.vertex == change.vidx.vertex ) { return false; } else if ( mVidx.vertex > change.vidx.vertex ) { mVidx.vertex += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1; } } } } return true; }
bool QgsGeometryChecker::fixError( QgsGeometryCheckError *error, int method, bool triggerRepaint ) { mMessages.clear(); if ( error->status() >= QgsGeometryCheckError::StatusFixed ) { return true; } #if 0 QTextStream( stdout ) << "Fixing " << error->description() << ": " << error->layerId() << ":" << error->featureId() << " @[" << error->vidx().part << ", " << error->vidx().ring << ", " << error->vidx().vertex << "](" << error->location().x() << ", " << error->location().y() << ") = " << error->value().toString() << endl; #endif QgsGeometryCheck::Changes changes; QgsRectangle recheckArea = error->affectedAreaBBox(); error->check()->fixError( error, method, mMergeAttributeIndices, changes ); #if 0 QTextStream( stdout ) << " * Status: " << error->resolutionMessage() << endl; static QVector<QString> strChangeWhat = { "ChangeFeature", "ChangePart", "ChangeRing", "ChangeNode" }; static QVector<QString> strChangeType = { "ChangeAdded", "ChangeRemoved", "ChangeChanged" }; for ( const QString &layerId : changes.keys() ) { for ( const QgsFeatureId &fid : changes[layerId].keys() ) { for ( const QgsGeometryCheck::Change &change : changes[layerId][fid] ) { QTextStream( stdout ) << " * Change: " << layerId << ":" << fid << " :: " << strChangeWhat[change.what] << ":" << strChangeType[change.type] << ":(" << change.vidx.part << "," << change.vidx.ring << "," << change.vidx.vertex << ")" << endl; } } } #endif emit errorUpdated( error, true ); if ( error->status() != QgsGeometryCheckError::StatusFixed ) { return false; } // If nothing was changed, stop here if ( changes.isEmpty() ) { return true; } // Determine what to recheck // - Collect all features which were changed, get affected area QMap<QString, QSet<QgsFeatureId>> recheckFeatures; for ( auto it = changes.constBegin(); it != changes.constEnd(); ++it ) { const QMap<QgsFeatureId, QList<QgsGeometryCheck::Change>> &layerChanges = it.value(); QgsFeaturePool *featurePool = mContext->featurePools[it.key()]; QgsCoordinateTransform t( featurePool->getLayer()->crs(), mContext->mapCrs, QgsProject::instance() ); for ( auto layerChangeIt = layerChanges.constBegin(); layerChangeIt != layerChanges.constEnd(); ++layerChangeIt ) { bool removed = false; for ( const QgsGeometryCheck::Change &change : layerChangeIt.value() ) { if ( change.what == QgsGeometryCheck::ChangeFeature && change.type == QgsGeometryCheck::ChangeRemoved ) { removed = true; break; } } if ( !removed ) { QgsFeature f; if ( featurePool->get( layerChangeIt.key(), f ) ) { recheckFeatures[it.key()].insert( layerChangeIt.key() ); recheckArea.combineExtentWith( t.transformBoundingBox( f.geometry().boundingBox() ) ); } } } } // - Determine extent to recheck for gaps for ( QgsGeometryCheckError *err : qgis::as_const( mCheckErrors ) ) { if ( err->check()->getCheckType() == QgsGeometryCheck::LayerCheck ) { if ( err->affectedAreaBBox().intersects( recheckArea ) ) { recheckArea.combineExtentWith( err->affectedAreaBBox() ); } } } recheckArea.grow( 10 * mContext->tolerance ); QMap<QString, QgsFeatureIds> recheckAreaFeatures; for ( const QString &layerId : mContext->featurePools.keys() ) { QgsFeaturePool *featurePool = mContext->featurePools[layerId]; QgsCoordinateTransform t( mContext->mapCrs, featurePool->getLayer()->crs(), QgsProject::instance() ); recheckAreaFeatures[layerId] = featurePool->getIntersects( t.transform( recheckArea ) ); } // Recheck feature / changed area to detect new errors QList<QgsGeometryCheckError *> recheckErrors; for ( const QgsGeometryCheck *check : qgis::as_const( mChecks ) ) { if ( check->getCheckType() == QgsGeometryCheck::LayerCheck ) { if ( !recheckAreaFeatures.isEmpty() ) { check->collectErrors( recheckErrors, mMessages, nullptr, recheckAreaFeatures ); } } else { if ( !recheckFeatures.isEmpty() ) { check->collectErrors( recheckErrors, mMessages, nullptr, recheckFeatures ); } } } // Go through error list, update other errors of the checked feature for ( QgsGeometryCheckError *err : qgis::as_const( mCheckErrors ) ) { if ( err == error || err->status() == QgsGeometryCheckError::StatusObsolete ) { continue; } QgsGeometryCheckError::Status oldStatus = err->status(); bool handled = err->handleChanges( changes ); // Check if this error now matches one found when rechecking the feature/area QgsGeometryCheckError *matchErr = nullptr; int nMatch = 0; for ( QgsGeometryCheckError *recheckErr : qgis::as_const( recheckErrors ) ) { if ( recheckErr->isEqual( err ) || recheckErr->closeMatch( err ) ) { ++nMatch; matchErr = recheckErr; } } // If just one close match was found, take it if ( nMatch == 1 && matchErr ) { err->update( matchErr ); emit errorUpdated( err, err->status() != oldStatus ); recheckErrors.removeAll( matchErr ); delete matchErr; continue; } // If no match is found and the error is not fixed or obsolete, set it to obsolete if... if ( err->status() < QgsGeometryCheckError::StatusFixed && ( // changes weren't handled !handled || // or if it is a FeatureNodeCheck or FeatureCheck error whose feature was rechecked ( err->check()->getCheckType() <= QgsGeometryCheck::FeatureCheck && recheckFeatures[err->layerId()].contains( err->featureId() ) ) || // or if it is a LayerCheck error within the rechecked area ( err->check()->getCheckType() == QgsGeometryCheck::LayerCheck && recheckArea.contains( err->affectedAreaBBox() ) ) ) ) { err->setObsolete(); emit errorUpdated( err, err->status() != oldStatus ); } } // Add new errors for ( QgsGeometryCheckError *recheckErr : qgis::as_const( recheckErrors ) ) { emit errorAdded( recheckErr ); mCheckErrors.append( recheckErr ); } if ( triggerRepaint ) { for ( const QString &layerId : changes.keys() ) { mContext->featurePools[layerId]->getLayer()->triggerRepaint(); } } return true; }