Skip to content

Commit

Permalink
Fix snapping on segment when layer CRS is different from project CRS. F…
Browse files Browse the repository at this point in the history
…ixes #29648
  • Loading branch information
lbartoletti authored and nyalldawson committed Feb 11, 2021
1 parent 16a071a commit e11b21e
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -462,8 +462,8 @@ int QgsMapToolCapture::fetchLayerPoint( const QgsPointLocator::Match &match, Qgs
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
QgsVectorLayer *sourceLayer = match.layer();
if ( match.isValid() && ( match.hasVertex() || ( QgsProject::instance()->topologicalEditing() && ( match.hasEdge() || match.hasMiddleSegment() ) ) ) && sourceLayer &&
( sourceLayer->crs() == vlayer->crs() ) )
if ( match.isValid() && ( match.hasVertex() || ( QgsProject::instance()->topologicalEditing() && ( match.hasEdge() || match.hasMiddleSegment() ) ) ) && sourceLayer )

{
QgsFeature f;
QgsFeatureRequest request;
Expand All @@ -483,7 +483,34 @@ int QgsMapToolCapture::fetchLayerPoint( const QgsPointLocator::Match &match, Qgs
return 2;
QgsLineString line( geom.constGet()->vertexAt( vId ), geom.constGet()->vertexAt( vId2 ) );

layerPoint = QgsGeometryUtils::closestPoint( line, QgsPoint( match.point() ) );
QgsPoint pt( match.point() );
// Transform point to sourceLayer crs, since vId and vId2 coordinates are in sourceLayer crs
if ( sourceLayer->crs() != QgsProject::instance()->crs() )
{
try
{
pt.transform( QgsCoordinateTransform( QgsProject::instance()->crs(), sourceLayer->crs(), sourceLayer->transformContext() ) );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse )
QgsDebugMsg( QStringLiteral( "transformation to layer coordinate failed" ) );
return 2;
}
}
layerPoint = QgsGeometryUtils::closestPoint( line, pt );
// (re)Transform layerPoint to vlayer crs
try
{
layerPoint.transform( QgsCoordinateTransform( sourceLayer->crs(), vlayer->crs(), vlayer->transformContext() ) );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse )
QgsDebugMsg( QStringLiteral( "transformation to layer coordinate failed" ) );
return 2;
}

}
else
{
Expand Down

0 comments on commit e11b21e

Please sign in to comment.