Skip to content

Commit

Permalink
Merge pull request #3262 from ahuarte47/Issue_MeasureDialog
Browse files Browse the repository at this point in the history
[Bugfix] Fix MeasureTool when DestinationCRS changes (fixes #15182)
  • Loading branch information
nyalldawson committed Jul 6, 2016
2 parents e985f2c + 70d2c9b commit 8eeceb0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/app/qgsmeasuredialog.cpp
Expand Up @@ -99,6 +99,7 @@ void QgsMeasureDialog::updateSettings()
QgsDebugMsg( QString( "Area units: %1" ).arg( QgsUnitTypes::encodeUnit( mAreaUnits ) ) );
QgsDebugMsg( QString( "Canvas units : %1" ).arg( QgsUnitTypes::encodeUnit( mCanvasUnits ) ) );

mTable->clear();
mTotal = 0;
updateUi();
}
Expand Down
42 changes: 42 additions & 0 deletions src/app/qgsmeasuretool.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsmeasuredialog.h"
#include "qgsmeasuretool.h"
#include "qgscursors.h"
#include "qgsmessagelog.h"

#include <QMessageBox>
#include <QMouseEvent>
Expand All @@ -46,6 +47,7 @@ QgsMeasureTool::QgsMeasureTool( QgsMapCanvas* canvas, bool measureArea )
mDone = true;
// Append point we will move
mPoints.append( QgsPoint( 0, 0 ) );
mDestinationCrs = canvas->mapSettings().destinationCrs();

mDialog = new QgsMeasureDialog( this );
mDialog->setWindowFlags( mDialog->windowFlags() | Qt::Tool );
Expand Down Expand Up @@ -125,7 +127,47 @@ void QgsMeasureTool::updateSettings()
mRubberBandPoints->setIcon( QgsRubberBand::ICON_CIRCLE );
mRubberBandPoints->setIconSize( 10 );
mRubberBandPoints->setColor( QColor( myRed, myGreen, myBlue, 150 ) );

// Reproject the points to the new destination CoordinateReferenceSystem
if ( mRubberBand->size() > 0 && mDestinationCrs != mCanvas->mapSettings().destinationCrs() )
{
QList<QgsPoint> points = mPoints;
bool lastDone = mDone;

mDialog->restart();
mDone = lastDone;
QgsCoordinateTransform ct( mDestinationCrs, mCanvas->mapSettings().destinationCrs() );

Q_FOREACH ( const QgsPoint& previousPoint, points )
{
try
{
QgsPoint point = ct.transform( previousPoint );

mPoints.append( point );
mRubberBand->addPoint( point, false );
mRubberBandPoints->addPoint( point, false );
}
catch ( QgsCsException &cse )
{
QgsMessageLog::logMessage( QString( "Transform error caught at the MeasureTool: %1" ).arg( cse.what() ) );
}
}

mRubberBand->updatePosition();
mRubberBand->update();
mRubberBandPoints->updatePosition();
mRubberBandPoints->update();
}
mDestinationCrs = mCanvas->mapSettings().destinationCrs();

mDialog->updateSettings();

if ( !mDone && mRubberBand->size() > 0 )
{
mRubberBand->addPoint( mPoints.last() );
mDialog->addPoint( mPoints.last() );
}
}

//////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsmeasuretool.h
Expand Up @@ -98,6 +98,9 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
// project projection
bool mWrongProjectProjection;

//! Destination CoordinateReferenceSystem used by the MapCanvas
QgsCoordinateReferenceSystem mDestinationCrs;

//! Returns the snapped (map) coordinate
//@param p (pixel) coordinate
QgsPoint snapPoint( const QPoint& p );
Expand Down
27 changes: 23 additions & 4 deletions tests/src/app/testqgsmeasuretool.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgsproject.h"
#include "qgsmapcanvas.h"
#include "qgsunittypes.h"
#include "qgstestutils.h"

/** \ingroup UnitTests
* This is a unit test for the measure tool
Expand Down Expand Up @@ -113,7 +114,7 @@ void TestQgsMeasureTool::testLengthCalculation()
QString measureString = dlg->editTotal->text();
double measured = measureString.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
double expected = 26932.156;
QVERIFY( qgsDoubleNear( measured, expected, 0.001 ) );
QGSCOMPARENEAR( measured, expected, 0.001 );

// change project length unit, check calculation respects unit
QgsProject::instance()->writeEntry( "Measurement", "/DistanceUnits", QgsUnitTypes::encodeUnit( QGis::Feet ) );
Expand All @@ -130,7 +131,25 @@ void TestQgsMeasureTool::testLengthCalculation()
measureString = dlg2->editTotal->text();
measured = measureString.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
expected = 88360.0918635;
QVERIFY( qgsDoubleNear( measured, expected, 0.001 ) );
QGSCOMPARENEAR( measured, expected, 0.001 );

// check new CoordinateReferenceSystem, points must be reprojected to paint them successfully (issue #15182)
QgsCoordinateReferenceSystem srs2( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );

QgsCoordinateTransform ct( srs, srs2 );

QgsPoint p0 = ct.transform( tool2->points()[0] );
QgsPoint p1 = ct.transform( tool2->points()[1] );

mCanvas->setDestinationCrs( srs2 );

QgsPoint n0 = tool2->points()[0];
QgsPoint n1 = tool2->points()[1];

QGSCOMPARENEAR( p0.x(), n0.x(), 0.001 );
QGSCOMPARENEAR( p0.y(), n0.y(), 0.001 );
QGSCOMPARENEAR( p1.x(), n1.x(), 0.001 );
QGSCOMPARENEAR( p1.y(), n1.y(), 0.001 );
}

void TestQgsMeasureTool::testAreaCalculation()
Expand Down Expand Up @@ -166,7 +185,7 @@ void TestQgsMeasureTool::testAreaCalculation()
QString measureString = dlg->editTotal->text();
double measured = measureString.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
double expected = 1009089817.0;
QVERIFY( qgsDoubleNear( measured, expected, 1.0 ) );
QGSCOMPARENEAR( measured, expected, 1.0 );

// change project area unit, check calculation respects unit
QgsProject::instance()->writeEntry( "Measurement", "/AreaUnits", QgsUnitTypes::encodeUnit( QgsUnitTypes::SquareMiles ) );
Expand All @@ -185,7 +204,7 @@ void TestQgsMeasureTool::testAreaCalculation()
measureString = dlg2->editTotal->text();
measured = measureString.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
expected = 389.6117565069;
QVERIFY( qgsDoubleNear( measured, expected, 0.001 ) );
QGSCOMPARENEAR( measured, expected, 0.001 );
}

QTEST_MAIN( TestQgsMeasureTool )
Expand Down

0 comments on commit 8eeceb0

Please sign in to comment.