Skip to content

Commit

Permalink
[feature] Add @map_crs_projection variable for retrieving a descriptive
Browse files Browse the repository at this point in the history
name of the projection used by the map (e.g. "Albers Equal Area")
  • Loading branch information
nyalldawson committed May 11, 2021
1 parent 21edff4 commit ca26f60
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -786,6 +786,7 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts()->insert( QStringLiteral( "map_units" ), QCoreApplication::translate( "variable_help", "Units for map measurements." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_definition" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of the map (full definition)." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_acronym" ), QCoreApplication::translate( "variable_help", "Acronym of the coordinate reference system of the map." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_projection" ), QCoreApplication::translate( "variable_help", "Projection method used by the coordinate reference system of the map." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_ellipsoid" ), QCoreApplication::translate( "variable_help", "Acronym of the ellipsoid of the coordinate reference system of the map." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_proj4" ), QCoreApplication::translate( "variable_help", "Proj4 definition of the coordinate reference system of the map." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_wkt" ), QCoreApplication::translate( "variable_help", "WKT definition of the coordinate reference system of the map." ) );
Expand Down
2 changes: 2 additions & 0 deletions src/core/expression/qgsexpressioncontextutils.cpp
Expand Up @@ -32,6 +32,7 @@
#include "qgsfeatureid.h"
#include "qgslayoutitemmap.h"
#include "qgsmaplayerlistutils.h"
#include "qgsprojoperation.h"

QgsExpressionContextScope *QgsExpressionContextUtils::globalScope()
{
Expand Down Expand Up @@ -456,6 +457,7 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_description" ), mapSettings.destinationCrs().description(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_acronym" ), mapSettings.destinationCrs().projectionAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_projection" ), mapSettings.destinationCrs().operation().description(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_ellipsoid" ), mapSettings.destinationCrs().ellipsoidAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapSettings.destinationCrs().toProj(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapSettings.destinationCrs().toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ), true ) );
Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -35,6 +35,7 @@
#include "qgsstyleentityvisitor.h"
#include "qgsannotationlayer.h"
#include "qgscoordinatereferencesystemregistry.h"
#include "qgsprojoperation.h"

#include <QPainter>
#include <QStyleOptionGraphicsItem>
Expand Down Expand Up @@ -1676,6 +1677,7 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_description" ), mapCrs.description(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapCrs.mapUnits() ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_acronym" ), mapCrs.projectionAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_projection" ), mapCrs.operation().description(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_ellipsoid" ), mapCrs.ellipsoidAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapCrs.toProj(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapCrs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ), true ) );
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgslayoutmap.cpp
Expand Up @@ -704,6 +704,10 @@ void TestQgsLayoutMap::expressionContext()
r = e6.evaluate( &c );
QCOMPARE( r.toString(), QString( "longlat" ) );

QgsExpression e6a( QStringLiteral( "@map_crs_projection" ) );
r = e6a.evaluate( &c );
QCOMPARE( r.toString(), QString( "Lat/long (Geodetic alias)" ) );

QgsExpression e7( QStringLiteral( "@map_crs_proj4" ) );
r = e7.evaluate( &c );
QCOMPARE( r.toString(), QString( "+proj=longlat +datum=WGS84 +no_defs" ) );
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsmapsettings.cpp
Expand Up @@ -520,6 +520,10 @@ void TestQgsMapSettings::testExpressionContext()
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "longlat" ) );

QgsExpression e6a( QStringLiteral( "@map_crs_projection" ) );
r = e6a.evaluate( &c );
QCOMPARE( r.toString(), QString( "Lat/long (Geodetic alias)" ) );

e = QgsExpression( QStringLiteral( "@map_crs_proj4" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "+proj=longlat +datum=WGS84 +no_defs" ) );
Expand Down

0 comments on commit ca26f60

Please sign in to comment.