Skip to content
Permalink
Browse files

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
  • Loading branch information
nyalldawson committed Nov 16, 2016
1 parent 1d538a6 commit 87d2ac3525f706759e646dba14919be529d53d9d
Showing with 4 additions and 0 deletions.
  1. +1 −0 src/core/composer/qgscomposermap.cpp
  2. +1 −0 src/core/qgsexpression.cpp
  3. +2 −0 src/core/qgsexpressioncontext.cpp
@@ -1820,6 +1820,7 @@ QgsExpressionContext QgsComposerMap::createExpressionContext() const
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), scale(), true ) );

QgsRectangle extent( *currentMapExtent() );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( QgsGeometry::fromRect( extent ) ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), extent.width(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), extent.height(), true ) );
QgsGeometry centerPoint = QgsGeometry::fromPoint( extent.center() );
@@ -5746,6 +5746,7 @@ void QgsExpression::initVariableHelp()
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." ) );
gVariableHelpTexts.insert( QStringLiteral( "map_rotation" ), QCoreApplication::translate( "variable_help", "Current rotation of map." ) );
gVariableHelpTexts.insert( QStringLiteral( "map_scale" ), QCoreApplication::translate( "variable_help", "Current scale of map." ) );
gVariableHelpTexts.insert( QStringLiteral( "map_extent" ), QCoreApplication::translate( "variable_help", "Geometry representing the current extent of the map." ) );
gVariableHelpTexts.insert( QStringLiteral( "map_extent_center" ), QCoreApplication::translate( "variable_help", "Center of map." ) );
gVariableHelpTexts.insert( QStringLiteral( "map_extent_width" ), QCoreApplication::translate( "variable_help", "Width of map." ) );
gVariableHelpTexts.insert( QStringLiteral( "map_extent_height" ), QCoreApplication::translate( "variable_help", "Height of map." ) );
@@ -752,6 +752,8 @@ QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope( const Qg
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_id" ), "canvas", true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_rotation" ), mapSettings.rotation(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), mapSettings.scale(), true ) );
QgsGeometry extent = QgsGeometry::fromRect( mapSettings.visibleExtent() );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( extent ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), mapSettings.visibleExtent().width(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), mapSettings.visibleExtent().height(), true ) );
QgsGeometry centerPoint = QgsGeometry::fromPoint( mapSettings.visibleExtent().center() );

2 comments on commit 87d2ac3

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn replied Nov 16, 2016

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

@nyalldawson

This comment has been minimized.

Copy link
Collaborator Author

@nyalldawson nyalldawson replied Nov 16, 2016

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

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