2

I have a shapefile with a lot of polygon features in it. For each polygon, I want to calculate the distance from the centroid of each polygon to a certain coordinate.

How can I achieve this using the field calculator?

enter image description here

See the added picture for an example: For every feature, I want the distance from its centroid to the red dot (CRS projection I used is EPSG28992, but regular lon lat is also fine. As long as the distance gets calculated).

4

I would use something like:

distance( centroid( $geometry ),  geom_from_wkt( 'POINT(350000 650000)'))

if you have a simple fixed point.

2
  • Everyone gave great suggestions and the problem was fairly simple i assume. I ended up using this solution, since it was the most intuitive for me. Thanks! Mar 19 at 10:33
  • 2
    lol ... there was something like a "race" for answering ... that's the beauty of SE ...
    – Snaileater
    Mar 19 at 11:18
3

Something like the following should be working :

distance(st_centroid($geometry),getfeature(point_layer_name, id, 1)

getfeature(point_layer_name,id, 1) is a way to retrieve your 'reference' point (based on its id=1 in that case) ... adapt it to your table's structure ...

1
  • distance($centroid,make_point(x-coord,y-coord)) works, too.
    – Erik
    Mar 19 at 10:12
2

Use this expression: length (make_line (centroid ($geometry), make_point ([x], [y]))) replace [x] and [y] with the x- and y-coordinates values of your reference point. Be sure to have an appropriate CRS (a projected one) and use the same CRS for your points and the coordinates of your reference point.

2

I think you are looking for something like:

  1. using the geom_from_wkt()

    length(shortest_line(centroid($geometry), geom_from_wkt('POINT(508503.622 5406706.511)')))
    
  2. or using the make_point()

    length(shortest_line(centroid($geometry), make_point(508503.622,5406706.511)))
    

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.