[GIS] Shortest distance from polygon centroids to a line in QGIS

centroidsdistancegrasslineqgis

To get the distance of each polygon centroid from a line, I have tried the Distance to nearest hub algorithm from the Processing Toolbox. The distances I got from the algorithm did not seem correct, upon checking manually. I later tried the v.distance GRASS command from within the Toolbox with the following settings:

v.distance settings

It could be that I am not specifying the options correctly – for example, my "To" layer is a line and I have set the "Column where values specified by ' upload'…" to the "Descriptio" field. If this means that the calculated distances will be uploaded into the field then it could be the source of the issue. However, there is no option to create a new field to hold the distances (besides, a new layer is anyways created so I dont quite understand this setting). Nevertheless, running this command gives an error and QGIS stops working:

v.distance error

I dont know what the error is referring to. Is there some other plugin/algorithm that I can use? Alternatively, what are the correct settings for Distance to nearest hub and v.distance? I would like to achieve this entirely within QGIS, but am open to using R as well.

PS: I am running the latest version of QGIS (2.4 Chugiak) on Win 7 x64. I have already referred to the following questions on GIS SE:

Shortest distance from point to line (big dataset)

Measuring the distance between lines and points in QGIS

UPDATE: I could not get this to work from within QGIS despite trying several permutations of the GRASS v.distance settings. I finally used the gDistance function from the rgeos R package to obtain the result. However, while I have solved my problem for now, I would still like to know if one can do this is QGIS.

Best Answer

The GRASS v.distance function does currently not work from QGIS Processing. See GRASS in QGIS not working (windows XP). You will have to run it through the GRASS plugin.

Update: You could also try the NNJoin plugin. It provides an option to use the centroids of the geometries of polygon (or line) input layers. It may be slow for large datasets.

Related Question