Skip to content

Commit

Permalink
Add more CRS expression varaibles
Browse files Browse the repository at this point in the history
Fixes #35113
  • Loading branch information
roya0045 authored and nyalldawson committed Mar 18, 2020
1 parent 7614de4 commit a10bb38
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -700,6 +700,12 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts()->insert( QStringLiteral( "project_home" ), QCoreApplication::translate( "variable_help", "Home path of current project." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_crs" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (e.g., 'EPSG:4326')." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_crs_definition" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (full definition)." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_units" ), QCoreApplication::translate( "variable_help", "Unit of the projet's CRS." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_crs_description" ), QCoreApplication::translate( "variable_help", "Name of the coordinate reference system of the project." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_crs_acronym" ), QCoreApplication::translate( "variable_help", "Acronym of the coordinate reference system of the projet." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_crs_ellipsoid" ), QCoreApplication::translate( "variable_help", "Acronym of the ellipsoid of the coordinate reference system of the projet." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_crs_proj4" ), QCoreApplication::translate( "variable_help", "Proj4 definition of the coordinate reference system of the projet." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_crs_wkt" ), QCoreApplication::translate( "variable_help", "WKT definition of the coordinate reference system of the projet." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_author" ), QCoreApplication::translate( "variable_help", "Project author, taken from project metadata." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_abstract" ), QCoreApplication::translate( "variable_help", "Project abstract, taken from project metadata." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_creation_date" ), QCoreApplication::translate( "variable_help", "Project creation date, taken from project metadata." ) );
Expand Down Expand Up @@ -752,11 +758,11 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts()->insert( QStringLiteral( "map_crs" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of map (e.g., 'EPSG:4326')." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_description" ), QCoreApplication::translate( "variable_help", "Name of the coordinate reference system of the map." ) );
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 map (full definition)." ) );
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_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." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_crs_wkt" ), QCoreApplication::translate( "variable_help", "WKT definition of the coordinate reference system." ) );
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." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_layer_ids" ), QCoreApplication::translate( "variable_help", "List of map layer IDs visible in the map." ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_layers" ), QCoreApplication::translate( "variable_help", "List of map layers visible in the map." ) );

Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsproject.cpp
Expand Up @@ -1763,8 +1763,14 @@ QgsExpressionContextScope *QgsProject::createExpressionContextScope() const
QgsCoordinateReferenceSystem projectCrs = crs();
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj(), true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_description" ), projectCrs.description(), true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_ellipsoid" ), ellipsoid(), true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "_project_transform_context" ), QVariant::fromValue<QgsCoordinateTransformContext>( transformContext() ), true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_units" ), QgsUnitTypes::toString( projectCrs.mapUnits() ), true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_acronym" ), projectCrs.projectionAcronym(), true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_ellipsoid" ), projectCrs.ellipsoidAcronym(), true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_proj4" ), projectCrs.toProj(), true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_wkt" ), projectCrs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ), true ) );

// metadata
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_author" ), metadata().author(), true, true ) );
Expand Down
46 changes: 46 additions & 0 deletions tests/src/core/testqgsproject.cpp
Expand Up @@ -49,6 +49,7 @@ class TestQgsProject : public QObject
void testSetGetCrs();
void testEmbeddedLayerGroupFromQgz();
void projectSaveUser();
void testCrsExpressions();
};

void TestQgsProject::init()
Expand Down Expand Up @@ -605,6 +606,51 @@ void TestQgsProject::testSetGetCrs()
ellipsoidChangedSpy.clear();
}

void TestQgsProject::testCrsExpressions()
{
QgsProject p;
QVariant r;

p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );

QgsExpressionContext c = p.createExpressionContext();

QgsExpression e2( QStringLiteral( "@project_crs" ) );
r = e2.evaluate( &c );
QCOMPARE( r.toString(), QString( "EPSG:4326" ) );

QgsExpression e3( QStringLiteral( "@project_crs_definition" ) );
r = e3.evaluate( &c );
QCOMPARE( r.toString(), QString( "+proj=longlat +datum=WGS84 +no_defs" ) );

QgsExpression e4( QStringLiteral( "@project_units" ) );
r = e4.evaluate( &c );
QCOMPARE( r.toString(), QString( "degrees" ) );

QgsExpression e5( QStringLiteral( "@project_crs_description" ) );
r = e5.evaluate( &c );
QCOMPARE( r.toString(), QString( "WGS 84" ) );

QgsExpression e6( QStringLiteral( "@project_crs_acronym" ) );
r = e6.evaluate( &c );
QCOMPARE( r.toString(), QString( "longlat" ) );

QgsExpression e7( QStringLiteral( "@project_crs_proj4" ) );
r = e7.evaluate( &c );
QCOMPARE( r.toString(), QString( "+proj=longlat +datum=WGS84 +no_defs" ) );

QgsExpression e8( QStringLiteral( "@project_crs_wkt" ) );
r = e8.evaluate( &c );
QVERIFY( r.toString().length() >= 15 );

QgsExpression e9( QStringLiteral( "@project_crs_ellipsoid" ) );
r = e9.evaluate( &c );
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( r.toString(), QString( "EPSG:7030" ) );
#else
QCOMPARE( r.toString(), QString( "WGS84" ) );
#endif
}

QGSTEST_MAIN( TestQgsProject )
#include "testqgsproject.moc"

0 comments on commit a10bb38

Please sign in to comment.