2

I would like to calculate the closest distance between polygons in the same layer. The "distance matrix" tool only accept point layers.

Is there a (easy) way to calculate the distance between edges and edges ?

I work with qgis 3.2

1

If Virtual Layer is acceptable, here is an example:

SELECT     a.id AS Id_1, 
           b.id AS Id_2, 
           round(st_distance(a.geometry, b.geometry), 3) AS Distance
FROM       your_polygons_layer AS a
CROSS JOIN your_polygons_layer AS b
ON         a.id <> b.id
  • your_polygons_layer: layer name as imported
  • id: any unique id field in your_polygons_layer

[EDIT] To delete duplicates:

SELECT     a.id AS Id_1, 
                b.id AS Id_2, 
                round(st_distance(a.geometry, b.geometry), 3) AS Distance
FROM       your_polygons_layer AS a
CROSS JOIN your_polygons_layer AS b
ON         a.id <> b.id
GROUP BY   concat(min(a.id, b.id), ' ', max(a.id, b.id)) 
  • Thanks a lot Kazuhito! I have tested and it works, but the distance between two polygons is writen two times (i.e. Id_1 with Id_2 AND Id_2 with Id_1). A possibility to delete duplicates pairs ? I expected a matrix with n*(n-1)/2 lines. – Christophe Jul 23 '18 at 7:56
  • Thanks @Volta and sorry about that. Will you test the latter syntax? (I have not yet had time to test it with data). – Kazuhito Jul 23 '18 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.