5

My goal is to create a line layer that is derived from the distance between a point layer and a line layer. An example could be using a line layer representing a road and a point layer representing a home or telephone pole.

I would like to create a vector line that extends from the road line layer to the point layer. Is there a Plugin for this?

1
  • A good question, but I was wondering what have you tried so far? What are your ideas? What kind of similar topics/threads/articles have you considered?
    – Taras
    Commented Mar 17, 2021 at 19:11

2 Answers 2

6

You can also use QGIS expressions to crease such lines. There are two ways to do that:

  1. Use geometry generator for visualization purpose, without creating a new layer. For that, add a new symbol layer in the style panel and set it to Geometry Generator / Linestring (see screenshot below).
  2. Create actual geometries on a new layer: Menu Processing / Toolbolx / Geometry by expression.

In both cases, use the expression on the points layer, use the same expression (on line 6, replace 'line' with the name of your line-layer). Be aware that the overlay_nearest function is available since QGIS 3.16:

make_line (
    $geometry, 
    closest_point (
        array_first (
            overlay_nearest (
                'line' , 
                $geometry
            )
        ), 
    $geometry
    )
)

The screenshot illustrates the first version of using the expression: enter image description here

4

You can achieve this using a virtual layer

go to the menu Layer > Add Layer > Add/Edit Virtual Layer... and enter the query

select p.id, l.id, 
       min(st_distance(p.geometry, l.geometry)) dist, 
       ST_ShortestLine(p.geometry, l.geometry) geometry
from myPointLayer p, myLineLayer l
group by p.id
order by st_distance(p.geometry, l.geometry) asc

The group by will ensure we get a single row for each row in layer1. Because we order by distance and keep the min, the group by will retain the 1st entry, i.e. the nearest one.

enter image description here

8
  • thanks for chiming in JGH, this looks simpler than writing a python script. What were the names on your two layers in you example?
    – sudofizz
    Commented Mar 17, 2021 at 18:13
  • @sudofizz In the text code in the answer (not the picture), replace myPointLayer and myLineLayer with your names. (in the picture-code, the layers were named p and l so no need for an alias as in the text-code)
    – JGH
    Commented Mar 17, 2021 at 18:42
  • Appreciate the help with this as I am learning a lot right now! Have you ever experienced the generated lines extending out of the map area?
    – sudofizz
    Commented Mar 17, 2021 at 18:53
  • Make sure all 3 layers have the same CRS
    – JGH
    Commented Mar 17, 2021 at 18:58
  • 1
    don't set but rather reproject one of the layer CRS (see this post)
    – JGH
    Commented Mar 17, 2021 at 19: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.