1

There is layer with polygons. I want to save in new field distance of every polygon's centroid to centroid of one particular polygon.

To do this I should write some expression like this:

distance(centroid($geometry), get_feature(6679)) # 6679 is feature id of some polygon

According to weak documentation get_feature returns the first feature of a layer matching a given attribute value. Writing it like this

get_feature('id', 6679)

results into "Parsing Error: Function is called with wrong number of arguments".

Who knows the right way to get geometry of some polygon with given feature id?

4

The function you are looking for is actually getFeature (at least in QGIS 2.8). It takes 3 arguments (hence your "Parsing Error") : the name of the layer, the name of the field, and the value of the field. So it would be something like getFeature( 'your_layer', 'id', 6679 ).

Additionnal point : you actually have to use geometry(getFeature( ... )) in your expression, because distance takes geometry arguments. In the end, you will have :

distance( centroid($geometry), geometry(getFeature('your_layer', 'id', 6679)) )

1

Your answer, actually helped, but I didn't find a way to get feature by feature id. So I created new field ID containing feature id, expression is simple:

@row_number 

And then solved my task

distance(centroid($geometry), centroid(geometry(getFeature(@layer_name, 'ID', 6609))))

Thanks!

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.