Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix select polygon in QgsMapToolSelectionHandler
  • Loading branch information
vcloarec authored and github-actions[bot] committed Dec 2, 2021
1 parent 701bced commit 3171238
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/app/qgsmaptoolselectionhandler.cpp
Expand Up @@ -263,9 +263,20 @@ void QgsMapToolSelectionHandler::selectPolygonPressEvent( QgsMapMouseEvent *e )
auto vectorLayer = static_cast<QgsVectorLayer *>( layer );
if ( vectorLayer->geometryType() == QgsWkbTypes::PolygonGeometry )
{
QgsRectangle rect( x - sr, y - sr, x + sr, y + sr );
QgsCoordinateTransform transform = mCanvas->mapSettings().layerTransform( vectorLayer );

try
{
rect = transform.transformBoundingBox( rect, QgsCoordinateTransform::ReverseTransform );
}
catch ( QgsCsException &exception )
{
QgsDebugMsg( QStringLiteral( "Could not transform geometry to layer CRS" ) );
}

QgsFeatureIterator fit = vectorLayer->getFeatures( QgsFeatureRequest()
.setDestinationCrs( mCanvas->mapSettings().destinationCrs(), mCanvas->mapSettings().transformContext() )
.setFilterRect( QgsRectangle( x - sr, y - sr, x + sr, y + sr ) )
.setFilterRect( rect )
.setFlags( QgsFeatureRequest::ExactIntersect ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
Expand All @@ -279,7 +290,20 @@ void QgsMapToolSelectionHandler::selectPolygonPressEvent( QgsMapMouseEvent *e )
QPoint globalPos = mCanvas->mapToGlobal( QPoint( e->pos().x() + 5, e->pos().y() + 5 ) );
const QList<QgsMapToolIdentify::IdentifyResult> selectedFeatures = mIdentifyMenu->exec( results, globalPos );
if ( !selectedFeatures.empty() && selectedFeatures[0].mFeature.hasGeometry() )
setSelectedGeometry( selectedFeatures[0].mFeature.geometry(), e->modifiers() );
{
QgsCoordinateTransform transform = mCanvas->mapSettings().layerTransform( selectedFeatures.at( 0 ).mLayer );
QgsGeometry geom = selectedFeatures[0].mFeature.geometry();
try
{
geom.transform( transform );
}
catch ( QgsCsException &exception )
{
QgsDebugMsg( QStringLiteral( "Could not transform geometry to layer CRS" ) );
}

setSelectedGeometry( geom, e->modifiers() );
}

return;
}
Expand Down

0 comments on commit 3171238

Please sign in to comment.