I am trying to find a way to measure minimum distance between two polygon layers in QGIS.

I have one polygon layer (orange) which represents hunting areas, and one polygon layer (green) which represent wind-turbine areas.

For each orange polygon, I want to get the minimum distance to ALL green polygons within 10,000m, also coupled with identify of the green polygons. The distance should preferably be from the edges of each polygon layer. But centroid distances might be valuable as well. The result should therefor be new columns in the orange attribute table containing green-id and distance.

polygon-layer (orange) represents hunting areas, and polygon layer (green) represent wind-turbine areas.1

Unfortunately it doesn't seem to work. I can preview the result in the field calculator, but when i press 'OK', the column values are still NULL. Same thing for both the distances and the IDs.

Previewed values seems ok.

3 Answers 3


On the orange polygon, use this expression to get an array of the $id of the green polygons within 10000 m distance:

overlay_nearest ('green',$id, max_distance:=10000,limit:=-1)
  • Replace green with the name of your green layer
  • Convert the array to a text string enclosing the expression in an array_to_string() function
  • Replace $id with any fieldname of the green layer to return these values

And with the following expression, you get the length of the shortest connection between each orange polygon to all green polygons within a radius of 10000 m:

array_foreach (
    overlay_nearest ('green',$geometry, max_distance:=10000,limit:=-1),
    length(shortest_line(@element, $geometry))

The tool join attributes by nearest does what you're looking for. To a features on a layer it joins attributes of objects on a second layer and adds the distance and directions between the objects. You may specify how many joins you want to create, as well as how large the join distance should be.

Set your hunting areas layer as first input, the wind farms as the second. You may define, which attributes you want to be joined, e.g. an ID or name of the windfarms should suffice. The max number of joined neighbours could be something around 20, due to the size and distribution of windfarms. Join distance is 10000 m.

  • first use join by location

  • second use field calculator with this to know each distance

    length(make_line ( make_point ( "feature_x" , "feature_y" ) ,
                       make_point ( "nearest_x" , "nearest_y" ) ))

enter image description here

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.