In the expression builder (QGIS 3.38) attached to the "Default value" setting of the Attribute Form, I'd like to assign (i.e. fill/update) an attribute of the entities of a "feature" layer the name of the nearest feature in a 2 km radius of either the closest airport or the closest harbor:
To this end, I'm trying to use the overlay_nearest
function, something like this, just to grab the idea:
array_first(
array_sort(
array_cat(
overlay_nearest(
layer:='airports',
expression:="name",
limit:=1,
max_distance:=5000
),
overlay_nearest(
layer:='harbors',
expression:="name",
limit:=1,
max_distance:=5000
)
)
)
)
But this code sorts the results alphabetically by their names, not by their corresponding distances to the layer's current feature.
I guess I have to aggregate in a structure like a nested array, the names with their corresponding distances to the feature of the third layer but then how to sort those based on the distance and assign the corresponding name to the feature?
I wish the overlay_nearest
would have a way to expose the computed distance (because at some point, it has to be computed in the background) but that's not the case.
Does anybody have an efficient and easy-to-maintain solution to achieve that?