I have a points layer and a polyline layer.

The points layer is basically bus stops and the line layer is the road network. The bus stops may or may not fall exactly on the road network layer. I want to create a column in the point layer which contains the ID of the nearest link and also the distance from the link's head node or tail node.

That means I need to find the nearest link wrt each node and also measure the distance of the point on the line from the line's head or tail node.

What tool in QGIS will help me get the above things done?


1 Answer 1


Below, I propose a Field Calculator approach to obtain the distance from the head of the nearest route.

This approach has two requirement:

  • Need to install refFunctions plugin which provides geomnearest function.
  • Your road network has to have an unique id field (e.g. "id", "fid", "road_name",...)

Start the Field Calculator on your bus_stops point layer and create a new field with:

line_locate_point(geometry := geometry(get_feature('routes', 'fid', geomnearest('routes', 'fid'))), 
                  point := $geometry)

The above expression is based on assumption that your road network polyline layer's layername is routes and it has unique fid field. Please modify them accordingly.

enter image description here


As to the question, "I want to find the nearest line and also add its ID in the stop feature", please create another field on your point layer by:

geomnearest('routes', 'fid')

enter image description here

  • Hi, thank you for your response. I now have two issues. First is, I tried the above function, although the stops seem to be around 50 m away from the link, the distance I got from the function is 0.22. I am working with WGS84: EPSG4326. I am not sure why this is happening. The second issue is I want to find the nearest line and also add its ID in the stop feature. Is it possible to do? Apr 26, 2020 at 12:33
  • Hi @GaneshAmbiRamakrishnan (1) You will need to reproject the layer to your local coordinate system (CRS). (2) Indeed it is possible, but you will need to post it as new question. Make sure to include information about your data (layername and attribute).
    – Kazuhito
    Apr 26, 2020 at 12:41
  • Ah, your second question "find the nearest line id" was actually just a part of the original question. Let me add that to my answer. Sorry about that, @GaneshAmbiRamakrishnan
    – Kazuhito
    Apr 26, 2020 at 13:40
  • Hi, I tried the second part of your answer and worked like a charm. For the first one, I do not know how to project to a local coordinate system (meaning which system must I specify?). I am working with a dataset from India if that helps. Apr 27, 2020 at 1:00
  • I usually go for UTM, but India is huge and its area spans zones 42 - 46. Can I ask whereabout in India?
    – Kazuhito
    Apr 27, 2020 at 3:09

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.