MongoDB的距离计算错误 [英] Wrong distance calculation with MongoDB
问题描述
我正在使用MongoDB执行以下原始查询:
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)
数据库中的位置设置为[52.473266, 13.45494]
.
The position in the database is set to [52.473266, 13.45494]
.
将距离设置为7.3或更高时,我得到一个结果,因此看来两个位置之间必须至少相距7.3公里.
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.
当我使用 Google地图计算这两个地理位置的距离时(例如开车)告诉我彼此相距仅5.2公里.
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.
我用不同位置的负载进行了测试,并且google和Mongodb的距离计算始终存在很大差异
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...
推荐答案
MongoDB假定坐标为(long, lat)
格式.如果您使用大圆距离来手动计算距离,您将会看到发生了什么:
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以(lat, long)
格式获取坐标,因此,如果您提供相同的输入,则Google解释将如下所示:
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
这篇关于MongoDB的距离计算错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!