开发者

SQL Server 2008 R2 Geography Distance?

I have created a table with the following columns:

Text:varchar(255)
Location:geography

They contain a few city's from The Netherlands as data (got the coordinates from google maps):

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)

I want to know the distance between all city's in the DB from Rotterdam, so I perform this query:

Select 
    Text, Location, 
    Location.STDistance(geography::Point(51.925638, 4.493408, 4326)) as Distance 
from Messages

But as a result I get a distance close to 6800000 for every city.

What could be causing this?

The only reason I can think of is that I'm using the wrong SRID, but I can't figure out which one I should use instead.

Thanks!

Edit:

Just for the heck of it I went playing with the numbers and I got some weird results:

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)

Now here's where it get interesting:

(A) - (B) = 48504 m = 48 km
(A) - (C) = 133123 m = 133 km

These开发者_如何学Python values are roughly the distances between these city's.


Try a structure like this.

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)


According this test case it seems to work just fine:

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 

So your actual query looks fine, which makes me wonder if the problem is due to incorrect data in your table. Can you confirm that the data is correctly stored in the table?

Also I recommend storing the geography value that you compare with in a separate value as in @TrickyNixons example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜