Skip to content

Commit

Permalink
Add Layer crs authid expression
Browse files Browse the repository at this point in the history
Adds a "@layer_crs" expression variable, to match behavior of existing @project_crs variable.
  • Loading branch information
roya0045 committed Oct 25, 2020
1 parent 7e95681 commit 1d9c0e7
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -728,6 +728,7 @@ void QgsExpression::initVariableHelp()
//layer variables
sVariableHelpTexts()->insert( QStringLiteral( "layer_name" ), QCoreApplication::translate( "variable_help", "Name of current layer." ) );
sVariableHelpTexts()->insert( QStringLiteral( "layer_id" ), QCoreApplication::translate( "variable_help", "ID of current layer." ) );
sVariableHelpTexts()->insert( QStringLiteral( "layer_crs" ), QCoreApplication::translate( "variable_help", "CRS Authority ID of current layer." ) );
sVariableHelpTexts()->insert( QStringLiteral( "layer" ), QCoreApplication::translate( "variable_help", "The current layer." ) );

//composition variables
Expand Down
1 change: 1 addition & 0 deletions src/core/expression/qgsexpressioncontextutils.cpp
Expand Up @@ -289,6 +289,7 @@ QgsExpressionContextScope *QgsExpressionContextUtils::layerScope( const QgsMapLa
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_name" ), layer->name(), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_id" ), layer->id(), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "_layer_crs" ), QVariant::fromValue<QgsCoordinateReferenceSystem>( layer->crs() ), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_crs" ), layer->crs().authid(), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer >( QgsWeakMapLayerPointer( const_cast<QgsMapLayer *>( layer ) ) ), true, true ) );

const QgsVectorLayer *vLayer = qobject_cast< const QgsVectorLayer * >( layer );
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsexpressioncontext.cpp
Expand Up @@ -790,6 +790,7 @@ void TestQgsExpressionContext::layerScope()

QCOMPARE( context.variable( "layer_name" ).toString(), vectorLayer->name() );
QCOMPARE( context.variable( "layer_id" ).toString(), vectorLayer->id() );
QCOMPARE( context.variable( "layer_crs" ).toString(), vectorLayer->sourceCrs().authid() );

QgsExpression expProject( QStringLiteral( "var('layer_name')" ) );
QCOMPARE( expProject.evaluate( &context ).toString(), vectorLayer->name() );
Expand Down

0 comments on commit 1d9c0e7

Please sign in to comment.