mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
Merge pull request #3262 from ahuarte47/Issue_MeasureDialog
[Bugfix] Fix MeasureTool when DestinationCRS changes (fixes #15182)
This commit is contained in:
commit
8eeceb0dc7
@ -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 )
|
||||
|
Loading…
x
Reference in New Issue
Block a user