Find nearest line feature from point in QGIS

In the Processing Toolbox use the Vector general > Join attributes by nearest tool with the following settings:

  • Input layer = your point layer
  • Input layer 2 = your line layer
  • Layer 2 fields to copy: Any you need, only choose the line layer ID if you want to keep the number of attributes down

Other settings can be left as default.

The result will be a duplicate of your point layer but it will now have extra attributes including "nearest_x" and "nearest_y" which we will use the generate the lines.

If you only want to display the lines as symbology then in the point layer symbology set the Symbol layer type to Geometry Generator, Geometry type to LineString / MultiLineString, and use the following expression: MAKE_LINE($geometry, MAKE_POINT("nearest_x", "nearest_y"))

If you want to actually create a data layer with the lines then use the same expression in the Vector geometry > Geometry by expression tool in the Processing toolbox


You can try a Virtual Table of the Data Source Manager like this:

select 
   p.myid, l.myid,
   min(st_distance(p.geometry, l.geometry)) dist
from 
   mypoints p, mylines l
group by 
   p.myid;

Where mypoint and mylines are replaced by your table names and myid by your id columns.

This code will return, for every point, the line id and distance of the nearest line to that point.


You can achieve this also without creating a new layer, using exclusivele QGIS expressions. For visualization purpose, you can create a line from each point of the points layer to the nearest point on the lines layer.

Add a symbol layer to the points layer, set it to Geometry generator / Geometry type: Linestring and paste the following expression (you must have a unique field named id in your lines layer that you can reference to - otherwise change id in the expression to the respective field name). Attention: overlay_nearest is available since QGIS 3.16!

make_line ( 
    $geometry,
    closest_point (
        geometry ( 
            get_feature_by_id (
                'line', 
                array_to_string ( 
                    overlay_nearest ( 
                        'line',  "id" )
                    )
            )
        ) ,
    $geometry )
)

qgis expression editor: make_line overlay_nearest to get the nearest point on line

If you want to get the coordinates of the nearest point on the lines layer as attributes in your points layer, just use this expression in the field calculator to get the x-coordinate (replace x by y to get y-coordinate). Use a virtual field if you want the coordinates to dynamically adapt to any changes you make to the points or the lines layer:

x (
    closest_point (
        geometry ( 
            get_feature_by_id (
                'line', 
                array_to_string ( 
                    overlay_nearest ( 
                        'line',  "id" )
                    )
            )
        ) ,
    $geometry )
)

QGIS expression editor: get coordinates of nearest point on geometry of other layer