I have a village shapefile of India and a river shapefile containing major rivers and tributaries polylines of India.

How do I calculate the distance between any village and the river nearest to it using QGIS?

  • Have a look at join attributes by nearest.
    – Erik
    Jun 3, 2021 at 10:03
  • did you mean the nearest distance , right?
    – MS.
    Jun 3, 2021 at 10:05
  • yes I need the nearest distance of village to the river
    – Tayyab
    Jun 6, 2021 at 7:56

1 Answer 1


Since QGIS 3.8 you can use Join Attributes by Nearest from Processing Toolbox (settings should be self-explaining, fields with attributes and join distance are added) and since QGIS 3.16 you can also use the expression overlay_nearest(), e.g. overlay_nearest('riverlayername',"attributename_you_want",limit:=1)[0]. This will get an attribute of the nearest river. To calculate the distance via expression, use it this way: distance($geometry,geometry(overlay_nearest('riverlayername',$currentfeature,limit:=1)[0])). Just change 'riverlayername' to the name of the layer containing the rivers.

  • 1
    Or, as a variant: length(shortest_line($geometry,array_first(overlay_nearest('riverlayername',$geometry))))
    – Babel
    Jun 3, 2021 at 10:26
  • 1
    It is sufficient, starting from the layer village: distance ($geometry, overlay_nearest ('riverlayername', $geometry) [0]) there is no need for limit: = 1 because it is by default for the closest one;
    – pigreco
    Jun 3, 2021 at 20:23

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.