3

I want to have the color of the lines defined by ean xpression. It has to be looked up in a csv-color-table by an Object-Nr

Layer 1 = Line Objects with 'Object-Nr'

Layer 2 = CSV Color Table with a defined 'Color' for each 'Object-Nr'

attribute( 
get_feature( 
    'CSV_Color_Table', 
         'Object-Nr'  ,  "Object-Nr" ,     'Color' ))

The expression does not find the corresponding color. I can't figure out why it is not working.

2
  • Are both Object-Nr fields the same data type? What are the values in your Color column, color names like blue, red, RGB values, HEX? Commented 8 hours ago
  • both Object-Nr. are String /// Color Table CSV : Field_1 = Object Nr. ( e.g. 3A, 3B, 3C, ... ); Field_2 = Color = HTML ( e.g. #FFC000 ) /// Commented 8 hours ago

2 Answers 2

1

Try aggregate:

I have a line layer roads with the attribute Object-Nr, and a csv table colors with the same Object-Nr column and a column with color names color_name. The expression also works with the hex column.

aggregate(
  layer:='colors', 
  aggregate:='array_agg', 
  expression:="hex_color", 
  filter:="Object-Nr"=attribute(@parent, 'Object-Nr')
         )[0]

enter image description here

1

Your expression is almost correct, you're literally just one( two) characters away from the solution: delete the last bracket and insert it before the last comma. You need a closing bracket after calling the value inside the field "Object-Nr", see line 6 in the expression below.

Function get_feature() has three (not four as in your case) arguments, whereas attribute() function in this case requires two (not one) argument.

The correct expression looks like this, where CSV_Color_Table corresponds to layer 2 and the Object-Nr as well as Color fieldnames from the corresponding layers as stated in the expression:

attribute (
    get_feature(
        'CSV_Color_Table',
        'Object-Nr_from_Layer_2',
        "Object-Nr_from_Layer_1"
    ),
    'Color_from_Layer_2'
)
5
  • Thanks @Bera but could you please be more specific about the function get_feature(layer, attribute, value): attribute = what is looked up = 'Object Nr' in the original Layer ? value = field_1 = "Object Nr" = column name in the CSV table with the 'Object Nr' ? 'Color' = the value to return from the CSV table ? Commented 3 hours ago
  • Sorry, don't understand what you mean? I thought you have these values and fields in your layer? Did you test it? Does it work? Commented 3 hours ago
  • @symbol_label || attribute ( get_feature( 'IO_Color_Table_12e2ca88_0a50_47ce_97a3_b38d42613dd7', 'INVENTORY_TYPE_KEY' , 'field_1' ), 'field_2' ) Commented 3 hours ago
  • like in Excel VLOOKUP i have to adress the cell with the value "color" in the coloumn "field 2". the object number is in the field_1 column. The corresponding Object-Number in the layer is in the colum "Inventoriy_type_Key". Commented 3 hours ago
  • Yes, try the expression I posted, based on the information you provided in your question. It should work. If not, provide more information by editing the question and adding more details, not on the comments. In your question, you used other fieldnamws, this is confusing. The expression I posted works for what you have in your initial question Commented 2 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.