3

I have a QGIS project with N layers, each pointing to the same CSV file with columns "1_1, 2_1, ..., N_1".

QGIS Layers and Attribute table

I would like each layer to refer to its respective column (Layer 1 -> column 1_1, and so on) and display points based on the Graduated style I have set. I am able to get the result I am looking for by manually selecting the Column in each Layer Style for every layer like so:

Layer Properties

However, this is very time consuming so I am trying to figure out a way to use Expressions to relate the layer name to the respective column name. I have tried the following expressions with no success:

  • @layer_name+'_1' (Valid expression but every point has the same value and style)
  • "@layer_name+'_1'" (Valid expression but null output)
  • "concat(@layer_name,'_1')" (Valid expression but null output)
  • column "@layer_name+'_1'" (Invalid expression)
  • column name "@layer_name+'_1'" (Invalid expression)

I am using this as reference: https://docs.qgis.org/2.8/en/docs/user_manual/working_with_vector/expression.html

Is there something I am misunderstanding about expressions in QGIS?

This is my desired result: (One layer visible)

Desired result

This is the closest I was able to get using the first expression:

Expression 1

2

You can't build the column name directly, as the styling function expects a column (i.e the data), not a string.

However, you can build an expression that returns the values to be classified. The expression would read the current feature (each of them in fact), build the column name, and return the value:

As you did, under the column expression, enter:

attribute($currentfeature , @layer_name ||  '_1')
  • This works! Thank you for the help and explanation. – evildoesit May 24 at 20:55
  • I have a follow up question - your solution works when I apply it. However, when I rename a layer the column will not change until I open the layer properties and apply the style again. Is there a way to refresh the styles for all layers without going through each one? Thanks! – evildoesit May 24 at 21:12
  • 1
    I believe I have found a solution to my follow up question. This command in the Python Console refreshes the styles: iface.mapCanvas().refreshAllLayers() – evildoesit May 24 at 21:26
  • Unfortunately it doesn't look like the expression passes correctly to the plugin I am using. I've posted another question: gis.stackexchange.com/questions/324177/… – evildoesit May 28 at 17:16

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.