QGIS – Using Attribute Tables as Variables

attribute-tablefields-attributesqgisqgis-variablevariables

I work for a company where we use the same file when starting a project, with all the print layouts already configured, including the names/themes of the maps, since we usually need the same themes. Each map is also associated with a number in the attachment list (a little text is displayed with this number). It goes something like:

  • 1 – Hidrography map – Attachment 1
  • 2 – Population map – Attachment 2
  • 3 – Topographic map – Attachment 3

enter image description here

Sometimes we don't need to print some specific maps on the list or need to change their names, or the order amongst them, so we started to circulate their names with variables, such as:

map_1 = returns 'Hidrography map' and att_map_1 = returns 'Attach. 1'

map_2 = returns 'Population map' and att_map_2 = returns 'Attach. 2'

map_3 = returns 'Topographic map' and att_map_3 = returns 'Attach. 3'

That saved us a lot of time because this title appears more than once in the layouts, and we don't need to open every single one to modify their names/attachment number, we just need to open the variables window and change it from there.

The thing is, variables are independent of each other, meaning they don't correlate with each other, as feature values are linked with their IDs/features, for instance. So, I was wondering if there is a way to work with an attribute table as we do with variables.

For example, instead of linking map_1 and att_map_1 to return the name and attachment number of that map, I could link the feature "id" from a table and it would identify the collums "map_name" and "attach_nbr", as it is shown in the picture below. It could also contain other personalized info from the maps, such as the fonts f.e.

enter image description here

Basically, it would be easier to control variables if they were displaced on a table and if they could be related somehow. The variables are a great tool, but when you're dealing with +50 print layouts, it gets a little tricky to keep everything on track.

Does someone know if this is possible?

Best Answer

Absolutely, you can set a variable in your map layout that correlates to the unique key of the map layout attributes table loaded in your project. Then use attribute() to get the corresponding values.


Example: I have set up a layer called table with the following columns. The unique id is in the maptype column.

enter image description here

In my print layout settings, I scroll down to the Variables panel and add a new variable maptype.

enter image description here

All throughout the map I make reference to the variable (@maptype), the table layer ('table'), the corresponding ID column ("maptype") and the attribute I want out of the table ("ATTRIBUTE") using an expression like:

attribute(get_feature('table', 'maptype', @maptype), 'ATTRIBUTE')

Note: all table and field references are case-sensitive.

Anywhere you can use expressions, you can make use of the above - text, map scale, map themes, rotation...

Then whenever you need to change the variable value just type in the new one and press Refresh.


Example result:

enter image description here

Related Question