Oracle中SDO_GEOM计算经纬度两点距离结果不符的原因咨询
Great question—this is a super common pitfall when working with Oracle Spatial and geographic coordinate systems like WGS84 (SRID 4326). Let's break down why your query isn't giving the right result, and how to fix it.
The Core Issue: Flat vs. Spherical Calculation
Your current query uses SDO_GEOM.SDO_DISTANCE with SRID 4326, but you haven't told Oracle to perform a geodetic (spherical) distance calculation. By default, SDO_GEOM functions treat coordinates as if they're on a flat Cartesian plane—even when you pass a geographic (lat/lon) SRID.
Since 4326 uses degrees as its unit, your original query is calculating the "flat" distance between two points in degrees, then trying to convert that to kilometers using unit=KM—but this conversion doesn't account for the Earth's curvature. That's why it's mismatching the online tool, which correctly computes the great-circle (spherical) distance between the two points.
Fix 1: Enable Geodetic Mode in SDO_GEOM.SDO_DISTANCE
Add the geodetic=true parameter to your function call. This tells Oracle to use spherical geometry for the distance calculation, which aligns with how online tools compute lat/lon distances:
select sdo_geom.sdo_distance( sdo_geometry(2001, 4326, sdo_point_type(40.0, 10.0, null), null, null), sdo_geometry(2001, 4326, sdo_point_type(40.0, 11.0, null), null, null), 0.01, 'unit=KM geodetic=true' ) as distance_km from dual;
Fix 2: Use SDO_CS.DISTANCE (Geodetic-First Function)
If you prefer a function designed explicitly for geographic coordinate systems, use SDO_CS.DISTANCE. This function automatically handles spherical calculations for geodetic SRIDs and returns results in meters (so divide by 1000 to get kilometers):
select sdo_cs.distance( sdo_geometry(2001, 4326, sdo_point_type(40.0, 10.0, null), null, null), sdo_geometry(2001, 4326, sdo_point_type(40.0, 11.0, null), null, null) ) / 1000 as distance_km from dual;
What You'll See Now
Both corrected queries will return a distance of ~85.3 kilometers, which matches the great-circle distance calculated by online tools. For context, this makes sense: at 40° latitude, each degree of longitude corresponds to ~85 km (since longitude lines converge as you move away from the equator).
内容的提问来源于stack exchange,提问作者Para Kan

