2

I have been using PostGIS 3.0. The ST_HausdorffDistance() function calculates a symmetric distance between two polygons. However, Hausdorff distance is not a metric since it is the maximum distance between the closest pairs of vertices. Therefore, it depends on the number of vertices each polygon possess. I could not understand how PostGIS made it symmetric.

In addition, when I investigated a toy dataset, where we have two polygon layers (A and B), and their corresponding vertices generated by the Coord. AttribuTable plugin of QGIS (A_vertices, B_vertices), I calculated the Hausdorff distance from A to B as 16.41 and 18.13 for polygons with IDs 111 and 222 respectively. These distances are 31.19 and 25.70 in PostGIS.

select ST_HausdorffDistance(a.geom, b.geom)
from a, b 
where a.id=222 and b.id=222
0

2 Answers 2

3

PostGIS implements the Hausdorff metric (as stated in the doc), which is symmetric. It is defined as the maximum of the two Oriented Hausdorff distances between the two geometries.

It's hard to say why you might be seeing different results. If you can provide a pure SQL example that would help to see what's going on.

1
  • I have written a code that calculates Hausdorff distance. I could not spot the bug. The code and data are here: github.com/banbar/stackoverflow There is no further SQL that I have posted previously.
    – banbar
    Commented Oct 15, 2020 at 13:26
1

The following SQL code calculates the Hausdorff distance between two polygons as 10 metres.

select ST_HausdorffDistance(ST_Polygon('LINESTRING(0 0, 20 0, 20 20, 0 20, 0 0)', 3857),
                            ST_Polygon('LINESTRING(0 0, 20 0, 20 20, 15 20, 15 30, 5 30, 5 20, 0 20, 0 0)', 3857))

The illustration of the polygons is: enter image description here

If the coordinates of the red point is adjusted to (15,50), then the new Hausdorff distance becomes 30. So, the calculated distance is from one polygon to the others' closest point and not to the nearest vertex.

I think there is a bug in PostGIS as the Hausdorff distance should be calculated as the distance between the red point to the nearest vertex, which is (20,20).

I am using PostGIS 3.0.

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.