8

I'm using QGIS 3.16 Hannover and I've got a vector layer of provinces with animal species names as attributes. If the species has been detected in the province, the value of the attribute is X; if there are breeding records, the value is R; otherwise the value is NULL.

I'm producing a map for each species and I'm using the categorized layer style for displaying the provinces where the species is present. Is there a way of automatically display the selected attribute name (i.e. the species) in the print composer?

enter image description here

2 Answers 2

12

I think that Atlas feature is what you seeking for (see Documentation). For this case I have a workaround with Atlas. Trick is that instead of iterating through geometries I use list of former field names (species names):

  1. Create .csv with field names in one column and load it into QGIS enter image description here

  2. Set this csv layer as Covarage layer in Atlas settings (layout window) and as Page name choose the column with the species names. enter image description here

    The Page name will be used to identify the atlas page, and also its values can be referenced in expressions using the @atlas_pagename variable. You now have the Atlas set up that will generate one page for each species name.

  3. Set the polygon layer symbology - as Value for categorized symbology use this expression: attribute(@atlas_pagename)

    The attribute() expression returns value of attribute of given field name (i.e. attribute('species_1') returns the same result as when you directly select the attribute species_1). However, we need this value to change (species_1, species_2 etc.) according to the page of the atlas. Thus we have to use @atlas_pagename variable which returns value of page name of current atlas feature. This causes iteration through the fields of the polygon layer because their names are identical to those in the Page name of the Covarage layer (csv table) used in atlas.

    Now you have to manually add the two categories and set their symbols (other solution is use of Rule-based symbology with expression such as attribute(@atlas_pagename) = 'X' and so on)

enter image description here

  1. Prepare your layout. For displaying the species name just add text (Add Label) and select the name of the field with species names.

enter image description here

Preview the result:

enter image description here

2
  • 1
    Your solution really works, but I can't actually understand WHY does it work. Could you please explain the process underneath step 3?
    – jpinilla
    Commented Feb 8, 2022 at 14:17
  • 1
    @JesúsPinilla Of course, sorry I was focused on writing the solution and missed some details (also in step 2). Also use of $currentfeature in expression is unnecessary in this case. Hope, it is more understandable now.
    – Oto Kaláb
    Commented Feb 9, 2022 at 12:32
6

One option can be create a new project variable, in the Variables tab of the Project Properties menu. And store in the value of that variable the name of the field to apply the symbology.

For example, you can create a new variable named project_symbology_field and store the value Taeniopygia castanotis.

At the symbology of the layer, the value must be the expression:
eval( @project_symbology_field)

At the title of the legend, or in a text item or where you want the name of the field, you can use the expression: @project_symbology_field

To change the field to symbolize, you need to update the value of the variable with other field name.

You can also use some categories instead of the field names, and use a expression like:

CASE
  WHEN @proyect_symbology_field = 1 THEN "Taeniopygia castanotis"
  WHEN @proyect_symbology_field = 2 THEN "Other field name"
END
1
  • Thank you for the idea, but I think it doesn't make much a difference than typing the species' name manually in each map, unless I'm missing something.
    – jpinilla
    Commented Feb 5, 2022 at 10:58

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.