QGIS – Drawing Fixed-Length Line from Points Layer Towards Nearest Points on Line in QGIS

linepoints-to-linepolyline-creationqgis

Basically, I've used a buffer to find all airbases within 100nmi of a coastline. Now what I'd like to do for each of these points is draw a line a) to the nearest point on the coastline, and b) extending for another 100nmi out to sea. Is there a straightforward way to do this?

EDIT: Babel has provided a really good solution and now I'm realizing there's a second issue here – I want to avoid any two bases connecting to a single point. Is there a way to take the results here but distribute them along that coastline? For instance, in the picture here, I've got three bases connecting to two points, but I'd like three relatively evenly spaced ones. Thoughts?
map with lines and points)

Best Answer

You can do that directly on the point layer of the airbases, using QGIS expressions with "Geometry generator" or "Geometry by expression" (see here for the differences).

Use this expression:

extend (
    make_line (
        $geometry, 
        closest_point (
            overlay_nearest (
                'coastline', 
                $geometry,
                max_distance:=100
            )[0],
            $geometry
        )
    ),
    0,
    100
)
  • Replace coastline on line 6 with the name of your line layer representing the coastline.
  • Replace 100 in line 8 to change the maximum distance the points (airbases) can be away from the coastline and on the second last line for another value for the distance the line extends to the sea.

Only points within the defined distance from the coastline (light blue buffer, for visualization purpose only) are connected to the coastline and extended to the sea:

enter image description here