Skip to content

Commit db1009c

Browse files
committedJan 11, 2017
[FEATURE] New expression variables for map settings
- map_crs, map_crs_definition: retrieves crs details for map - map_units: retrieves units for map (eg 'meters','degrees')
1 parent f2032ea commit db1009c

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed
 

‎src/core/composer/qgscomposermap.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,13 @@ QgsExpressionContext QgsComposerMap::createExpressionContext() const
18341834
QgsGeometry centerPoint = QgsGeometry::fromPoint( extent.center() );
18351835
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_center" ), QVariant::fromValue( centerPoint ), true ) );
18361836

1837+
if ( mComposition )
1838+
{
1839+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mComposition->mapSettings().destinationCrs().authid(), true ) );
1840+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mComposition->mapSettings().destinationCrs().toProj4(), true ) );
1841+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mComposition->mapSettings().mapUnits() ), true ) );
1842+
}
1843+
18371844
context.appendScope( scope );
18381845

18391846
return context;

‎src/core/qgsexpression.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5825,6 +5825,9 @@ void QgsExpression::initVariableHelp()
58255825
gVariableHelpTexts.insert( QStringLiteral( "map_extent_center" ), QCoreApplication::translate( "variable_help", "Center of map." ) );
58265826
gVariableHelpTexts.insert( QStringLiteral( "map_extent_width" ), QCoreApplication::translate( "variable_help", "Width of map." ) );
58275827
gVariableHelpTexts.insert( QStringLiteral( "map_extent_height" ), QCoreApplication::translate( "variable_help", "Height of map." ) );
5828+
gVariableHelpTexts.insert( QStringLiteral( "map_crs" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of map (e.g., 'EPSG:4326')." ) );
5829+
gVariableHelpTexts.insert( QStringLiteral( "map_crs_definition" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of map (full definition)." ) );
5830+
gVariableHelpTexts.insert( QStringLiteral( "map_units" ), QCoreApplication::translate( "variable_help", "Units for map measurements." ) );
58285831

58295832
gVariableHelpTexts.insert( QStringLiteral( "row_number" ), QCoreApplication::translate( "variable_help", "Stores the number of the current row." ) );
58305833
gVariableHelpTexts.insert( QStringLiteral( "grid_number" ), QCoreApplication::translate( "variable_help", "Current grid annotation value." ) );

‎src/core/qgsexpressioncontext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,10 @@ QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope( const Qg
803803
QgsGeometry centerPoint = QgsGeometry::fromPoint( mapSettings.visibleExtent().center() );
804804
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_center" ), QVariant::fromValue( centerPoint ), true ) );
805805

806+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapSettings.destinationCrs().authid(), true ) );
807+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj4(), true ) );
808+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
809+
806810
return scope;
807811
}
808812

‎tests/src/core/testqgscomposition.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ void TestQgsComposition::itemVariablesFunction()
624624
r = e.evaluate( &c );
625625
QGSCOMPARENEAR( r.toDouble(), 1.38916e+08, 100 );
626626

627+
QgsExpression e2( "map_get( item_variables( 'map_id' ), 'map_crs' )" );
628+
r = e2.evaluate( &c );
629+
QCOMPARE( r.toString(), QString( "EPSG:4326" ) );
630+
631+
QgsExpression e3( "map_get( item_variables( 'map_id' ), 'map_crs_definition' )" );
632+
r = e3.evaluate( &c );
633+
QCOMPARE( r.toString(), QString( "+proj=longlat +datum=WGS84 +no_defs" ) );
634+
635+
QgsExpression e4( "map_get( item_variables( 'map_id' ), 'map_units' )" );
636+
r = e4.evaluate( &c );
637+
QCOMPARE( r.toString(), QString( "degrees" ) );
638+
627639
delete composition;
628640
}
629641

0 commit comments

Comments
 (0)
Please sign in to comment.