Skip to content
Permalink
Browse files

Merge pull request #3262 from ahuarte47/Issue_MeasureDialog

[Bugfix] Fix MeasureTool when DestinationCRS changes (fixes #15182)
  • Loading branch information
nyalldawson committed Jul 6, 2016
2 parents e985f2c + 70d2c9b commit 8eeceb0dc7af14779d5530b7d2b2ac372a677098
Showing with 69 additions and 4 deletions.
  1. +1 −0 src/app/qgsmeasuredialog.cpp
  2. +42 −0 src/app/qgsmeasuretool.cpp
  3. +3 −0 src/app/qgsmeasuretool.h
  4. +23 −4 tests/src/app/testqgsmeasuretool.cpp
@@ -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();
}
@@ -26,6 +26,7 @@
#include "qgsmeasuredialog.h"
#include "qgsmeasuretool.h"
#include "qgscursors.h"
#include "qgsmessagelog.h"

#include <QMessageBox>
#include <QMouseEvent>
@@ -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 );
@@ -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() );
}
}

//////////////////////////
@@ -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 );
@@ -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
@@ -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 ) );
@@ -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()
@@ -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 ) );
@@ -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 )

0 comments on commit 8eeceb0

Please sign in to comment.
You can’t perform that action at this time.