Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Exposing more CRS variables
  • Loading branch information
roya0045 authored and nyalldawson committed Jan 22, 2019
1 parent cb7838b commit f514516
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/core/expression/qgsexpression.cpp
Expand Up @@ -745,8 +745,13 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts.insert( QStringLiteral( "map_extent_width" ), QCoreApplication::translate( "variable_help", "Width of map." ) );
sVariableHelpTexts.insert( QStringLiteral( "map_extent_height" ), QCoreApplication::translate( "variable_help", "Height of map." ) );
sVariableHelpTexts.insert( QStringLiteral( "map_crs" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of map (e.g., 'EPSG:4326')." ) );
sVariableHelpTexts.insert( QStringLiteral( "map_crs_definition" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of map (full definition)." ) );
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_acronym" ), QCoreApplication::translate( "variable_help", "Acronym of the coordinate reference system of the map." ) );
sVariableHelpTexts.insert( QStringLiteral( "map_ellipsoid_acr" ), 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_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
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -1263,7 +1263,12 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const
QgsCoordinateReferenceSystem mapCrs = crs();
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapCrs.authid(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapCrs.toProj4(), true ) );
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_ellipsoid_acr" ), mapCrs.ellipsoidAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapCrs.toProj4(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapCrs.toWkt(), true ) );

QVariantList layersIds;
QVariantList layers;
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -1011,6 +1011,11 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapSettings.destinationCrs().authid(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj4(), true ) );
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_ellipsoid_acr" ), mapSettings.destinationCrs().ellipsoidAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapSettings.destinationCrs().toProj4(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapSettings.destinationCrs().toWkt(), true ) );

// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
Expand Down
36 changes: 28 additions & 8 deletions tests/src/core/testqgslayoutmap.cpp
Expand Up @@ -618,24 +618,44 @@ void TestQgsLayoutMap::expressionContext()
r = e4.evaluate( &c );
QCOMPARE( r.toString(), QString( "degrees" ) );

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

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

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

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

QgsExpression e9( QStringLiteral( "@map_ellipsoid_acr" ) );
r = e9.evaluate( &c );
QCOMPARE( r.toString(), QString( "WGS84" ) );

QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
QgsVectorLayer *layer2 = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "B" ), QStringLiteral( "memory" ) );
map->setLayers( QList<QgsMapLayer *>() << layer << layer2 );
QgsProject::instance()->addMapLayers( map->layers() );
c = map->createExpressionContext();
QgsExpression e5( QStringLiteral( "@map_layer_ids" ) );
r = e5.evaluate( &c );
QgsExpression e10( QStringLiteral( "@map_layer_ids" ) );
r = e10.evaluate( &c );
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "%1,%2" ).arg( layer->id(), layer2->id() ) );
e5 = QgsExpression( QStringLiteral( "array_foreach(@map_layers, layer_property(@element, 'name'))" ) );
r = e5.evaluate( &c );
e10 = QgsExpression( QStringLiteral( "array_foreach(@map_layers, layer_property(@element, 'name'))" ) );
r = e10.evaluate( &c );
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "A,B" ) );

QgsExpression e6( QStringLiteral( "is_layer_visible( '%1' )" ).arg( layer->id() ) );
r = e6.evaluate( &c );
QgsExpression e11( QStringLiteral( "is_layer_visible( '%1' )" ).arg( layer->id() ) );
r = e11.evaluate( &c );
QCOMPARE( r.toBool(), true );

QgsExpression e7( QStringLiteral( "is_layer_visible( 'aaaaaa' )" ) );
r = e7.evaluate( &c );
QgsExpression e12( QStringLiteral( "is_layer_visible( 'aaaaaa' )" ) );
r = e12.evaluate( &c );
QCOMPARE( r.toBool(), false );
}

Expand Down
23 changes: 22 additions & 1 deletion tests/src/core/testqgsmapsettings.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsapplication.h"
#include "qgsmaplayerlistutils.h"
#include "qgsvectorlayer.h"
#include "qgscoordinatereferencesystem.h"

class TestQgsMapSettings: public QObject
{
Expand Down Expand Up @@ -440,7 +441,27 @@ void TestQgsMapSettings::testExpressionContext()

e = QgsExpression( QStringLiteral( "@map_units" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "degrees" ) );
QCOMPARE( r.toString(), QStringLiteral( "degrees" ) );\

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

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

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

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

QgsExpression e9( QStringLiteral( "@map_ellipsoid_acr" ) );
r = e9.evaluate( &c );
QCOMPARE( r.toString(), QString( "WGS84" ) );
}

QGSTEST_MAIN( TestQgsMapSettings )
Expand Down

0 comments on commit f514516

Please sign in to comment.