5

With QGIS expressions, if I have a feature, I can get the corresponding geometry with geometry (feature). How is it possible the other way round: if I have a geometry to get the feature (or feature id) of this geometry? Unfortunately, an expression like feature (geometry) does not exist. This could be useful to access attributes of this geometry.

If a feature consists of three parts: 1) feature-id, 2) attributes and 3) geometry, than you have expressions to get the corresponding feature for the first two of these three inputs: 1) get_feature_by_id(layer,feature_id) , 2) get_feature(layer,attribute,value). There is missing something like get_feature_by_geometry (geometry).


Just to illustrate a use case (the question is not about this example: it should just help to understand what this could be used for). I have a point- and a line layer, both in red on the screenshot. To each point, I want to assign the id (or any other attribute) of the nearest line.

I can easily get the geometry of the nearest line: the black dotted copy of the original red line, created with geometry generator on the point layer: array_first ( overlay_nearest ( 'line', $geometry ) ). To access the attributes of the line, there is the function attribute ( feature , attribute_name ) . But I first should be able to get the feature-id of the geometry to be able to paste it as first arguement.

QGIS does not accept to input a geometry for the feature part of the expression, I get an Evaluation error: Cannot convert to feature.

enter image description here

3
  • Could you expound your a geometry to get the feature part? -since in my understanding feature = geometry + attributes. If you mean to get the attributes of a particular feature, there are attribute() and attributes(). E.g to get "fid": attribute($currentfeature, 'fid'), or attributes($currentfeature)['fid'] if you prefer filtering. Sorry if I misread the question.
    – Kazuhito
    Jan 24, 2021 at 0:38
  • @Kazuhito: I updated the question to make clearer under what circumstances in a more complex expression, you access a geometry - not the one of the current feature - and you want to get the associated feature id to access the attributes.
    – Babel
    Jan 24, 2021 at 10:17
  • 1
    $currentfeature was just meant for a concise example, but I see your point now; we were talking about the same thing.
    – Kazuhito
    Jan 24, 2021 at 11:13

1 Answer 1

5

You can access the feature-id by using $id instead of $geometry and then use get_feature_by_id() to turn this feature-id integer value into an actual feature of a layer. This can then be used to get the attribute() or attributes(). As a whole:

attribute(get_feature_by_id('line',array_first(overlay_nearest('line',$id))),'fieldname')

You can also skip the get_feature_by_id() part by using $currentfeature instead of $id:

attribute(array_first(overlay_nearest('line',$currentfeature)),'fieldname')

Your Answer

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

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