Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_40] Fix topological slowness with spatial filtering #60171

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ Merge features into a single one.
.. versionadded:: 3.30
%End


};

/************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ Merge features into a single one.
.. versionadded:: 3.30
%End


};

/************************************************************************
Expand Down
25 changes: 24 additions & 1 deletion src/app/qgsmaptooladdfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgisapp.h"
#include "qgsexpressioncontextutils.h"
#include "qgsrubberband.h"
#include "qgsvectorlayereditutils.h"

#include <QSettings>

Expand Down Expand Up @@ -119,18 +120,40 @@ void QgsMapToolAddFeature::featureDigitized( const QgsFeature &feature )
}
if ( topologicalEditing )
{
QgsFeatureRequest request = QgsFeatureRequest().setNoAttributes().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setLimit( 1 );
const QgsRectangle bbox = feature.geometry().boundingBox();
const QList<QgsMapLayer *> layers = canvas()->layers( true );

for ( QgsMapLayer *layer : layers )
{
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
QgsRectangle searchRect;
QgsFeature f;
QgsCoordinateTransform transform;

if ( !vectorLayer || !vectorLayer->isEditable() )
continue;

if ( !( vectorLayer->geometryType() == Qgis::GeometryType::Polygon || vectorLayer->geometryType() == Qgis::GeometryType::Line ) )
continue;

if ( vectorLayer->crs() == vlayer->crs() )
{
searchRect = QgsRectangle( bbox );
}
else
{
transform = QgsCoordinateTransform( vlayer->crs(), vectorLayer->crs(), vectorLayer->transformContext() );
searchRect = transform.transformBoundingBox( bbox );
}

searchRect.grow( QgsVectorLayerEditUtils::getTopologicalSearchRadius( vectorLayer ) );
request.setFilterRect( searchRect );

// We check that there is actually at least one feature intersecting our geometry in the layer to avoid creating an empty edit command and calling costly addTopologicalPoint
if ( !vectorLayer->getFeatures( request ).nextFeature( f ) )
continue;

vectorLayer->beginEditCommand( tr( "Topological points added by 'Add Feature'" ) );

int res = 2;
Expand All @@ -140,7 +163,7 @@ void QgsMapToolAddFeature::featureDigitized( const QgsFeature &feature )
try
{
// transform digitized geometry from vlayer crs to vectorLayer crs and add topological points
transformedGeom.transform( QgsCoordinateTransform( vlayer->crs(), vectorLayer->crs(), vectorLayer->transformContext() ) );
transformedGeom.transform( transform );
res = vectorLayer->addTopologicalPoints( transformedGeom );
}
catch ( QgsCsException &cse )
Expand Down
15 changes: 15 additions & 0 deletions src/app/vertextool/qgsvertextool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "qgsexpressioncontextutils.h"
#include "qgsmessagebar.h"
#include "qgssettingsentryimpl.h"
#include "qgsvectorlayereditutils.h"


#include <QMenu>
Expand Down Expand Up @@ -2204,13 +2205,19 @@ void QgsVertexTool::moveVertex( const QgsPointXY &mapPoint, const QgsPointLocato
{
// topo editing: add vertex to existing segments when moving/adding a vertex to such segment.

QgsFeatureRequest request = QgsFeatureRequest().setNoAttributes().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setLimit( 1 );
const QgsRectangle bbox = layerPoint.boundingBox();


const QList<QgsMapLayer *> targetLayers = canvas()->layers( true );

for ( auto itLayerEdits = edits.begin(); itLayerEdits != edits.end(); ++itLayerEdits )
{
for ( QgsMapLayer *targetLayer : targetLayers )
{
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( targetLayer );
QgsRectangle searchRect;
QgsFeature f;

if ( !vectorLayer || !vectorLayer->isEditable() )
continue;
Expand All @@ -2222,6 +2229,14 @@ void QgsVertexTool::moveVertex( const QgsPointXY &mapPoint, const QgsPointLocato
if ( vectorLayer->crs() != itLayerEdits.key()->crs() )
continue;

searchRect = QgsRectangle( bbox );
searchRect.grow( QgsVectorLayerEditUtils::getTopologicalSearchRadius( vectorLayer ) );
request.setFilterRect( searchRect );

// We check that there is actually at least one feature intersecting our geometry in the layer to avoid creating an empty edit command and calling costly addTopologicalPoint
if ( !vectorLayer->getFeatures( request ).nextFeature( f ) )
continue;

vectorLayer->beginEditCommand( tr( "Topological points added by 'Vertex Tool'" ) );

bool topoPointsAdded = false;
Expand Down
38 changes: 23 additions & 15 deletions src/core/vector/qgsvectorlayereditutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,28 @@ Qgis::GeometryOperationResult staticAddRing( QgsVectorLayer *layer, std::unique_
return success ? Qgis::GeometryOperationResult::Success : addRingReturnCode;
}

///@cond PRIVATE
double QgsVectorLayerEditUtils::getTopologicalSearchRadius( const QgsVectorLayer *layer )
{
double threshold = layer->geometryOptions()->geometryPrecision();

if ( qgsDoubleNear( threshold, 0.0 ) )
{
threshold = 1e-8;

if ( layer->crs().mapUnits() == Qgis::DistanceUnit::Meters )
{
threshold = 0.001;
}
else if ( layer->crs().mapUnits() == Qgis::DistanceUnit::Feet )
{
threshold = 0.0001;
}
}
return threshold;
}
///@endcond

Qgis::GeometryOperationResult QgsVectorLayerEditUtils::addRing( const QVector<QgsPointXY> &ring, const QgsFeatureIds &targetFeatureIds, QgsFeatureId *modifiedFeatureId )
{
QgsPointSequence l;
Expand Down Expand Up @@ -801,21 +823,7 @@ int QgsVectorLayerEditUtils::addTopologicalPoints( const QgsPoint &p )
double segmentSearchEpsilon = mLayer->crs().isGeographic() ? 1e-12 : 1e-8;

//work with a tolerance because coordinate projection may introduce some rounding
double threshold = mLayer->geometryOptions()->geometryPrecision();

if ( qgsDoubleNear( threshold, 0.0 ) )
{
threshold = 1e-8;

if ( mLayer->crs().mapUnits() == Qgis::DistanceUnit::Meters )
{
threshold = 0.001;
}
else if ( mLayer->crs().mapUnits() == Qgis::DistanceUnit::Feet )
{
threshold = 0.0001;
}
}
double threshold = getTopologicalSearchRadius( mLayer );

QgsRectangle searchRect( p, p, false );
searchRect.grow( threshold );
Expand Down
4 changes: 4 additions & 0 deletions src/core/vector/qgsvectorlayereditutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ class CORE_EXPORT QgsVectorLayerEditUtils
*/
bool mergeFeatures( const QgsFeatureId &targetFeatureId, const QgsFeatureIds &mergeFeatureIds, const QgsAttributes &mergeAttributes, const QgsGeometry &unionGeometry, QString &errorMessage SIP_OUT );

///@cond PRIVATE
static double getTopologicalSearchRadius( const QgsVectorLayer *layer ) SIP_SKIP;
///@endcond

private:

/**
Expand Down
Loading