10

I have a line feature set and point feature set. I want to get the nearest line feature from every point. For this, the line features are converted to point using "Extract Nodes" tool. Then I use the "Distance to Nearest Hub (line to hub)" tool.

Here, many points got the nearest line but for some lines that don't have many nodes are not recognized by the nearest point. Here I marked one such case.

How to solve this?

icon

Red circle marked showing the point which falsely recognizes the nearest line due to less node of nearest line.

3 Answers 3

14

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

0
8

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.

2
  • For every point - you get one and only one - line (the nearest) why to you want to order that?
    – Jakob
    Commented Dec 7, 2020 at 8:20
  • I was referring to this documented behavior, but you are right, it should be ok since there is a single min() in use
    – JGH
    Commented Dec 7, 2020 at 13:03
7

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

2
  • 1
    A very interesting other approach, geometry generators are a real cool thing ...
    – Jakob
    Commented Dec 7, 2020 at 13:40
  • 1
    Indeed, geometry generators are a very powerful tool - with the new overlay expressions in QGIS 3.16 even more: qgis.org/de/site/forusers/visualchangelog316/…
    – Babel
    Commented Dec 7, 2020 at 15:13

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.