# Finding line with minimum distance to point and update attribute of points?

I am currently working with QGIS 2.18 and I have two layers: one layer is filled with lines which represent streets. The second layer describes the location of sensors which are represented as points like in the image below: Each point has its own attribute values like followed: As you can see, the attribute edgeid is still empty and I want to fill it with the id of the nearest street. Each line in the street-line-layer has an edgeid as followed: Now I will have to determine the street with the shortest distance to the point and store its id into the edgeid-attribut of my point. The distance between point and line should be calculated as illustrated (d=distance; AB is the street vector and P is the point): I have already tried the Distancematrix in QGIS but it seems that it can only be done between points. And I do not want to convert the lines into points.

Another thing I tried was the v.distance()-algorithm (GRASS) but I couldn't get it to work. I tried the following configurations for executing the algorithm: For your better understanding: The tiger:graph is the line-layer and the EVIS_Stat_Sensorik_... is the point-layer. I want to calculate the nearest line (or edge) for each point and write its id into the attribute "edgeid" of the point. Is this even possible with this function? As I am quite new to this tool, I do not really know how to debug this.

Is there any other possible way to do this?

• Rather than calculating the distance from the points to the lines, what if you calculated the distance from the lines to each point? Then it would be simple to select the line with the lowest value and copy its id.
– csk
Nov 28, 2017 at 20:18

A simple solution for your question is using QGIS tool Distance to Nearest Hub. However, this tool does not bring into account your particular way of calculating distance using a 90 degree angle, as you specified. It simply measures the distance from each input point to the middle of each line feature.

Here is an example of how the tool can be used in a similar context:

Input: Parameters (Hub Layer name attribute is the attribute from the line layer you wish to keep, in your case 'id'): Output (same points as input, with added attribute): As with any other processing algorithms, make sure both your layers are using the same coordinate systems (in your case you had a different CRS for each input layer). If the distance field (Hub Dist) is of importance to you too (not just which feature is closest), then make sure you are also using projected coordinate systems (not 4326).
Hope this helps!

• thanks for your suggestion! I tried it out and came to the conclusion that my problem is indeed the different CRS I use for each input layer. But the problem is: if the two input layers have the same CRS, the points are not overlaying correctly with the streets anymore! So how can I convert both input layers into one CRS so that they overlay correctly? I tried it out with both layers being in CRS EPSG:31255 and my output lines where somewhere in nowhere. I hope you understand my problem? Nov 29, 2017 at 15:09
• Follow these steps: 1) Start a new QGIS project 2) Drag your "EVIS_Stat_Sensorik..." layer first (EPSG: 31255), and make sure that after that on the bottom left of the QGIS interface it says EPSG: 31255 3) Drag your "tiger:graph" layer (it should then say OTF, too in the bottom-left corner). Then run the tool Reproject in the processing toolbox with Target CRS: 31255 4) Use this output layer as input for the algorithm you choose (Distance to nearest hub / v.distance) Nov 29, 2017 at 15:32
• it worked! Thank you so much. Now I have successfully tried the Distance to neares hub- algorithm and as you said, I get the distances to the middle of the lines. So I would like to do this with v.distance-algorithm. Maybe you can help me out with this one too? I opened an other question because I have a problem in understanding of how to use this algorithm correctly: gis.stackexchange.com/questions/263508/… Nov 29, 2017 at 16:55

Your request should be achievable with the GRASS-GIS module `v.distance`. In your screenshot I think I see two problems:

First your EVIS_Stat_Sensorik and tiger layers are in different coordinate systems. GRASS does not work like that (I don't know what manipulation QGIS does behind the scenes); first convert to a uniform coordinate system.

Second, I think you have reversed the column names for egdeid. The "Column name where value will be uploaded" should be edgeid, and "Column of nearest feature" should be id.

• thanks for your suggestion! I tried it out as well and came to the conclusion that my problem is indeed the different CRS I use for each input layer. But the problem is: if the two input layers have the same CRS, the points are not overlaying correctly with the streets anymore! So how can I convert both input layers into one CRS so that they overlay correctly? I tried it out with both layers being in CRS EPSG:31255 and my output lines where somewhere in nowhere. I hope you understand my problem? Nov 29, 2017 at 15:10

You could make use of QGIS virtual layer as described here (italial tutorial) https://pigrecoinfinito.wordpress.com/2017/04/28/qgis-minima-distanza-tra-punti-e-linee

• Hey Enrico, thank you so much for your suggestion! I have tried it out but unfortunately, I cannot get the query (which is provided in this tutorial) to work. Maybe you can help me out one more time? I have opened a new question on that topic, maybe you know what I am doing wrong: gis.stackexchange.com/questions/264335/… Dec 6, 2017 at 7:26

one solution could be to use Attribute Transfer Plugin I didn't use it yet but it looks like it could solve your problem.

For QGIS users, the NNJoin QGIS plugin is simpler to use than the GRASS v.distance function. It will produce a new point layer with the original points and attributes plus the attributes of the nearest line and the distance to it.