Skip to content
Permalink
Browse files
show correct coordinates in status bar when projected CRS used and
canvas units set to degrees (fix #12395)

Tests for latitude wrapping included
  • Loading branch information
alexbruy committed May 24, 2015
1 parent 72f0c14 commit 9b797471104b984cd0e49d82aaa8c723e1795766
Showing with 59 additions and 9 deletions.
  1. +9 −3 src/app/qgisapp.cpp
  2. +16 −4 src/core/qgspoint.cpp
  3. +34 −2 tests/src/core/testqgspoint.cpp
@@ -6927,14 +6927,20 @@ void QgisApp::showMouseCoordinate( const QgsPoint & p )
{
if ( mMapCanvas->mapUnits() == QGis::Degrees )
{
QgsPoint geo = p;
if ( !mMapCanvas->mapSettings().destinationCrs().geographicFlag() )
{
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), QgsCoordinateReferenceSystem( GEOSRID ) );
geo = ct.transform( p );
}
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );

if ( format == "DM" )
mCoordsEdit->setText( p.toDegreesMinutes( mMousePrecisionDecimalPlaces ) );
mCoordsEdit->setText( geo.toDegreesMinutes( mMousePrecisionDecimalPlaces ) );
else if ( format == "DMS" )
mCoordsEdit->setText( p.toDegreesMinutesSeconds( mMousePrecisionDecimalPlaces ) );
mCoordsEdit->setText( geo.toDegreesMinutesSeconds( mMousePrecisionDecimalPlaces ) );
else
mCoordsEdit->setText( p.toString( mMousePrecisionDecimalPlaces ) );
mCoordsEdit->setText( geo.toString( mMousePrecisionDecimalPlaces ) );
}
else
{
@@ -153,13 +153,25 @@ QString QgsPoint::toDegreesMinutesSeconds( int thePrecision, const bool useSuffi
myWrappedX = myWrappedX + 360.0;
}

//first, limit latitude to -180 to 180 degree range
double myWrappedY = fmod( m_y, 180.0 );
//next, wrap around latitudes > 90 or < -90 degrees, so that eg "110S" -> "70N"
if ( myWrappedY > 90.0 )
{
myWrappedY = myWrappedY - 180.0;
}
else if ( myWrappedY < -90.0 )
{
myWrappedY = myWrappedY + 180.0;
}

int myDegreesX = int( qAbs( myWrappedX ) );
double myFloatMinutesX = double(( qAbs( myWrappedX ) - myDegreesX ) * 60 );
int myIntMinutesX = int( myFloatMinutesX );
double mySecondsX = double( myFloatMinutesX - myIntMinutesX ) * 60;

int myDegreesY = int( qAbs( m_y ) );
double myFloatMinutesY = double(( qAbs( m_y ) - myDegreesY ) * 60 );
int myDegreesY = int( qAbs( myWrappedY ) );
double myFloatMinutesY = double(( qAbs( myWrappedY ) - myDegreesY ) * 60 );
int myIntMinutesY = int( myFloatMinutesY );
double mySecondsY = double( myFloatMinutesY - myIntMinutesY ) * 60;

@@ -192,15 +204,15 @@ QString QgsPoint::toDegreesMinutesSeconds( int thePrecision, const bool useSuffi
if ( useSuffix )
{
myXHemisphere = myWrappedX < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
myYHemisphere = m_y < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
myYHemisphere = myWrappedY < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
}
else
{
if ( myWrappedX < 0 )
{
myXSign = QObject::tr( "-" );
}
if ( m_y < 0 )
if ( myWrappedY < 0 )
{
myYSign = QObject::tr( "-" );
}
@@ -218,6 +218,38 @@ void TestQgsPoint::toDegreesMinutesSeconds()
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 );
QCOMPARE( QgsPoint( -359, 0 ).toDegreesMinutesSeconds( 2 ), myControlString );

//check if latitudes > 90 or <-90 wrap around
myControlString = QString( "0" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) +
QString( ",10" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "N" );
QCOMPARE( QgsPoint( 0, 190 ).toDegreesMinutesSeconds( 2 ), myControlString );
myControlString = QString( "0" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) +
QString( ",10" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "S" );
QCOMPARE( QgsPoint( 0, -190 ).toDegreesMinutesSeconds( 2 ), myControlString );
myControlString = QString( "0" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) +
QString( ",89" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "S" );
QCOMPARE( QgsPoint( 0, 91 ).toDegreesMinutesSeconds( 2 ), myControlString );
myControlString = QString( "0" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) +
QString( ",89" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "N" );
QCOMPARE( QgsPoint( 0, -91 ).toDegreesMinutesSeconds( 2 ), myControlString );
myControlString = QString( "0" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) +
QString( ",1" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "S" );
QCOMPARE( QgsPoint( 0, 179 ).toDegreesMinutesSeconds( 2 ), myControlString );
myControlString = QString( "0" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) +
QString( ",1" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "N" );
QCOMPARE( QgsPoint( 0, -179 ).toDegreesMinutesSeconds( 2 ), myControlString );

//should be no directional suffixes for 0 degree coordinates
myControlString = QString( "0" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) +
@@ -256,9 +288,9 @@ void TestQgsPoint::toDegreesMinutesSeconds()
//test rounding does not create seconds >= 60
myControlString = QString( "100" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "E" ) +
QString( ",100" ) + QChar( 176 ) +
QString( ",90" ) + QChar( 176 ) +
QString( "0" ) + QChar( 0x2032 ) + QString( "0.00" ) + QChar( 0x2033 ) + QString( "N" );
QCOMPARE( QgsPoint( 99.999999, 99.999999 ).toDegreesMinutesSeconds( 2 ), myControlString );
QCOMPARE( QgsPoint( 99.999999, 89.999999 ).toDegreesMinutesSeconds( 2 ), myControlString );

//should be no directional suffixes for 180 degree longitudes
myControlString = QString( "180" ) + QChar( 176 ) +

0 comments on commit 9b79747

Please sign in to comment.