Skip to content
Permalink
Browse files

[FEATURE] Add expression variables for current map temporal range

Adds:

@map_start_time: Start of the map's temporal time range (as a datetime value)
@map_end_time: End of the map's temporal time range (as a datetime value)
@map_interval: Duration of the map's temporal time range (as an interval value)
  • Loading branch information
nyalldawson committed Mar 6, 2020
1 parent 722dd8a commit 6dbf341d04983961ada7ed48a40a636b11843587
@@ -760,6 +760,10 @@ void QgsExpression::initVariableHelp()
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." ) );

sVariableHelpTexts()->insert( QStringLiteral( "map_start_time" ), QCoreApplication::translate( "variable_help", "Start of the map's temporal time range (as a datetime value)" ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_end_time" ), QCoreApplication::translate( "variable_help", "End of the map's temporal time range (as a datetime value)" ) );
sVariableHelpTexts()->insert( QStringLiteral( "map_interval" ), QCoreApplication::translate( "variable_help", "Duration of the map's temporal time range (as an interval value)" ) );

sVariableHelpTexts()->insert( QStringLiteral( "row_number" ), QCoreApplication::translate( "variable_help", "Stores the number of the current row." ) );
sVariableHelpTexts()->insert( QStringLiteral( "grid_number" ), QCoreApplication::translate( "variable_help", "Current grid annotation value." ) );
sVariableHelpTexts()->insert( QStringLiteral( "grid_axis" ), QCoreApplication::translate( "variable_help", "Current grid annotation axis (e.g., 'x' for longitude, 'y' for latitude)." ) );
@@ -411,6 +411,13 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )

scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_start_time" ), mapSettings.isTemporal() ? mapSettings.temporalRange().begin() : QVariant(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_end_time" ), mapSettings.isTemporal() ? mapSettings.temporalRange().end() : QVariant(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_interval" ), mapSettings.isTemporal() ? ( mapSettings.temporalRange().end() - mapSettings.temporalRange().begin() ) : QVariant(), true ) );

// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )

return scope;
}

@@ -1521,6 +1521,11 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const

scope->addFunction( QStringLiteral( "is_layer_visible" ), new QgsExpressionContextUtils::GetLayerVisibility( layersInMap, scale() ) );

// maybe one day we'll populate these with real values!
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_start_time" ), QVariant(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_end_time" ), QVariant(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_interval" ), QVariant(), true ) );

return context;
}

@@ -531,6 +531,29 @@ void TestQgsMapSettings::testExpressionContext()
#else
QCOMPARE( r.toString(), QStringLiteral( "WGS84" ) );
#endif

e = QgsExpression( QStringLiteral( "@map_start_time" ) );
r = e.evaluate( &c );
QVERIFY( !r.isValid() );
e = QgsExpression( QStringLiteral( "@map_end_time" ) );
r = e.evaluate( &c );
QVERIFY( !r.isValid() );
e = QgsExpression( QStringLiteral( "@map_interval" ) );
r = e.evaluate( &c );
QVERIFY( !r.isValid() );

ms.setTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2002, 3, 4 ) ), QDateTime( QDate( 2010, 6, 7 ) ) ) );
c = QgsExpressionContext();
c << QgsExpressionContextUtils::mapSettingsScope( ms );
e = QgsExpression( QStringLiteral( "@map_start_time" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDateTime(), QDateTime( QDate( 2002, 3, 4 ) ) );
e = QgsExpression( QStringLiteral( "@map_end_time" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDateTime(), QDateTime( QDate( 2010, 6, 7 ) ) );
e = QgsExpression( QStringLiteral( "@map_interval" ) );
r = e.evaluate( &c );
QCOMPARE( r.value< QgsInterval >(), QgsInterval( QDateTime( QDate( 2010, 6, 7 ) ) - QDateTime( QDate( 2002, 3, 4 ) ) ) );
}

void TestQgsMapSettings::testRenderedFeatureHandlers()

0 comments on commit 6dbf341

Please sign in to comment.
You can’t perform that action at this time.