Starting from a layer with point geometry, my goal is to do two calculations in one Geometry Generator expression.
First, for each point, I would like to calculate the centroid for the 5 points nearest to it.
Next, for each calculated centroid I would like to draw the 10 nearest lines connecting to it.
To calculate the centroid I am working with the following expression:
centroid(collect_geometries (
array_foreach (
overlay_nearest (
'TEST',
$geometry,
limit:=5
),
make_line (
$geometry,
@element
)
)
))
To draw the line I used this expression:
collect_geometries (
array_foreach (
overlay_nearest (
'CENTRO',
$geometry,
limit:=10
),
make_line (
$geometry,
@element
)
)
)
Result:
My goal is to unify the two expressions in a single expression for the Geometry Generator that works with the original layer.
Best Answer
There are several things regarding your expression:
overlay_nearest()
function. It will also consider the same feature as a closest taken from the same layer. See this thread for more details: QGIS expression: when using overlay_nearest sometimes a feature considers itself its nearest neighbour. It also refers to a QGIS discussion on the GitHubLet's assume there is a point layer called 'points_in_polygon' (N=10), see the image below:
After using this expression:
The above expression is sure can be optimized with the
with_variable()
to avoid double writings.Afterwards, apply the "Multipart to singleparts" geoalgorithm.
And get the output like this (N=120):
Usage of the "Delete duplicate geometries" can also be feasible.