2013-10-21 68 views
4

我執行以下原始查詢與MongoDB的:錯誤距離計算用的MongoDB

qry = {"position" : SON([("$near", [52.497309,13.39385]), ("$maxDistance", distance/111.12)])} 
locations = Locations.objects(__raw__=qry) 

在數據庫中的位置被設置爲[52.473266, 13.45494]

當我將距離設置爲7.3或更高時,我得到一個結果,因此看起來兩個位置至少距離彼此7.3公里。

當我計算出這兩個地理位置的距離爲Google Maps(例如駕車時),它告訴我它距離彼此只有5.2公里。

我有不同的位置,負載測試,並始終存在於谷歌和MongoDB

我失去任何東西,或有人可以解釋請該差分是來自距離計算大的差別?

我已經檢查this answer,但它不是爲我工作...

+0

嗨,我從django/tastypie執行此調用,所以SON正在爲我工​​作,我不能使用.getIndexes()。與傳統座標對的maxDistance使用弧度。 ....「如果您查詢傳統座標對,請以弧度指定$ maxDistance。」因此,要計算以km爲單位的距離,我需要將其轉換爲111.12的弧度 – Jonas

回答

5

MongoDB的假定座標在(long, lat)格式。如果您在使用Great-circle distance手工計算距離,你就會明白是怎麼回事:

> from math import acos, sin, cos, radians 
> 
> long_x, lat_x = [radians(y) for y in [52.473266, 13.45494]] 
> long_y, lat_y = [radians(y) for y in [52.497309, 13.39385]] 
> 
> acos(sin(lat_x) * sin(lat_y) + cos(lat_x) * cos(lat_y) * cos(long_x - long_y)) * 6371.0 
7.27362435031 

谷歌需要座標(lat, long)格式,所以如果你提供相同的輸入谷歌的解釋會象下面這樣:

> acos(sin(long_x) * sin(long_y) + cos(long_x) * cos(long_y) * cos(lat_x - lat_y)) * 6371.0 
4.92535867182