A QGIS Composer atlas can be generated from a subset of the coverage layer features using the coverage layer filter control. I can filter for coverage layer features by comparing an attribute of the features to a static value, but not by comparing to a project variable (with the same value). Can this be done? What is the syntax of the expression? Is it just not possible for some reason? Is there a work-around?

Specific situaion

I have QGIS print composer set up to generate an atlas. This uses a coverage layer 'Locations'. In the 'Locations' data is a field 'Project'. I want to print atlas pages for any feature in Locations where "Project" = '1'. Entering "Project" = '1' into the atlas coverage layer filter field works fine - I get atlas pages.

So far so good, but I'd like to be able to change the value '1', using a project variable @myvariable in its place.

Entering "Project" = @myvariable into the filter field results in no atlas pages (an error appears) even if @myvariable has the value 1. I've tried numerous changes in syntax without success.

Images to clarify question:

A Composer atlas can use select features (rather than all features) from the coverage layer - using a filter - as shown (the filter is set to "ProjectID"='1'):

Showing a coverage layer filtered using "ProjectID" = '1'

Using a variable in the filter seems not to work - the error is shown here when filtering using ("ProjectID"="@currentprojectUPK):

Showing a coverage layer filtered using "ProjectID" = @CurrentprojectUPK

It can be seen that the variable @currentprojectUPK has the value '1' in the list of variables as shown here:

Showing that the variable @currentprojectUPK has the value '1'

I wondered if somehow the Composer couldn't access the variable values, but this can be used elsewhere (for example in a text box) successfully. I also wondered if there was an issue with the variable being a string not an integer, but changing it to an integer in the statement didn't help - and anyway I'm using the value successfully elsewhere.

  • check this answer let me know if you need more info: gis.stackexchange.com/questions/173399/… Jun 21 '17 at 23:37
  • The situation in that question is different. The atlas in the example is (I think) set to show ALL features in a layer 'schools', and they are using the attribute 'SCHNUM' from the features to determine the visibility of other layer features using styling. I, on the other hand, want atlas pages only for limited features from my layer ('Locations'). I can do this using the atlas coverage layer filter if I write the value here by hand. But I'm trying to develop something where a novice user could set the value using a button. I thought setting/using a project variable would be a simple solution. Jun 22 '17 at 16:14
  • So you're looking to assign a project variable then expose a custom box in the GUI that would link to the atlas page...? Jun 22 '17 at 16:22
  • I'm able to set a project variable no problem. This is working fine, and I use it in other ways (showing/hiding features for example). I can use a numerical value equivalent to the variable value to limit (with the filter function) what coverage layer features are used in the atlas - to select for only certain coverage layer features. The problem is that when I try to use the project variable in the filter statement it doesn't work. I may try to add some images to explain, but I think the question is essentially a simple one about a very specific issue. Jun 22 '17 at 16:37
  • 1
    Just a note to say that in the end I concluded that this isn't possible. If I understand correctly variables are understood within QGIS expressions because the evaluation of the expression takes place within QGIS - but filtering takes place on the data before it arrives in QGIS - with the expression evaluated by the data provider (before the data arrives in QGIS). This is certainly the case when filtering a whole data layer. It seems to me that filtering in the composer works in a similar manner. BUT this is all guesswork on my part. I'd be pleased to be proved wrong. Jan 31 '18 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.