6

In QGIS 3.16.2 (same on 3.16.0) on Win 10, I created a custom function get_color to get a feature's color by layer name\id and feature ID as proposed in this answer by @Dror Bogin: https://gis.stackexchange.com/a/383753/88814

The expression was successfully created as it appears under custom expressions. However, I'm not able to get the function to run - using this data (project + layers). I try to define the color of the points as the color of the nearest polygon, setting the point's color to data driven override with this expression: get_color('Buildings',array_first(overlay_nearest('Buildings',$id))). This returns an error: Evaluation error: $id. See my screenshot: enter image description here

Using array_first(overlay_nearest('Buildings',$id)) however works fine, it returns a feature no. So $id seems to be a problem together with the custom function get_color in my case.

What is wrong - is there some setting I have to change? I restarted QGIS as well as Windows.

That the solution in principle works is demonstrated by @Dror Bogin who used the very same data as me (linked above) on his Win/QGIS 3.16.1 successfully, see his screenshot, where I get the error mentioned:

enter image description here

1 Answer 1

7

After trying it on another Windows machine and playing around with the settings, I found the problem: the color of my polygon was defined as a categorized style with $id as the value to create the classes for differenty symbol colors. I changed this to an attribute value fid:

enter image description here

Like this, it works perfectly. On the point layer, the expression still is get_color('buildings',array_first(overlay_nearest('buildings',$id))), thus with $id, not with 'fid'. For some reason, the custom function had a problem to refer with $id to the polygon layer that itself used $id to set the color for the features.

As @Dror Bogin mentioned in his post cited above:

the function currently only supports single, graduated and categorized symbol types, and not rule based.

Thus this makes clear why $id did not work.

It would be great to see this custom function become part of the official QGIS code as a new expression.

4
  • 2
    It's probably possible, but I wrote a relatively basic function that uses the layer's attributes and doesn't even support rule based symbology because of this reason (rules and expressions were a hassle to parse and I wanted to do a quick and dirty function)
    – Dror Bogin
    Jan 8, 2021 at 11:01
  • It's quick but not so dirty ;-) OK, that answers also my question - in fact, you mentioned it already in the other comments yesterday. I should have been more attentive.
    – Babel
    Jan 8, 2021 at 11:02
  • Hi @Babel, I had exactly the same experience re using $id (it messes up the colours) as opposed to an attribute defined to have an ID. But I can't work out why. As you say "Thus this makes clear why $id did not work", would you be able to explain a bit more in detail why this is the case? But, or some expected behaviour?
    – puntofisso
    Aug 16 at 10:35
  • I'm quite busy right now and the problem appeared more than two and a half years before, so I don't remember the details. Sorry for not being able to give you an answer - maybe someone else will he able to do so.
    – Babel
    Aug 16 at 11:13

Your Answer

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

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