Wrong distance calculation with MongoDB

北慕城南 提交于 2019-11-28 04:50:38

问题


I am executing the following raw query with MongoDB:

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

The position in the database is set to [52.473266, 13.45494].

I get a result once I set the distance to 7.3 or higher, so it seems the two locations must at least be 7.3 kilometer away from each other.

When I calculate the distance of those two geo locations with Google Maps (for example going by car) it's telling me it's only 5.2 kilometer away from each other.

I tested it with loads of different locations and there is always a big difference in the distance calculation of google and Mongodb

Am i missing anything or can somebody explain please where this difference is coming from?

I already checked this answer but it's not working for me...


回答1:


MongoDB assumes that coordinates are in (long, lat) format. If you compute distances by hand using Great-circle distance you'll see what is going on:

> 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

Google takes coordinates in (lat, long) format so if you provide the same input Google interpretation will be like below:

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


来源:https://stackoverflow.com/questions/19496883/wrong-distance-calculation-with-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!