Skip to content

Commit f7cec71

Browse files
committedOct 5, 2014
[composer] Don't use directional suffixes for annotations when in geographic
coordinate system and direction is ambiguous (eg 0 degrees latitude) and format is set to 'Decimal with suffix'
1 parent 0587872 commit f7cec71

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed
 

‎src/core/composer/qgscomposermapgrid.cpp

100755100644
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,13 +1236,34 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
12361236
else if ( mGridAnnotationFormat == QgsComposerMapGrid::DecimalWithSuffix )
12371237
{
12381238
QString hemisphere;
1239+
1240+
//check if we are using degrees (ie, geographic crs)
1241+
bool geographic = false;
1242+
if ( mCRS.isValid() && mCRS.geographicFlag() )
1243+
{
1244+
geographic = true;
1245+
}
1246+
else if ( mComposerMap && mComposerMap->composition() )
1247+
{
1248+
geographic = mComposerMap->composition()->mapSettings().destinationCrs().geographicFlag();
1249+
}
1250+
1251+
double coordRounded = qRound( value * pow( 10.0, mGridAnnotationPrecision ) ) / pow( 10.0, mGridAnnotationPrecision );
12391252
if ( coord == QgsComposerMapGrid::Longitude )
12401253
{
1241-
hemisphere = value < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
1254+
//don't use E/W suffixes if ambiguous (eg 180 degrees)
1255+
if ( !geographic || ( coordRounded != 180.0 && coordRounded != 0.0 ) )
1256+
{
1257+
hemisphere = value < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
1258+
}
12421259
}
12431260
else
12441261
{
1245-
hemisphere = value < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
1262+
//don't use N/S suffixes if ambiguous (eg 0 degrees)
1263+
if ( !geographic || coordRounded != 0.0 )
1264+
{
1265+
hemisphere = value < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
1266+
}
12461267
}
12471268
return QString::number( qAbs( value ), 'f', mGridAnnotationPrecision ) + hemisphere;
12481269
}

‎src/core/composer/qgscomposermapgrid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,8 @@ class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
878878
void drawGridFrameLineBorder( QPainter *p, BorderSide border ) const;
879879

880880
void calculateCRSTransformLines();
881+
882+
friend class TestQgsComposerMapGrid;
881883
};
882884

883885
#endif // QGSCOMPOSERMAPGRID_H

‎tests/src/core/testqgscomposermapgrid.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class TestQgsComposerMapGrid: public QObject
4848
void interiorExteriorTicksAnnotated(); //test interior & exterior tick mode with annotations
4949
void lineBorder(); //test line border frame mode
5050
void lineBorderAnnotated(); //test line border frame with annotations
51+
void annotationFormats(); //various tests for annotation formats
5152

5253
private:
5354
QgsComposition* mComposition;
@@ -454,6 +455,47 @@ void TestQgsComposerMapGrid::lineBorderAnnotated()
454455
mComposerMap->grid()->setAnnotationEnabled( false );
455456
}
456457

458+
void TestQgsComposerMapGrid::annotationFormats()
459+
{
460+
//create grids in geographic and projected coordinates
461+
QgsCoordinateReferenceSystem projectedCrs;
462+
projectedCrs.createFromSrid( 3994 );
463+
QgsCoordinateReferenceSystem geographicCrs;
464+
geographicCrs.createFromSrid( 4326 );
465+
466+
QgsComposerMapGrid gridGeographic( "geographic grid", mComposerMap );
467+
gridGeographic.setCrs( geographicCrs );
468+
QgsComposerMapGrid gridProjected( "projected grid", mComposerMap );
469+
gridProjected.setCrs( projectedCrs );
470+
471+
//decimal degrees format
472+
gridGeographic.setAnnotationFormat( QgsComposerMapGrid::DecimalWithSuffix );
473+
gridGeographic.setAnnotationPrecision( 1 );
474+
gridProjected.setAnnotationFormat( QgsComposerMapGrid::DecimalWithSuffix );
475+
gridProjected.setAnnotationPrecision( 1 );
476+
477+
//normal e/w
478+
QCOMPARE( gridGeographic.gridAnnotationString( 90, QgsComposerMapGrid::Longitude ), QString( "90.0E" ) );
479+
QCOMPARE( gridProjected.gridAnnotationString( 90, QgsComposerMapGrid::Longitude ), QString( "90.0E" ) );
480+
481+
//0 degrees
482+
QCOMPARE( gridGeographic.gridAnnotationString( 0, QgsComposerMapGrid::Longitude ), QString( "0.0" ) );
483+
QCOMPARE( gridProjected.gridAnnotationString( 0, QgsComposerMapGrid::Longitude ), QString( "0.0E" ) );
484+
485+
//180 degrees
486+
QCOMPARE( gridGeographic.gridAnnotationString( 180, QgsComposerMapGrid::Longitude ), QString( "180.0" ) );
487+
QCOMPARE( gridProjected.gridAnnotationString( 180, QgsComposerMapGrid::Longitude ), QString( "180.0E" ) );
488+
489+
//normal n/s
490+
QCOMPARE( gridGeographic.gridAnnotationString( 45, QgsComposerMapGrid::Latitude ), QString( "45.0N" ) );
491+
QCOMPARE( gridProjected.gridAnnotationString( 45, QgsComposerMapGrid::Latitude ), QString( "45.0N" ) );
492+
493+
//0 north/south
494+
QCOMPARE( gridGeographic.gridAnnotationString( 0, QgsComposerMapGrid::Latitude ), QString( "0.0" ) );
495+
QCOMPARE( gridProjected.gridAnnotationString( 0, QgsComposerMapGrid::Latitude ), QString( "0.0N" ) );
496+
497+
}
498+
457499

458500

459501
QTEST_MAIN( TestQgsComposerMapGrid )

0 commit comments

Comments
 (0)
Please sign in to comment.