Skip to content

Commit 70d2c9b

Browse files
committed
Fix MeasureTool when DestinationCRS changes
1 parent bf07d2b commit 70d2c9b

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

src/app/qgsmeasuredialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void QgsMeasureDialog::updateSettings()
9999
QgsDebugMsg( QString( "Area units: %1" ).arg( QgsUnitTypes::encodeUnit( mAreaUnits ) ) );
100100
QgsDebugMsg( QString( "Canvas units : %1" ).arg( QgsUnitTypes::encodeUnit( mCanvasUnits ) ) );
101101

102+
mTable->clear();
102103
mTotal = 0;
103104
updateUi();
104105
}

src/app/qgsmeasuretool.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsmeasuredialog.h"
2727
#include "qgsmeasuretool.h"
2828
#include "qgscursors.h"
29+
#include "qgsmessagelog.h"
2930

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

5052
mDialog = new QgsMeasureDialog( this );
5153
mDialog->setWindowFlags( mDialog->windowFlags() | Qt::Tool );
@@ -125,7 +127,47 @@ void QgsMeasureTool::updateSettings()
125127
mRubberBandPoints->setIcon( QgsRubberBand::ICON_CIRCLE );
126128
mRubberBandPoints->setIconSize( 10 );
127129
mRubberBandPoints->setColor( QColor( myRed, myGreen, myBlue, 150 ) );
130+
131+
// Reproject the points to the new destination CoordinateReferenceSystem
132+
if ( mRubberBand->size() > 0 && mDestinationCrs != mCanvas->mapSettings().destinationCrs() )
133+
{
134+
QList<QgsPoint> points = mPoints;
135+
bool lastDone = mDone;
136+
137+
mDialog->restart();
138+
mDone = lastDone;
139+
QgsCoordinateTransform ct( mDestinationCrs, mCanvas->mapSettings().destinationCrs() );
140+
141+
Q_FOREACH ( const QgsPoint& previousPoint, points )
142+
{
143+
try
144+
{
145+
QgsPoint point = ct.transform( previousPoint );
146+
147+
mPoints.append( point );
148+
mRubberBand->addPoint( point, false );
149+
mRubberBandPoints->addPoint( point, false );
150+
}
151+
catch ( QgsCsException &cse )
152+
{
153+
QgsMessageLog::logMessage( QString( "Transform error caught at the MeasureTool: %1" ).arg( cse.what() ) );
154+
}
155+
}
156+
157+
mRubberBand->updatePosition();
158+
mRubberBand->update();
159+
mRubberBandPoints->updatePosition();
160+
mRubberBandPoints->update();
161+
}
162+
mDestinationCrs = mCanvas->mapSettings().destinationCrs();
163+
128164
mDialog->updateSettings();
165+
166+
if ( !mDone && mRubberBand->size() > 0 )
167+
{
168+
mRubberBand->addPoint( mPoints.last() );
169+
mDialog->addPoint( mPoints.last() );
170+
}
129171
}
130172

131173
//////////////////////////

src/app/qgsmeasuretool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
9898
// project projection
9999
bool mWrongProjectProjection;
100100

101+
//! Destination CoordinateReferenceSystem used by the MapCanvas
102+
QgsCoordinateReferenceSystem mDestinationCrs;
103+
101104
//! Returns the snapped (map) coordinate
102105
//@param p (pixel) coordinate
103106
QgsPoint snapPoint( const QPoint& p );

tests/src/app/testqgsmeasuretool.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgsproject.h"
2525
#include "qgsmapcanvas.h"
2626
#include "qgsunittypes.h"
27+
#include "qgstestutils.h"
2728

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

118119
// change project length unit, check calculation respects unit
119120
QgsProject::instance()->writeEntry( "Measurement", "/DistanceUnits", QgsUnitTypes::encodeUnit( QGis::Feet ) );
@@ -130,7 +131,25 @@ void TestQgsMeasureTool::testLengthCalculation()
130131
measureString = dlg2->editTotal->text();
131132
measured = measureString.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
132133
expected = 88360.0918635;
133-
QVERIFY( qgsDoubleNear( measured, expected, 0.001 ) );
134+
QGSCOMPARENEAR( measured, expected, 0.001 );
135+
136+
// check new CoordinateReferenceSystem, points must be reprojected to paint them successfully (issue #15182)
137+
QgsCoordinateReferenceSystem srs2( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );
138+
139+
QgsCoordinateTransform ct( srs, srs2 );
140+
141+
QgsPoint p0 = ct.transform( tool2->points()[0] );
142+
QgsPoint p1 = ct.transform( tool2->points()[1] );
143+
144+
mCanvas->setDestinationCrs( srs2 );
145+
146+
QgsPoint n0 = tool2->points()[0];
147+
QgsPoint n1 = tool2->points()[1];
148+
149+
QGSCOMPARENEAR( p0.x(), n0.x(), 0.001 );
150+
QGSCOMPARENEAR( p0.y(), n0.y(), 0.001 );
151+
QGSCOMPARENEAR( p1.x(), n1.x(), 0.001 );
152+
QGSCOMPARENEAR( p1.y(), n1.y(), 0.001 );
134153
}
135154

136155
void TestQgsMeasureTool::testAreaCalculation()
@@ -166,7 +185,7 @@ void TestQgsMeasureTool::testAreaCalculation()
166185
QString measureString = dlg->editTotal->text();
167186
double measured = measureString.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
168187
double expected = 1009089817.0;
169-
QVERIFY( qgsDoubleNear( measured, expected, 1.0 ) );
188+
QGSCOMPARENEAR( measured, expected, 1.0 );
170189

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

191210
QTEST_MAIN( TestQgsMeasureTool )

0 commit comments

Comments
 (0)