4

I have 20 thematic maps with different sources like CITY PLANNING OFFICE, ENVIRONMENT OFFICE, AGRICULTURE OFFICE etc.

Since I have many maps, I want to automate the data source citation of my thematic maps by simply having a code or process. I want that each map layout, the data source will change based on the map that is being viewed. This will lessen the burden because I am not having to manually type the data source for each map layout.

I saw someone discussed about variables and it got my hopes high but it didn't work for me because the layer variables cannot be used in text label in map layout. Only the Global and project variables were working.

enter image description here

3
  • 1
    This screenshot looks like QGIS print composer to me, not ArcGIS desktop as your tag suggests. Confirm? Commented Dec 4, 2018 at 7:35
  • retagged this accordingly. Commented Dec 4, 2018 at 8:41
  • ye, it automatically tagged arcgis.
    – kalaw23
    Commented Dec 6, 2018 at 8:59

3 Answers 3

8

You should use the layers Metadata for it. In the Layers properties, fill in Attribution - Title string like shown below (you may use the Url too, as well):

enter image description here

Then insert the following expression into your label item (note the output preview in the screenshot and the arguments description of the layer_properties function on the right, you may access layer name, feature count and many more, no need for custom functions):

layer_properties(layer_id, 'attribution')

enter image description here

The layer_id must be in single quotes, you may obtain it either from the layers variables (but you cannot copy and paste it from there) or via the python console (with the desired layer selected):

iface.activeLayer().id()

enter image description here

3
  • Thanks for this, Jochen. Very useful! I wasn't aware of this when I wrote my answer. Just to let you know, you can copy the layer_id from the variables by selecting it and using Ctrl + C (right-click does not work).
    – Matt
    Commented Dec 4, 2018 at 10:32
  • thx, we all keep on learning! ;-) Commented Dec 4, 2018 at 11:37
  • Do I still need to manually update the expression every time I change the active layer? layer_property('LAYER','ATRRIBUTION')
    – kalaw23
    Commented Dec 6, 2018 at 9:04
4

If the source is the layer name you could define your own custom function and use it as an expression in the label content dialog.

Click "Insert an Expression..." in the Main Properties of the label. Select the function editor tab and paste the following. Click "Save and Load Functions".

# Be sure to import iface from qgis.utils

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

@qgsfunction(args='auto', group='Custom')
def get_layer_name(feature, parent):
    return qgis.utils.iface.activeLayer().name()

Then back on the Expression tab of the "Insert an Expression..." dialog double click the get_layer_name function (under Custom) to add it to your expression.

After clicking OK, the name of active layer in your main QGIS window will appear in your label. You will have to refresh the Layout Manager to update the name after a different layer is selected.

4

If you have many layers in your map and you would like to build the attribution label based on layers which are visible, it's possible with QGIS 3.4.

In QGIS 3.4, you can build your attribution label automatically, according to layers which are visible.

First, you need to setup metadata for all your layers.

In QGIS 3.4.0 or 3.4.4, you had to use the QGIS Server panel and fill the attribution field: QGIS Server panel

Since QGIS 3.4.5, you can use the Metadata panel: Metadata panel

In my project, I have 3 layers, but 2 layers have the same attribution Provider 2.

Then, in your layout, give an itemID to your map:

itemID for map

Then, in your label, you can create an expression like this:

array_to_string( -- 4 we make the attribution list unique and convert to string
    array_distinct(
        array_foreach( --2 for each layer in the map1
            map_get(item_variables('map1'),'map_layers')  --1 get the list of layers in the map1
            ,array_to_string(
                -- 3 we fetch the attribution for the layer
                layer_property(@element,'attribution')))))

Try to understand the expression by reading comments from 1 to 4.

This will render the correct attribution based on layers which are visible in my map. It will also remove attributions which are the same across layers.

my label with expression

In QGIS 3.6.0, you can add the array_sort expression to sort alphabetically.

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.