From 188033a6afc298864860ba94a3f150dd5e23f86d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 22 Nov 2016 10:08:14 +1000 Subject: [PATCH] [FEATURE] Expression variables for project CRS Adds @project_crs and @project_crs_definition variables for retrieving the current project CRS --- src/core/qgsexpression.cpp | 2 ++ src/core/qgsexpressioncontext.cpp | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index fc183e5c34c..f44629b6ed8 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -5712,6 +5712,8 @@ void QgsExpression::initVariableHelp() gVariableHelpTexts.insert( QStringLiteral( "project_path" ), QCoreApplication::translate( "variable_help", "Full path (including file name) of current project." ) ); gVariableHelpTexts.insert( QStringLiteral( "project_folder" ), QCoreApplication::translate( "variable_help", "Folder for current project." ) ); gVariableHelpTexts.insert( QStringLiteral( "project_filename" ), QCoreApplication::translate( "variable_help", "Filename of current project." ) ); + gVariableHelpTexts.insert( QStringLiteral( "project_crs" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (eg 'EPSG:4326')." ) ); + gVariableHelpTexts.insert( QStringLiteral( "project_crs_definition" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (full definition)." ) ); //layer variables gVariableHelpTexts.insert( QStringLiteral( "layer_name" ), QCoreApplication::translate( "variable_help", "Name of current layer." ) ); diff --git a/src/core/qgsexpressioncontext.cpp b/src/core/qgsexpressioncontext.cpp index 4e91e08ba05..91ea5ee44ba 100644 --- a/src/core/qgsexpressioncontext.cpp +++ b/src/core/qgsexpressioncontext.cpp @@ -644,6 +644,9 @@ QgsExpressionContextScope* QgsExpressionContextUtils::projectScope() scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_path" ), project->fileInfo().filePath(), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_folder" ), project->fileInfo().dir().path(), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_filename" ), project->fileInfo().fileName(), true ) ); + QgsCoordinateReferenceSystem projectCrs = project->crs(); + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj4(), true ) ); scope->addFunction( QStringLiteral( "project_color" ), new GetNamedProjectColor() ); return scope;