Skip to content
Permalink
Browse files

[composer] Don't use directional suffixes for annotations when in geo…

…graphic

coordinate system and direction is ambiguous (eg 0 degrees latitude) and format
is set to 'Decimal with suffix'
  • Loading branch information
nyalldawson committed Oct 5, 2014
1 parent 0587872 commit f7cec71a7873372646e9e02a0b40d79db2b8dcb6
@@ -1236,13 +1236,34 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
else if ( mGridAnnotationFormat == QgsComposerMapGrid::DecimalWithSuffix )
{
QString hemisphere;

//check if we are using degrees (ie, geographic crs)
bool geographic = false;
if ( mCRS.isValid() && mCRS.geographicFlag() )
{
geographic = true;
}
else if ( mComposerMap && mComposerMap->composition() )
{
geographic = mComposerMap->composition()->mapSettings().destinationCrs().geographicFlag();
}

double coordRounded = qRound( value * pow( 10.0, mGridAnnotationPrecision ) ) / pow( 10.0, mGridAnnotationPrecision );
if ( coord == QgsComposerMapGrid::Longitude )
{
hemisphere = value < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
//don't use E/W suffixes if ambiguous (eg 180 degrees)
if ( !geographic || ( coordRounded != 180.0 && coordRounded != 0.0 ) )
{
hemisphere = value < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
}
}
else
{
hemisphere = value < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
//don't use N/S suffixes if ambiguous (eg 0 degrees)
if ( !geographic || coordRounded != 0.0 )
{
hemisphere = value < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
}
}
return QString::number( qAbs( value ), 'f', mGridAnnotationPrecision ) + hemisphere;
}
@@ -878,6 +878,8 @@ class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
void drawGridFrameLineBorder( QPainter *p, BorderSide border ) const;

void calculateCRSTransformLines();

friend class TestQgsComposerMapGrid;
};

#endif // QGSCOMPOSERMAPGRID_H
@@ -48,6 +48,7 @@ class TestQgsComposerMapGrid: public QObject
void interiorExteriorTicksAnnotated(); //test interior & exterior tick mode with annotations
void lineBorder(); //test line border frame mode
void lineBorderAnnotated(); //test line border frame with annotations
void annotationFormats(); //various tests for annotation formats

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

void TestQgsComposerMapGrid::annotationFormats()
{
//create grids in geographic and projected coordinates
QgsCoordinateReferenceSystem projectedCrs;
projectedCrs.createFromSrid( 3994 );
QgsCoordinateReferenceSystem geographicCrs;
geographicCrs.createFromSrid( 4326 );

QgsComposerMapGrid gridGeographic( "geographic grid", mComposerMap );
gridGeographic.setCrs( geographicCrs );
QgsComposerMapGrid gridProjected( "projected grid", mComposerMap );
gridProjected.setCrs( projectedCrs );

//decimal degrees format
gridGeographic.setAnnotationFormat( QgsComposerMapGrid::DecimalWithSuffix );
gridGeographic.setAnnotationPrecision( 1 );
gridProjected.setAnnotationFormat( QgsComposerMapGrid::DecimalWithSuffix );
gridProjected.setAnnotationPrecision( 1 );

//normal e/w
QCOMPARE( gridGeographic.gridAnnotationString( 90, QgsComposerMapGrid::Longitude ), QString( "90.0E" ) );
QCOMPARE( gridProjected.gridAnnotationString( 90, QgsComposerMapGrid::Longitude ), QString( "90.0E" ) );

//0 degrees
QCOMPARE( gridGeographic.gridAnnotationString( 0, QgsComposerMapGrid::Longitude ), QString( "0.0" ) );
QCOMPARE( gridProjected.gridAnnotationString( 0, QgsComposerMapGrid::Longitude ), QString( "0.0E" ) );

//180 degrees
QCOMPARE( gridGeographic.gridAnnotationString( 180, QgsComposerMapGrid::Longitude ), QString( "180.0" ) );
QCOMPARE( gridProjected.gridAnnotationString( 180, QgsComposerMapGrid::Longitude ), QString( "180.0E" ) );

//normal n/s
QCOMPARE( gridGeographic.gridAnnotationString( 45, QgsComposerMapGrid::Latitude ), QString( "45.0N" ) );
QCOMPARE( gridProjected.gridAnnotationString( 45, QgsComposerMapGrid::Latitude ), QString( "45.0N" ) );

//0 north/south
QCOMPARE( gridGeographic.gridAnnotationString( 0, QgsComposerMapGrid::Latitude ), QString( "0.0" ) );
QCOMPARE( gridProjected.gridAnnotationString( 0, QgsComposerMapGrid::Latitude ), QString( "0.0N" ) );

}



QTEST_MAIN( TestQgsComposerMapGrid )

0 comments on commit f7cec71

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