4

I have two point layers, let's say one is 'endpoints' and another layer is 'nodes'.

The "endpoints" layer attribute table has a field called "nearest_node", I want to populate it with names/IDs of the nearest "node".

I have tried using the field calculator to build a proper query, however, I found no way to cycle through all "node" objects on a different layer to find the nearest point.

Is there some easy way to do it within QGIS?

1
  • 1
    Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Commented Feb 15, 2023 at 11:56

2 Answers 2

5

An alternative way to do it would be to use the overlay_nearest function on a new field ("nearest_node") in your 'endpoints' layer.

overlay_nearest(
    layer:='nodes',    -- the name of your nodes layer
    expression:=$id,   -- e.g. the attribute you wish to get from the nearest feature
    limit:=1           -- limit the number of returned features to 1 (the nearest)
)[0]                   -- the [0] is to get the first (and in this case only - due to the limit) element of the resulting array

enter image description here

3

Basically, I use the "Distance to nearest hub (line to hub)" algorithm, then use the created temporary layer to use in an attribute calculation with the following query:

attribute(get_feature('NewLayer', 'matching_field', "matching_field"), 'HubName')

This does exactly what I want it to do, the above example uses different field names, so use what layers and field names you've got.

Afterwards, I can discard the layer created by the processing algorithm.

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.