From 5b17456393a6dc6407074a4879e5905e42ed3e37 Mon Sep 17 00:00:00 2001 From: Alvaro Huarte Date: Thu, 30 Jun 2016 12:20:29 +0200 Subject: [PATCH 1/2] Fix zoom and scale of MapCanvas when DestinationCrs changes --- src/gui/qgsmapcanvas.cpp | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/gui/qgsmapcanvas.cpp b/src/gui/qgsmapcanvas.cpp index dc5e6e18599..daa5cba4f39 100644 --- a/src/gui/qgsmapcanvas.cpp +++ b/src/gui/qgsmapcanvas.cpp @@ -558,32 +558,34 @@ void QgsMapCanvas::setDestinationCrs( const QgsCoordinateReferenceSystem &crs ) if ( mSettings.destinationCrs() == crs ) return; - if ( mSettings.hasCrsTransformEnabled() ) + // try to reproject current extent to the new one + QgsRectangle rect; + if ( !mSettings.visibleExtent().isEmpty() ) { - // try to reproject current extent to the new one - QgsRectangle rect; - if ( !mSettings.visibleExtent().isEmpty() ) + QgsCoordinateTransform transform( mSettings.destinationCrs(), crs ); + try { - QgsCoordinateTransform transform( mSettings.destinationCrs(), crs ); - try - { - rect = transform.transformBoundingBox( mSettings.visibleExtent() ); - } - catch ( QgsCsException &e ) - { - Q_UNUSED( e ); - QgsDebugMsg( QString( "Transform error caught: %1" ).arg( e.what() ) ); - } + rect = transform.transformBoundingBox( mSettings.visibleExtent() ); } - if ( !rect.isEmpty() ) + catch ( QgsCsException &e ) { - setExtent( rect ); + Q_UNUSED( e ); + QgsDebugMsg( QString( "Transform error caught: %1" ).arg( e.what() ) ); } - - QgsDebugMsg( "refreshing after destination CRS changed" ); - refresh(); } + if ( !mSettings.hasCrsTransformEnabled() ) + { + mSettings.setMapUnits( crs.mapUnits() ); + } + if ( !rect.isEmpty() ) + { + setExtent( rect ); + } + + QgsDebugMsg( "refreshing after destination CRS changed" ); + refresh(); + mSettings.setDestinationCrs( crs ); updateDatumTransformEntries(); From 4057c3a6b49dd409348a2345d43011a8239a4f86 Mon Sep 17 00:00:00 2001 From: Alvaro Huarte Date: Fri, 1 Jul 2016 11:05:01 +0200 Subject: [PATCH 2/2] Add test code --- src/core/qgstestutils.h | 11 +++++++++++ tests/src/gui/testprojectionissues.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/core/qgstestutils.h b/src/core/qgstestutils.h index 8561b7a109c..12c52f76ee6 100644 --- a/src/core/qgstestutils.h +++ b/src/core/qgstestutils.h @@ -32,5 +32,16 @@ QVERIFY( qgsDoubleNear( value, expected, epsilon ) ); \ } +#define QGSCOMPARENEARPOINT(point1,point2,epsilon) { \ + QGSCOMPARENEAR( point1.x(), point2.x(), epsilon ); \ + QGSCOMPARENEAR( point1.y(), point2.y(), epsilon ); \ + } + +#define QGSCOMPARENEARRECTANGLE(rectangle1,rectangle2,epsilon) { \ + QGSCOMPARENEAR( rectangle1.xMinimum(), rectangle2.xMinimum(), epsilon ); \ + QGSCOMPARENEAR( rectangle1.xMaximum(), rectangle2.xMaximum(), epsilon ); \ + QGSCOMPARENEAR( rectangle1.yMinimum(), rectangle2.yMinimum(), epsilon ); \ + QGSCOMPARENEAR( rectangle1.yMaximum(), rectangle2.yMaximum(), epsilon ); \ + } #endif // QGSTESTUTILS_H diff --git a/tests/src/gui/testprojectionissues.cpp b/tests/src/gui/testprojectionissues.cpp index 2f76121c444..b11f255ab69 100644 --- a/tests/src/gui/testprojectionissues.cpp +++ b/tests/src/gui/testprojectionissues.cpp @@ -23,6 +23,7 @@ #include "qgsrasterlayer.h" #include #include +#include "qgstestutils.h" class TestProjectionIssues : public QObject { @@ -39,6 +40,7 @@ class TestProjectionIssues : public QObject void init();// will be called before each testfunction is executed. void cleanup();// will be called after every testfunction. void issue5895();// test for #5895 + void issue15183();// test for #15183 private: QgsRasterLayer* mRasterLayer; @@ -109,5 +111,28 @@ void TestProjectionIssues::issue5895() mMapCanvas->zoomByFactor( 2.0 ); // Zoom out. This should exceed the transform limits. } +void TestProjectionIssues::issue15183() +{ + QgsRectangle largeExtent( -610861, 5101721, 2523921, 6795055 ); + mMapCanvas->setExtent( largeExtent ); + + // Set to CRS's + QgsCoordinateReferenceSystem sourceCRS; + sourceCRS = mMapCanvas->mapSettings().destinationCrs(); + QgsCoordinateReferenceSystem targetCRS; + targetCRS.createFromId( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ); + + QgsCoordinateTransform ct( sourceCRS, targetCRS ); + QgsRectangle initialExtent = ct.transformBoundingBox( mMapCanvas->extent() ); + + mMapCanvas->setCrsTransformEnabled( false ); + mMapCanvas->setDestinationCrs( targetCRS ); + + QgsRectangle currentExtent = mMapCanvas->extent(); + + // Compare center + QGSCOMPARENEARPOINT( initialExtent.center(), currentExtent.center(), 0.00001 ); +} + QTEST_MAIN( TestProjectionIssues ) #include "testprojectionissues.moc"