I have 2 shapefiles - one is a series of points, the other is a polyline. I want to draw a straight line between each point to the nearest point on the polyline.

This question has already been asked with regards to ArcGIS. The solution provided by Jeff Berry looks to be exactly what I need:

Creating a line (closest vertex to line)

Unfortunately, I have never worked with python, and have only a basic understanding of coding through Linux.

My questions:

  • Is anyone willing and able to modify Jeff Berry's solution to fit QGIS?
  • Or is there an alternative way to create a line between points and polyline, hence avoiding python?*

*NOTE: A useful suggestion is provided here:

QGIS: creating lines from starting to arrival points?

However I've not been able to identify the coordinates of the nearest neighbor - if anyone knows how to do this, I can proceed using the non-python solution.


2 Answers 2


You could try the GRASS function v.distance (https://grass.osgeo.org/grass70/manuals/v.distance.html / https://grass.osgeo.org/grass64/manuals/v.distance.html).

The "Create a new vector map" section says:

Create a new vector map which contains lines connecting nearest features of maps "pnt" and map "ln". The resulting vector map can be used for example to connect points to a network as needed for network analysis:

v.distance -p from=pnt to=ln out=connections upload=dist column=dist

Unfortunately, the QGIS Processing version of v.distance is currently not working (https://github.com/qgis/QGIS/pull/2009, http://hub.qgis.org/issues/12817), so you will have to access this GRASS function from QGIS using the GRASS plugin.

  • I updated the answer with links that indicate that GRASS v.distance is not working through Processing. But since in this case ("out=connections"), the input dataset should not be updated, it could work (but I have not been able to make it work on QGIS 2.8.2, Processing 2.10.1, GRASS 6.4.3). @Joseph, Could you explain how you made it work in Processing. Sep 15, 2015 at 13:47

A similar question was asked before which aims to find the Nearest neighbor between a point layer and a line layer, with an answer provided by @scw which uses the Shapely Python package. This might help put you in the right direction.

Alternatively, there is a Distance to nearest hub tool in QGIS which can create lines between a point source and its closest destination hub. You can use this from the interface or by calling it through python:

import processing
processing.runalg('qgis:distancetonearesthub', points, hubs, field, geometry, unit, output)

Hope this helps!

  • Ok that's excellent Joseph, thank you very much for your reply. I'll give it a try and report back.
    – Cai Ladd
    Jun 10, 2015 at 13:12
  • Most welcome buddy! Although if it's not what you're looking for then hopefully others can advise =)
    – Joseph
    Jun 10, 2015 at 13:14
  • Hi Joseph, Ok I've given it a crack, but unfortunately wasn't successful. May be for a number of reasons, but first, here's what I entered: processing.runalg('qgis:distancetonearesthub', "/User/Ladd...etc..shp","/User/Ladd/...etc...shp","id",1,0,"/User/etc...shp") Error read: Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'processing' is not defined Is this because I need to install a plugin first to work? The documentation website has no extra information on using the command!
    – Cai Ladd
    Jun 10, 2015 at 16:42
  • Ahh my apologies, I believe you have to call the processing plugin by using import processing first and then call the algorithm. I've edited my post.
    – Joseph
    Jun 11, 2015 at 8:51
  • 1
    Ah ok! grand I'll give it a go. I've read that this plugin is part of MMQGIS plugin, which has a "distance to hub" function. I used that tool but unfortunately, it locates the centroid of a nearest object, rather than the actual nearest edge of a line... so for this reason I don't think the tool will work (though I'll still give it a try, I want to learn how to use python commands for my own experience!). I've now used an alternative way to solve my problem - I'll post it as an answer. But thank you anyway fro your help :)
    – Cai Ladd
    Jun 11, 2015 at 9:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.