Shwe
Shwe

Reputation: 469

SQL server GEOGRAPHY STDistance function is returning big difference than other test results

I am calculating distance in meters between two lat long values - 40.1844441 -77.2252771 and
40.319166 -76.7880552

I tested the distance between these two points in below two web sites - http://www.nhc.noaa.gov/gccalc.shtml and http://www.movable-type.co.uk/scripts/latlong.html
Both of these web sites return approximately 40014 meters.

So, I uses SQL server's GEOGRAPHY data type to calculate the distance between these two points again and it returns 48927 meters. It is a huge difference to me.
Below is my code and any suggestion is appreciated.

declare @latlong1 GEOGRAPHY
declare @latlong2 GEOGRAPHY
DECLARE @distance float

set @latlong1 = GEOGRAPHY::STGeomFromText('POINT(40.1844441 -77.2252771)', 4326)
set @latlong2 = GEOGRAPHY::STGeomFromText('POINT(40.319166 -76.7880552)', 4326)

SET @distance = @latlong1.STDistance(@latlong2)
SELECT @distance -- this returns 48927 meters

Upvotes: 4

Views: 2135

Answers (2)

John Machin
John Machin

Reputation: 83032

Try reversing the lat and lon in the "VeryOdd" case i.e. use POINT(-77.2252771 40.1844441) and POINT(-76.7880552 40.319166).

My offline "spherical law of cosines" result is 48733 metres which is only about 0.4% difference from the original VeryOdd 48927 metres.

I suggest trying a few more tests with different lats and lons, plus checking the docs for the relevant functions/methods.

Late breaking news: STGeomFromText('POINT(longitude latitude)' etc -- compared with the more frequent protocol: latitude first then longitude

See the selected answer to this question: Create Geometry/Geography Field from Latitude & Longitude fields (SQL Server)

To clear up the remaining differences, somebody needs to search for what radius/radii is/are invoked by the 4326 magic ...

Upvotes: 0

John Cappelletti
John Cappelletti

Reputation: 82010

I too have noticed some odd results, so I tend to use geography::Point() which seems to generate cleaner results. Even then, option 2 is 80 meters off from the UDF which seems to be spot-on.

Example

declare @latlong1 GEOGRAPHY
declare @latlong2 GEOGRAPHY

Set @latlong1 = GEOGRAPHY::STGeomFromText('POINT(40.1844441 -77.2252771)',4326)
Set @latlong2 = GEOGRAPHY::STGeomFromText('POINT(40.319166  -76.7880552)',4326)

Select VeryOdd = @latlong1.STDistance(@latlong2)
      ,SQLGeo  = geography::Point(40.1844441, -77.2252771, 4326).STDistance(geography::Point(40.319166,-76.7880552, 4326)) 
      ,UDFGeo  = [dbo].[udf-Geo-Meters](40.1844441,-77.2252771,40.319166,-76.7880552)

Returns

VeryOdd             SQLGeo              UDFGeo
48927.1485292471    40093.8055001913    40014.8833526855

The UDF if Interested

CREATE Function [dbo].[udf-Geo-Meters](@Lat1 FLOAT, @Lng1 FLOAT, @Lat2 FLOAT, @Lng2 FLOAT)
Returns Float as
Begin
    Return ACOS(SIN(PI()*@Lat1/180.0)*SIN(PI()*@Lat2/180.0)+COS(PI()*@Lat1/180.0)*COS(PI()*@Lat2/180.0)*COS(PI()*@Lng2/180.0-PI()*@Lng1/180.0)) * 6371008.8
    -- 6.371 mean radius of earth in meters
End

Upvotes: 2

Related Questions