1

QGIS 3.16

Looking to insert an expression to my layer, Survey Coverage (text), in the legend. I want dynamic text to reflect the attribute value from the field, VIS_RADIUS (Integer64) so it looks something like:

Survey Coverage (20 m)


Survey Coverage map layer id: Spatial_SurveyCoverage_Mission

Field: VIS_RADIUS

Map Item ID: Working Map

At present I have testing different expressions in the Legend Item Properties as follows:

Survey Coverage ([%to_string(display_expression(@Spatial_SurveyCoverage_Mission, "VIS_RADIUS"))%] m)

Survey Coverage ([%to_string(display_expression(@Spatial_SurveyCoverage_Mission, "VIS_RADIUS", map_get(item_variables('Working Map'), 'map_extent')))%] m)

Survey Coverage ([%layer_property('Survey Coverage', 'VIS_RADIUS')%] m)

All result in...

Survey Coverage ( m)


Ultimate goal is to reduce the amount of revisions to layouts for multi-map projects which I can share with colleagues to be more user-friendly.


Edit: Additional dynamic text using SQL to update as Map Extent is updated/moved

Map Canvas enter image description here

Map Layout Text Box Properties

[% map_get(item_variables('Working Map'),'map_crs_description') %]
Base Map Source: ESRI World Topo
Map data sources from BC Gazetteer [%layer_property(@NTS, 'title')%] [%map_get(item_variables('Working Map'), 'map_layers')%]
[%attribute(get_feature('nts_snrc_50k', 'NTS_SNRC', 'IS NOT NULL'), 'NTS_SNRC')%]
[%@map_layers%]

As seen in the below image, the 'map_crs_description' reveals text in a way I want. However, the text after BC Gazetteer does not appear in the text box display.

enter image description here


RE: aggregate expression

Solution provided yields no result...

Survey Coverage Layer Properties > Variables

note: the "display as" in Source is greyed out, unsure why or if it even effects this at all.

enter image description here

1

2 Answers 2

1

You can use the aggregate function for that, something like the expression

survey radius: [% aggregate( 'Survey Coverage', 'sum', "VIS_RADIUS")%] m

will get the aggregated sum of all values within the field VIS_RADIUS. If you want to limit it on the visible area in your Map, you can use the filter criteria of the aggregate function and intersect the geometries of the layer with the mapextent.

enter image description here

2
  • I'm not sure why, but entering this into the legend properties for that layer and into the text box does not work. I even tried setting it to the layer_id. Is 'Survey Coverage' in your example here the layer or layer_name value? My 'Survey Coverage' only appears in layer_name (see edit above)
    – ArchMarmot
    Jul 14, 2022 at 22:14
  • the aggregate takes the layer name like it is displayed in the layer tree. you can test the function anywhere, like in the normal expression window. when it works there it should work in the layout anyway. for some reasons, in the layout sometimes the aggregate shows no result. the I normaly test it in a second QGIS session with the same layer to be sure, that the expression is correct.
    – eurojam
    Jul 15, 2022 at 4:17
1

I understand your want to get only one specific information and not an aggregate like a sum or count. So here is two solution, only working if you use atlas.

Solution 1

Condition : your atlas coverage layer is your layer containing the field VIS_RADIUS.

You can access attribute of your current atlas feature really easily. Here is the expression for your example :

attribute(@atlas_feature, 'VIS_RADIUS') 

Solution 2

Condition : your atlas coverage layer share a common attribute with your layer Spatial_SurveyCoverage_Mission.

I assume your atlas coverage layer has field named survey_coverage_id to connect with the field id of your layer Spatial_SurveyCoverage_Mission. For your layer Spatial_SurveyCoverage_Mission the id must be unique.

Here is the expression to get the value you want :

attribute(
 get_feature(
  'Spatial_SurveyCoverage_Mission',
  'id',
  attribute(@atlas_feature,'survey_coverage_id')
 )
,'VIS_RADIUS')

You can adapt your expression to your layer name and field name.

1
  • I will need to explore how to use the Atlas more, at a preliminary glance, I don't know if it will allow multiple atlas features to be created. Another goal is to have multiple dynamic text items that will update depending on which features are visible in the map layout (item id: 'Working Map'). Another example being NTS (50K) map layer to display attribute value(s) (eg: 082L03) in the 'NTS_SNRC' field. Please let me know if this needs elaboration, I'll try uploading a screenshot if it helps.
    – ArchMarmot
    Jul 14, 2022 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.