Skip to content
Permalink
Browse files

When copying coordinates, base number of decimal places on the current

map extent

So we get better precision the further the user zooms in to the map.
6 decimal places of precision makes little sense when copying a coordinate
from a world map scale view, but DOES make sense when zoomed into
a meter-level map!

Fixes #27831
  • Loading branch information
nyalldawson committed May 26, 2020
1 parent aa86c74 commit d6a9aa22ad241a2f4696e23c31541856e2bbee03
Showing with 32 additions and 1 deletion.
  1. +32 −1 src/gui/qgsmapcanvas.cpp
@@ -815,7 +815,38 @@ void QgsMapCanvas::showContextMenu( QgsMapMouseEvent *event )
{
const QgsPointXY transformedPoint = ct.transform( mapPoint );

const int displayPrecision = crs.mapUnits() == QgsUnitTypes::DistanceDegrees ? 5 : 3;
// calculate precision based on visible map extent -- if user is zoomed in, we get better precision!
int displayPrecision = 0;
try
{
QgsRectangle extentReproj = ct.transformBoundingBox( extent() );
const double mapUnitsPerPixel = ( extentReproj.width() / width() + extentReproj.height() / height() ) * 0.5;
if ( mapUnitsPerPixel > 10 )
displayPrecision = 0;
else if ( mapUnitsPerPixel > 1 )
displayPrecision = 1;
else if ( mapUnitsPerPixel > 0.1 )
displayPrecision = 2;
else if ( mapUnitsPerPixel > 0.01 )
displayPrecision = 3;
else if ( mapUnitsPerPixel > 0.001 )
displayPrecision = 4;
else if ( mapUnitsPerPixel > 0.0001 )
displayPrecision = 5;
else if ( mapUnitsPerPixel > 0.00001 )
displayPrecision = 6;
else if ( mapUnitsPerPixel > 0.000001 )
displayPrecision = 7;
else if ( mapUnitsPerPixel > 0.0000001 )
displayPrecision = 8;
else
displayPrecision = 9;
}
catch ( QgsCsException & )
{
displayPrecision = crs.mapUnits() == QgsUnitTypes::DistanceDegrees ? 5 : 3;
}

QAction *copyCoordinateAction = new QAction( QStringLiteral( "%3 (%1, %2)" ).arg(
QString::number( transformedPoint.x(), 'f', displayPrecision ),
QString::number( transformedPoint.y(), 'f', displayPrecision ),

0 comments on commit d6a9aa2

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