Skip to content

ST_FrechetDistance returns 0.0 instead of null for empty geometries #2720

@jiayuasu

Description

@jiayuasu

Description

ST_FrechetDistance returns 0.0 when either input geometry is empty. The correct behavior (matching PostGIS and shapely/geopandas) is to return null/NaN, since the distance between empty geometries is undefined.

Root cause

In GeomUtils.java line 546:

public static double getFrechetDistance(Geometry g1, Geometry g2) {
    if (g1.isEmpty() || g2.isEmpty()) return 0.0;  // should return null/NaN
    return DiscreteFrechetDistance.distance(g1, g2);
}

Steps to reproduce

SELECT ST_FrechetDistance(ST_GeomFromText('POINT EMPTY'), ST_GeomFromText('POINT EMPTY'))
-- Sedona returns: 0.0
-- Expected (PostGIS / shapely): null / NaN
import geopandas as gpd
from shapely.geometry import Point

gpd.GeoSeries([Point()]).frechet_distance(gpd.GeoSeries([Point()]))
# 0    NaN

Suggested fix

Change the return type to Double (boxed) and return null for empty geometries, or return Double.NaN:

public static Double getFrechetDistance(Geometry g1, Geometry g2) {
    if (g1.isEmpty() || g2.isEmpty()) return Double.NaN;
    return DiscreteFrechetDistance.distance(g1, g2);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions