Skip to content

Commit

Permalink
use avoidIntersectionsV2
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Sep 21, 2023
1 parent a541a1a commit c6020ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
42 changes: 28 additions & 14 deletions src/app/maptools/qgsavoidintersectionsoperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures )
{
QList<QgsVectorLayer *> avoidIntersectionsLayers;
Expand All @@ -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<QgsGeometry> newGeoms = geom.coerceToType( layer->wkbType() );
if ( newGeoms.count() == 1 )
{
geom = newGeoms.at( 0 );
avoidIntersectionsReturn = Qgis::GeometryOperationResult::Success;
}
else // handle multi geometries
{
Expand Down Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions src/app/maptools/qgsavoidintersectionsoperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
Qgis::GeometryOperationResult apply( QgsVectorLayer *layer, QgsFeatureId fid, QgsGeometry &geom,
const QHash<QgsVectorLayer *, QSet<QgsFeatureId> > &ignoreFeatures = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );

signals:

Expand Down
4 changes: 2 additions & 2 deletions src/app/vertextool/qgsvertextool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit c6020ef

Please sign in to comment.