# Hausdorff Distance

### Question

• SQL Server don't have Hausdorff Distance function by default, like PostGIS ?

How can I code a function which gets as input two polygons and returns a float with Hausdorff distance?

I'm using SQL Server "Denali".

Sincerely,

João Vicente

Tuesday, October 18, 2011 10:26 AM

• No, there is no inbuilt function.

The ST_Hausdorff function in PostGIS does not give a true Hausdorff distance between two geometries - it only considers the distance measured from the vertices of one of the geometries to the other (as noted at http://postgis.refractions.net/docs/ST_HausdorffDistance.html).
Therefore, you could rewrite this fairly easily in a SQLCLR procedure that examined each of the vertices of one supplied geometry (using STPointN() up to STNumPoints()) and determined the distance from that vertex to any point of the second geometry (using STDistance()). The maximum value of STDistance() returned from any vertex in one geometry to any point in the other geometry is the "discrete Hausdorff distance" equivalent to that which you'd get from PostGIS.
twitter: @alastaira blog: http://alastaira.wordpress.com/
• Marked as answer by Thursday, October 20, 2011 8:08 AM
Tuesday, October 18, 2011 11:24 AM

### All replies

• No, there is no inbuilt function.

The ST_Hausdorff function in PostGIS does not give a true Hausdorff distance between two geometries - it only considers the distance measured from the vertices of one of the geometries to the other (as noted at http://postgis.refractions.net/docs/ST_HausdorffDistance.html).
Therefore, you could rewrite this fairly easily in a SQLCLR procedure that examined each of the vertices of one supplied geometry (using STPointN() up to STNumPoints()) and determined the distance from that vertex to any point of the second geometry (using STDistance()). The maximum value of STDistance() returned from any vertex in one geometry to any point in the other geometry is the "discrete Hausdorff distance" equivalent to that which you'd get from PostGIS.
twitter: @alastaira blog: http://alastaira.wordpress.com/
• Marked as answer by Thursday, October 20, 2011 8:08 AM
Tuesday, October 18, 2011 11:24 AM
• Thanks a lot!
Tuesday, October 18, 2011 12:26 PM