Dot-Net
SQL Server 2008 R2 地理距離?
我創建了一個包含以下列的表:
Text:varchar(255) Location:geography它們包含來自荷蘭的一些城市作為數據(從Google地圖獲取座標):
Rotterdam - POINT (51.925637 4.493408 4326) Utrecht - POINT (52.055868 5.103149 4326) Nijmegen - POINT (51.801822 5.828247 4326) Breda - POINT (51.542919 4.77356 4326)我想知道數據庫中所有城市與鹿特丹之間的距離,所以我執行了這個查詢:
Select Text, Location, Location.STDistance(geography::Point(51.925638, 4.493408, 4326)) as Distance from Messages但結果我得到每個城市的距離接近 6800000。
這可能是什麼原因造成的?
我能想到的唯一原因是我使用了錯誤的 SRID,但我不知道應該改用哪個。
謝謝!
編輯:
只是為了它,我去玩數字,我得到了一些奇怪的結果:
Distance from Rotterdam to Rotterdam: 6828459.57 (A) (weird but true) Distance from Rotterdam to Breda: 6779956.10 (B) Distance from Rotterdam to Nijmegen: 6695336.38 (C)現在這裡是有趣的地方:
(A) - (B) = 48504 m = 48 km (A) - (C) = 133123 m = 133 km這些值大致是這些城市之間的距離。
根據這個測試案例,它似乎工作得很好:
DECLARE @rotterdam geography = geography::Point(51.925637, 4.493408,4326); with tmp(txt, geo) as ( select 'Rotterdam',geography::Point(51.925637, 4.493408,4326) UNION ALL select 'Utrecht',geography::Point(52.055868, 5.103149,4326) UNION ALL select 'Nijmegen',geography::Point(51.801822, 5.828247,4326) UNION ALL select 'Breda',geography::Point(51.542919, 4.77356,4326) ) SELECT t.txt, t.geo.STDistance(geography::Point(51.925637, 4.493408,4326)) from tmp t所以你的實際查詢看起來很好,這讓我想知道問題是否是由於表中的數據不正確造成的。您能否確認數據已正確儲存在表中?
我還建議將您比較的地理值儲存在一個單獨的值中,如@TrickyNixons 範例中所示。
試試這樣的結構。
DECLARE @a geography, @b geography SET @a = geography::Point(51.925637, 4.493408,4326) SET @b= geography::Point(51.542919, 4.77356,4326) SELECT @a.STDistance(@b)