Saturday 23 March 2019

Dynamic copyright or layer credit in QGIS 3.4?


Is it possible to add a layer credit to the bottom right corner of the map and print composer in QGIS 3.4? This fuctionality existed in 2.8, as seen here.


I can't find this option in the layer properties anymore. I realize I can do something similar manually via the Copyright Decoration, but it was nice to just check/uncheck a box when needed. The Copyright Decoration is also always on top and visible.


Perhaps someone can suggest an expression to use in the Copyright Decoration that will pull the visible layer's metadata and use that as the text string? My QGIS expression skills aren't that strong yet.



Answer



Building on J. Monticolo's excellent answer, here's a method that gives attributions for all visible layers.




  • @map_layer_ids returns a list (or "array") of the visible map layers

  • array_get() returns a single item from a list

  • array_get( @map_layer_ids ,0) returns the first item from the list of visible map layers

  • concat() combines multiple text strings


For a map with eight layers in the layer panel, use this expression to combine the attributions of all the visible layers, separated with a space:


concat(
layer_property( array_get( @map_layer_ids ,0),'attribution') || ' ',
layer_property( array_get( @map_layer_ids ,1),'attribution') || ' ',
layer_property( array_get( @map_layer_ids ,2),'attribution') || ' ',

layer_property( array_get( @map_layer_ids ,3),'attribution') || ' ',
layer_property( array_get( @map_layer_ids ,4),'attribution') || ' ',
layer_property( array_get( @map_layer_ids ,5),'attribution') || ' ',
layer_property( array_get( @map_layer_ids ,6),'attribution') || ' ',
layer_property( array_get( @map_layer_ids ,7),'attribution'))

enter image description here


Notes:


Add or subtract lines so there's one for every layer in your project. When the expression is evaluated, it only applies to visible layers. By creating sure you have a line for every map layer, you cover every possible combination of enabled/disabled layers.


Note that the array is 0 indexed, so the first map layer has the index value 0, and the last map layer has the index value (total number of layers) - 1.



You can change the separator between attributions. For example, to use a semicolon, substitute ';' for ' '.


Layers without any attribution are omitted automatically. If you see extra spaces in the attribution as displayed on the map, check if any of the layers have a space in their attribution field.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...