How to label a point in QGIS based on a vector layer below it
How to label a point in QGIS based on a vector layer below it
For example, you might have a point layer and want to label what town it is in. You might have the Census Tiger County Subdivision layer named ‘tl_2020_36_cousub20’, with the field ‘NAMELSAD20’ that you want to return from your point or polygon layer.
attribute(array_first(overlay_nearest(‘tl_2020_36_cousub20′,$currentfeature)),’NAMELSAD20’)
This will give you the nearest feature.
You need to wrap it in array_first as shown above, as overlay_nearest returns an array with one element. Then attribute just returns the attribute based on the object provided.
Alternatives include overlay_contains, overlay_crosses, overlay_intersects, overlay_touches, overlay_within which will return what you want. Try searching the QGIS Help in the QGIS Expressions builder.
You can also use this style of expression to create a spatial join or nearest neighbor join, inside the field calculator.