Skip to content

Commit 87d2ac3

Browse files
committedNov 16, 2016
Add @map_extent variable containing geometry of current map extent
...Because I've seen a lot of ugly workarounds to recreate this extent using wkt/buffers/etc
1 parent 1d538a6 commit 87d2ac3

File tree

3 files changed

+4
-0
lines changed

3 files changed

+4
-0
lines changed
 

‎src/core/composer/qgscomposermap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,7 @@ QgsExpressionContext QgsComposerMap::createExpressionContext() const
18201820
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), scale(), true ) );
18211821

18221822
QgsRectangle extent( *currentMapExtent() );
1823+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( QgsGeometry::fromRect( extent ) ), true ) );
18231824
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), extent.width(), true ) );
18241825
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), extent.height(), true ) );
18251826
QgsGeometry centerPoint = QgsGeometry::fromPoint( extent.center() );

‎src/core/qgsexpression.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5746,6 +5746,7 @@ void QgsExpression::initVariableHelp()
57465746
gVariableHelpTexts.insert( QStringLiteral( "map_id" ), QCoreApplication::translate( "variable_help", "ID of current map destination. This will be 'canvas' for canvas renders, and the item ID for composer map renders." ) );
57475747
gVariableHelpTexts.insert( QStringLiteral( "map_rotation" ), QCoreApplication::translate( "variable_help", "Current rotation of map." ) );
57485748
gVariableHelpTexts.insert( QStringLiteral( "map_scale" ), QCoreApplication::translate( "variable_help", "Current scale of map." ) );
5749+
gVariableHelpTexts.insert( QStringLiteral( "map_extent" ), QCoreApplication::translate( "variable_help", "Geometry representing the current extent of the map." ) );
57495750
gVariableHelpTexts.insert( QStringLiteral( "map_extent_center" ), QCoreApplication::translate( "variable_help", "Center of map." ) );
57505751
gVariableHelpTexts.insert( QStringLiteral( "map_extent_width" ), QCoreApplication::translate( "variable_help", "Width of map." ) );
57515752
gVariableHelpTexts.insert( QStringLiteral( "map_extent_height" ), QCoreApplication::translate( "variable_help", "Height of map." ) );

‎src/core/qgsexpressioncontext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope( const Qg
752752
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_id" ), "canvas", true ) );
753753
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_rotation" ), mapSettings.rotation(), true ) );
754754
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), mapSettings.scale(), true ) );
755+
QgsGeometry extent = QgsGeometry::fromRect( mapSettings.visibleExtent() );
756+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( extent ), true ) );
755757
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), mapSettings.visibleExtent().width(), true ) );
756758
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), mapSettings.visibleExtent().height(), true ) );
757759
QgsGeometry centerPoint = QgsGeometry::fromPoint( mapSettings.visibleExtent().center() );

2 commit comments

Comments
 (2)

nirvn commented on Nov 16, 2016

@nirvn
Contributor

Shouldn't this be added for map canvas too? Or is it already available?

nyalldawson commented on Nov 16, 2016

@nyalldawson
CollaboratorAuthor

There is no map canvas scope - it uses the same map settings scope.

Please sign in to comment.