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 )
)
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 )
)