24

I have two layers, a line and a point layer.

I need to make a line layer which connects all the points to the nearest line feature. How can I do that?

Is there a plugin available for QGIS? This is a very important tool which is missing in QGIS.

ArcView has this tool: "Nearest Features"

4

5 Answers 5

27

As an alternative, you could:

  1. Use the Convert Lines to Points tool from:

    Processing Toolbox > SAGA > Shapes - Points > Convert Lines to Points

    (Add points over small distances. E.g. add a point every 1m if the overall line is 100m)

    Convert lines to points

  2. Use the Distance to nearest hub from:

    Processing Toolbox > QGIS geoalgorithms > Vector analysis tools > Distance to nearest hub

    (Set the parameters, using the output layer of the Convert Lines to Points tool as the Destination hubs layer and setting the Output shape type as Line to hub)

    Distance to nearest hub

  3. Final result should be a line layer which connects the original points layer with the original line layer (with line-converted points):

    Final result with points

    (without line-converted points):

    Final result without points

I used QGIS 2.12.3-Lyon.

1
  • There is now a tool called "Points along geometry" in the Vector geometry menu in QGIS which does the same as the SAGA tool.
    – Jens
    Jul 6, 2020 at 9:24
10

In QGIS I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer....

Let's assume we have two layers 'points' and 'river' with their corresponding attribute tables, see image below.

input

With the following query, it is possible to create new lines that will represent the connection between points to the nearest line features.

SELECT
    ST_ShortestLine(r.geometry, p.geometry),
    p.id AS pid,
    r.id AS rid,
    ROUND(ST_Length(ST_ShortestLine(r.geometry, p.geometry)), 6) AS distance
FROM
    points AS p, river AS r
WHERE
    distance < 0.5
GROUP BY
    p.id
ORDER BY
    MIN(distance)

Note:

  • WHERE distance < 0.5 specifies the longest acceptable distance between point to the line feature

The output Virtual Layer with its Attribute table will look as following

output

1
  • Note - This is a PostGIS query... Jul 16, 2021 at 13:57
4

Meanwhile (since QGIS 3.16), there is the new overlay_nearest() function you can use for that with QGIS expressions. For each point, 1) get the nearest line with overlay_nearest(), 2) get the closest point on this line to the point using closest_point().

The expression to be used with Geometry generator or Geometry by expression (see here for details) look like:

make_line (
    $geometry,
    closest_point (
        overlay_nearest ('line', $geometry)[0],
        $geometry
    )
)
  • Replace line in line 4 with the name of your line layer.

Dotted lines to connect points to nearest lines are created with the above expression:

enter image description here

2

The ClosestPoint does what you are looking for, currently limited to selected features only. You can take a look at the code and modify it for your needs

-2

If you find the point over the line, the shortest point, with the coordinate 3D (X,Y,Z) you need an algorithm to calc the position over the shortest line. You can using this tool https://github.com/rafaelduartenom/findpointonline. You need postgres to execute this tool and shapefiles.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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