5

I am trying to apply an attribute filter to multiple layers in QGIS at once. The filter is to be applied to .osm data brought in through PostGIS to QGIS.

I have tried selecting multiple layers -> right-click --> filter but it only applies it to the last layer I selected of the group. I want to be able to apply a filter through a query like "osm_user" = 'donlaser' to multiple layers at once, rather than having to repeat the process many times.

Is there a standard way to do this within QGIS that I am missing? Is there a plugin that would provide this functionality?

A Python-based solution would also be useful for me.

  • Are you Ok if I give you a (short) solution with Python? – Germán Carrillo Feb 12 '15 at 23:53
  • Yes. I would be open to trying a Python solution. – traffikHam Feb 13 '15 at 0:04
6

This is how I'd do it with PyQGIS:

In your QGIS ToC, turn off those layers that you want to keep untouched, i.e., leave visible only the layers you want to set the filter to.

Now open a QGIS Python console and paste this code:

layers = iface.mapCanvas().layers()
for layer in layers:
    layer.setSubsetString('"osm_user" = \'donlaser\'')

This should do the trick.

  • That worked, but it really isn't a final solution for me. How would I apply it to every layer in a specific layer group? – traffikHam Feb 13 '15 at 0:51
  • If I get you right, just make that specific group visible and all the other groups invisible. Otherwise, please edit your question and please make that clearer. – Germán Carrillo Feb 13 '15 at 0:56
  • yeah you are right, I could achieve that just by toggling group layer visibility, thanks so much! – traffikHam Feb 13 '15 at 1:34
  • That's great! You're welcome. – Germán Carrillo Feb 13 '15 at 1:57
  • The backslash is a escape, it helps the interpreter to deal with the subsequent single quote in a different way, so that it is acknowledged as a single quote included into the expression. If you omit the backslash, the single quote finishes the expression and is not included there, resulting into: "osm_user" = I guess python would raise a syntax error or, the expression will not be applied successfully. – Germán Carrillo Feb 17 '16 at 16:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.