4

In QGIS 3.22 I have two point layers named:

  • 'source_p'
  • 'target_p'

I want to point automatically with a line from the 'source_p' layer to the two nearest points on the 'target_p' layer.

Manually, I was able to generate such code where the 'target_p' layer refers to the "parent" field in the 'source_p' layer.

make_line( $geometry , geometry( get_feature('target_p', 'fid', parent) ) ) 
1
  • Run join attributes by nearest adding the 2 closests points on your target layer to your source layer, then create the lines. Commented Aug 8, 2022 at 8:33

1 Answer 1

8

Use the following expression with the following functions:

  1. array_foreach() (to create two lines) and
  2. collect_geometries() to convert the array of lines to actual lines (geometries)
collect_geometries ( array_foreach ( overlay_nearest ( 'target_p', -- rename the layer if necessary $geometry, limit:=2 ), make_line ( $geometry, @element ) ) ) 

The expression used on the source_p layer (red points) to create lines to the two nearest points from targe_p layer (blue dots):

enter image description here

1
  • Thank you, this is working for me and exactly what I wanted Commented Aug 8, 2022 at 16:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.