1

I am trying to connect points in a vector layer. Each point should be connected to its nearest neighbour that is not inside the same polygon. I am using the ProcessX "nearest point to path" function, but I didn't find a proper expression. The points have an attribute with the name of the Polygon, which I wanted to use for that.

Looking at the figure: The whole idea is to separate the yellow areas (roads) at each crossing in case someone has a better idea.

Polygon Layer and vertices to connect

1
  • 1
    Select all the points in the polygon, invert the selection and use that as the input to the nearest neighbour process
    – Ian Turton
    Commented May 22 at 19:32

1 Answer 1

1

You can create a virtual layer that will join the two layers, create the line and apply the desired filtering.

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

SELECT pt1.id, pt2.id,
       ROUND(MIN(ST_Distance(pt1.geometry, pt2.geometry)),2) AS distance,
       MAKELINE(pt1.geometry, pt2.geometry) AS geometry
FROM point_layer AS pt1, point_layer AS pt2
WHERE pt1.polygon_name <> pt2.polygon_name
GROUP BY pt1.id
ORDER BY distance DESC

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

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.