【问题标题】:Wrong distance calculation with MongoDB使用 MongoDB 计算错误的距离
【发布时间】:2015-02-21 18:09:19
【问题描述】:

我正在使用 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 公里的距离。

我用不同的位置负载测试它,google和mongodb的距离计算总是有很大的差异

我是否遗漏了什么,或者有人可以解释一下这种差异的来源吗?

我已经检查了this answer,但它对我不起作用...

【问题讨论】:

  • 嗨,我正在 django/tastypie 中执行这个调用,所以 SON 为我工作,我不能使用 .getIndexes()。带有旧坐标对的 maxDistance 使用弧度。 ....“如果您查询旧坐标对,请以弧度指定 $maxDistance。”因此,要计算以公里为单位的距离,我需要将其转换为除以 111.12 的弧度

标签: mongodb distance geospatial


【解决方案1】:

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

Google 以(lat, long) 格式获取坐标,因此如果您提供相同的输入,Google 的解释将如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多