Skip to content

ST_HausdorffDistance

Returns the Hausdorff distance between two geometries.

Usage

double ST_HausdorffDistance(geomA: geometry, geomB: geometry)
double ST_HausdorffDistance(geomA: geometry, geomB: geometry, densifyFrac: double)

Arguments

  • geomA (geometry): Input geometry
  • geomB (geometry): Input geometry
  • densifyFrac (double): A fraction by which to densify each segment of the geometries before computing the distance. A value of 0.25 will densify the segment by adding vertices every ¼ of the segment length. Densification can give a more accurate result for geometries with long segments or complex shapes.

Description

The Hausdorff distance is a measure of the similarity between two geometric shapes. This implementation returns the greatest of all distances from a point in one geometry to the closest point in the other geometry.

Returns NULL if either geometry is NULL. Returns 0 if either geometry is empty.

Examples

Basic Hausdorff distance between two points:

SELECT ST_HausdorffDistance(ST_Point(0, 0), ST_Point(3, 4));
┌──────────────────────────────────────────────────────────────────────────────┐
│ st_hausdorffdistance(st_point(Int64(0),Int64(0)),st_point(Int64(3),Int64(4)) │
│                                      )…                                      │
╞══════════════════════════════════════════════════════════════════════════════╡
│                                                                          5.0 │
└──────────────────────────────────────────────────────────────────────────────┘

Hausdorff distance between two linestrings:

SELECT ST_HausdorffDistance(
    ST_GeomFromText('LINESTRING (0 0, 2 0)'),
    ST_GeomFromText('LINESTRING (0 1, 2 1)')
);
┌──────────────────────────────────────────────────────────────────────────────┐
│ st_hausdorffdistance(st_geomfromtext(Utf8("LINESTRING (0 0, 2 0)")),st_geomf │
│                   romtext(Utf8("LINESTRING (0 1, 2 1)")))…                   │
╞══════════════════════════════════════════════════════════════════════════════╡
│                                                                          1.0 │
└──────────────────────────────────────────────────────────────────────────────┘

Using densification for more accurate results as needed:

SELECT ST_HausdorffDistance(
    ST_GeomFromText('LINESTRING (130 0, 0 0, 0 150)'),
    ST_GeomFromText('LINESTRING (10 10, 10 150, 130 10)'),
    0.5
);
┌──────────────────────────────────────────────────────────────────────────────┐
│ st_hausdorffdistance(st_geomfromtext(Utf8("LINESTRING (130 0, 0 0, 0 150)")) │
│  ,st_geomfromtext(Utf8("LINESTRING (10 10, 10 150, 130 10)")),Float64(0.5))… │
╞══════════════════════════════════════════════════════════════════════════════╡
│                                                                         70.0 │
└──────────────────────────────────────────────────────────────────────────────┘