Skip to content
Permalink
Browse files

Fix snapping on segment when layer CRS is different from project CRS. F…

…ixes #29648
  • Loading branch information
lbartoletti authored and nyalldawson committed Feb 11, 2021
1 parent 16a071a commit e11b21e954301c1fe317652be36b67da0ff932af
Showing with 30 additions and 3 deletions.
  1. +30 −3 src/gui/qgsmaptoolcapture.cpp
@@ -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;
@@ -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
{

0 comments on commit e11b21e

Please sign in to comment.