I am looking for the distance of the closest point (Layer 1) to my atlas object (Layer 2). I want to use the expression builder.

Example: I have a House, which is also the atlas_feature, on 'Layer A'. I have bus stations which are on 'Layer B'. I want to get the distances to the closest Bus Station on 'Layer B'.

For getting the Name of the closest Busstation on 'Layer B' I am using reffunctions geomnearest. However, I also need the distance to the closest one.

  • 3
    Something like that : distance($geometry, geomnearest('Busstation','$geometry')) ? Nov 20, 2020 at 16:51
  • geomnearest gives out an attribute (e.g. the name of a Busstation) so I think it cannot be used with distance like that, since the distance expression is looking for a geometry. Maybe something with the aggregate expression?
    – Corninski
    Nov 23, 2020 at 12:32
  • 1
    Look at the result of geomnearest('other_layer_name','$geometry'), it returns the geometry field / attribute of the other layer, so, geometry. Nov 23, 2020 at 13:10
  • 2
    Yes, I tried and geomnearest returns the result as a WKT (string). So, just add the function geom_from_wkt as : distance(geom_from_wkt(geomnearest('Haltestelle_KBS', '$geometry')), $geometry). It'll work. Nov 25, 2020 at 12:50
  • 2
    In a QGIS expression builder (selection or update), you have in the bottom left corner a Preview text : docs.qgis.org/3.16/en/docs/user_manual/working_with_vector/… Nov 25, 2020 at 16:39

1 Answer 1


I used the following:

  • 1
    Maybe I misunderstand, but for me it's unnecessary complex. Nov 23, 2020 at 14:56

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.