# How to calculate the distance to the nearest water source?

I have a polygon hydro layer of my study area and am trying to calculate the distance to nearest water bodies from my site. Since my site are 2km*2km grids I am not sure how to calculate it? Whether to calculate from centroids of the grids or convert the layer to raster and somehow calculate mean distance of raster cells within the grid. Would be a great help if somebody could provide steps :)

• Do you have the shp with the water bodies and want to find the distance to nearest? – geo_dd Nov 27 '15 at 12:42
• May be you can try using distance matrix (Vector/Analysis tools). Works with point layers where each point have a unique id. You can try the centroids of the "site" against the centroid of the water bodies. Other thing you can do is convert the water bodies (polygons) to line. Then extract the nodes (point layer), assign a unique ID (field calculator either \$rownum or \$id). You can run the site's centroids against the edge created with the nodes. Check this: qgistutorials.com/en/docs/nearest_neighbor_analysis.html – Gerardo Jimenez Nov 27 '15 at 13:51

If you are working with GRASS, then you can use the module v.distance. When you import the 2kmX2km grid into grass, it automatically creates centroids for each grid cell (that's how the vector design in GRASS works). So you would first add a column in the grids table to hold the distance value, then run v.distance to update that column:

(Assuming two GRASS vector maps, 'water' and 'grid')

``````v.db.addcolumn map=grid column="dist_to_water double"
v.distance from=grid from_type=centroid to=water upload=dist column=dist_to_water
``````

View the results:

``````v.db.select grid
``````
• Thx both of you! Am trying.. I ran the command v.distance but in QGIS 2.10.1 there was no button called "View Output" to see the map that has calculated the distance :( – Yadav Ghimirey Nov 28 '15 at 18:45

GRASS v.distance in QGIS Processing is not working (see this pull request), so if you want to use v.distance you will have to use the GRASS plugin.

An alternative is the QGIS NNJoin Plugin, that for each feature in the input dataset finds the nearest neighbour in the join dataset. The resulting layer will contain all the features from the input dataset with all the attributes from the nearest join feature added, and also a new attribute that contains the distance to the nearest (join) feature.