sql-server – SQL server GEOGRAPHY STDistance函数返回的差异大于其他测试结果

我计算两个lat长值之间的距离 –

40.1844441 -77.2252771和

40.319166 -76.7880552

我测试了以下两个网站中这两点之间的距离 –
http://www.nhc.noaa.gov/gccalc.shtml
http://www.movable-type.co.uk/scripts/latlong.html

这两个网站都返回大约40014米.

因此,我使用SQL服务器的GEOGRAPHY数据类型再次计算这两个点之间的距离,它返回48927米.这对我来说是一个巨大的差异.

以下是我的代码,任何建议表示赞赏.

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

最佳答案 我也注意到一些奇怪的结果,所以我倾向于使用geography :: Point(),这似乎可以产生更清晰的结果.即便如此,选项2距离UDF 80米,这似乎是现货.

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)

返回

VeryOdd             SQLGeo              UDFGeo
48927.1485292471    40093.8055001913    40014.8833526855

UDF如果有兴趣

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
点赞