3

I have in QGIS ver 3.22 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 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.
    – Erik
    Commented Aug 8, 2022 at 8:33

1 Answer 1

6

Use the following expression with:

  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
    – Rii Pii
    Commented Aug 8, 2022 at 16:31

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.