From c6020ef0dce25d604272626fa7f392f2b6bd5bd7 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Thu, 21 Sep 2023 15:38:22 +0200 Subject: [PATCH] use avoidIntersectionsV2 --- .../qgsavoidintersectionsoperation.cpp | 42 ++++++++++++------- .../maptools/qgsavoidintersectionsoperation.h | 4 +- src/app/vertextool/qgsvertextool.cpp | 4 +- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/app/maptools/qgsavoidintersectionsoperation.cpp b/src/app/maptools/qgsavoidintersectionsoperation.cpp index 779fe1d2daf0..9ad17c33f3e9 100644 --- a/src/app/maptools/qgsavoidintersectionsoperation.cpp +++ b/src/app/maptools/qgsavoidintersectionsoperation.cpp @@ -23,7 +23,7 @@ #include "qgsproject.h" #include "qgsvectorlayer.h" -int QgsAvoidIntersectionsOperation::apply( QgsVectorLayer *layer, QgsFeatureId fid, QgsGeometry &geom, +Qgis::GeometryOperationResult QgsAvoidIntersectionsOperation::apply( QgsVectorLayer *layer, QgsFeatureId fid, QgsGeometry &geom, const QHash > &ignoreFeatures ) { QList avoidIntersectionsLayers; @@ -36,24 +36,25 @@ int QgsAvoidIntersectionsOperation::apply( QgsVectorLayer *layer, QgsFeatureId f avoidIntersectionsLayers = QgsProject::instance()->avoidIntersectionsLayers(); break; case Qgis::AvoidIntersectionsMode::AllowIntersections: - return -1; + break; } if ( avoidIntersectionsLayers.isEmpty() ) - { - // TODO - return -1; - } + return Qgis::GeometryOperationResult::NothingHappened; - int avoidIntersectionsReturn = geom.avoidIntersections( avoidIntersectionsLayers, ignoreFeatures ); + Qgis::GeometryOperationResult avoidIntersectionsReturn = geom.avoidIntersectionsV2( avoidIntersectionsLayers, ignoreFeatures ); + bool geomHasChanged = false; switch ( avoidIntersectionsReturn ) { - case 2: // Geometry type was changed, let's try our best to make it compatible with the target layer + geomHasChanged = true; + case Qgis::GeometryOperationResult::OperationChangedGeometryType: // Geometry type was changed, let's try our best to make it compatible with the target layer { + geomHasChanged = true; const QVector newGeoms = geom.coerceToType( layer->wkbType() ); if ( newGeoms.count() == 1 ) { geom = newGeoms.at( 0 ); + avoidIntersectionsReturn = Qgis::GeometryOperationResult::Success; } else // handle multi geometries { @@ -95,18 +96,31 @@ int QgsAvoidIntersectionsOperation::apply( QgsVectorLayer *layer, QgsFeatureId f break; } - case 3: + case Qgis::GeometryOperationResult::InvalidBaseGeometry: emit messageEmitted( tr( "At least one geometry intersected is invalid. These geometries must be manually repaired." ), Qgis::MessageLevel::Warning ); break; - default: + case Qgis::GeometryOperationResult::Success: + geomHasChanged = true; + break; + + case Qgis::GeometryOperationResult::NothingHappened: + case Qgis::GeometryOperationResult::InvalidInputGeometryType: + case Qgis::GeometryOperationResult::SelectionIsEmpty: + case Qgis::GeometryOperationResult::SelectionIsGreaterThanOne: + case Qgis::GeometryOperationResult::GeometryEngineError: + case Qgis::GeometryOperationResult::LayerNotEditable: + case Qgis::GeometryOperationResult::AddPartSelectedGeometryNotFound: + case Qgis::GeometryOperationResult::AddPartNotMultiGeometry: + case Qgis::GeometryOperationResult::AddRingNotClosed: + case Qgis::GeometryOperationResult::AddRingNotValid: + case Qgis::GeometryOperationResult::AddRingCrossesExistingRings: + case Qgis::GeometryOperationResult::AddRingNotInExistingFeature: + case Qgis::GeometryOperationResult::SplitCannotSplitPoint: break; } - // if the geometry has been changed and we have topological editing - if ( QgsProject::instance()->topologicalEditing() - // TODO use a bool in the switch - && avoidIntersectionsReturn != 1 && avoidIntersectionsReturn != 4 ) + if ( QgsProject::instance()->topologicalEditing() && geomHasChanged ) { // then add the new points generated by avoidIntersections QgsGeometry oldGeom = layer->getGeometry( fid ).convertToType( Qgis::GeometryType::Point, true ); diff --git a/src/app/maptools/qgsavoidintersectionsoperation.h b/src/app/maptools/qgsavoidintersectionsoperation.h index 2cad3f22e881..ba199b271c7c 100644 --- a/src/app/maptools/qgsavoidintersectionsoperation.h +++ b/src/app/maptools/qgsavoidintersectionsoperation.h @@ -45,8 +45,8 @@ class GUI_EXPORT QgsAvoidIntersectionsOperation : public QObject // TODO cartouche // TODO don't return int, return enum - int apply( QgsVectorLayer *layer, QgsFeatureId fid, QgsGeometry &geom, - const QHash > &ignoreFeatures = ( QHash >() ) ); + Qgis::GeometryOperationResult apply( QgsVectorLayer *layer, QgsFeatureId fid, QgsGeometry &geom, + const QHash > &ignoreFeatures = ( QHash >() ) ); signals: diff --git a/src/app/vertextool/qgsvertextool.cpp b/src/app/vertextool/qgsvertextool.cpp index d21f4ad7433f..7a2c5b2692b3 100644 --- a/src/app/vertextool/qgsvertextool.cpp +++ b/src/app/vertextool/qgsvertextool.cpp @@ -2406,10 +2406,10 @@ void QgsVertexTool::applyEditsToLayers( QgsVertexTool::VertexEdits &edits ) connect( &avoidIntersections, &QgsAvoidIntersectionsOperation::messageEmitted, this, &QgsMapTool::messageEmitted ); for ( auto itFeatEdit = itLayerEdits->begin() ; itFeatEdit != itLayerEdits->end(); ++itFeatEdit ) { - const int res = avoidIntersections.apply( layer, itFeatEdit.key(), itFeatEdit->geom, ignoreFeatures ); + const Qgis::GeometryOperationResult res = avoidIntersections.apply( layer, itFeatEdit.key(), itFeatEdit->geom, ignoreFeatures ); // TODO add a static method in avoidintersection that return true if avoidintersection happened or not according to return code // avoid intersection happened, no need to add initial new points - if ( res != -1 && res != 1 && res != 4 ) + if ( res != Qgis::GeometryOperationResult::Success && res != Qgis::GeometryOperationResult::InvalidInputGeometryType && res != Qgis::GeometryOperationResult::NothingHappened ) itFeatEdit->newPoints.clear(); layer->changeGeometry( itFeatEdit.key(), itFeatEdit->geom );