计算纬度/经度点之间的短距离

发布于 2024-12-22 15:19:09 字数 146 浏览 4 评论 0原文

我有一个带有空间点的 MySQL 表,需要计算距离。我找到了很多关于使用半正弦公式执行此操作的材料,但是所有这些都假设点之间的距离很大。就我而言,我只关心短距离(< 1 英里),因此我不需要校正地球曲率。我的直觉是,在如此小的距离下,使用半正矢公式会不准确。有什么建议吗?

I have a MySQL table with Spatial Points, and need to calculate distances. I found lots of material on doing this using the Haversine formula, however all of these assume a large distance between points. In my case, I only care about short distances (< 1 mile) so I don't need to correct for the earth's curvature. My intuition is using the Haversine formula will be inaccurate at such small distances. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-12-29 15:19:09

你的直觉是不正确的。根据维基百科,考虑半正矢公式和半正矢的定义(φ 是纬度,ψ 是经度):

haversin(d/r) = hasrsin(φ_2 - φ_1) + cos(φ_1) cos(φ_2) hasrsin(ψ_2) - ψ_1)

haveversin(θ) = sin(θ/2)^2

还有一个相关事实:对于较小的 θ 值,sin θ 约等于 θ< /em>;更相关的是,它在θ中近似线性。因此,haversin θ 大约为 (θ/2)²。随着 θ 接近零,这种近似值变得更好

如果纬度和经度接近,则 φ2 - φ₁ 和 ψ2 - ψ₁,此处应用半正弦函数的值将接近于零,这意味着该公式大约为

(d/2r)2 = ((φ2 - φ₁) / 2)2 + cos(φ₁) cos (φ2) ((ψ2 - ψ₁) / 2)²

现在请注意,该公式与二维欧氏距离具有相同的形式一些任意的缩放因子(记住(kx)² = k² x² 因此我们可以将常数移入和移出方块):

kd² = k2 Δφ² + k ₃ Δψ²

最后,我在没有证据的情况下断言,这些任意的缩放因子与将纬度/经度的变化转换为线性距离的因子相同。

因此,半正矢公式对于小距离不会变得不准确;在小距离的限制下,它与普通的欧几里德距离计算完全相同。

Your intuition is incorrect. Consider the haversine formula, and the definition of haversine, according to Wikipedia (φ is latitude and ψ is longitude):

haversin(d/r) = haversin(φ_2 - φ_1) + cos(φ_1) cos(φ_2) haversin(ψ_2 - ψ_1)

haversin(θ) = sin(θ/2)^2

There is a further fact that is relevant: for small values of θ, sin θ is approximately equal to θ; more relevantly, it is approximately linear in θ. Therefore, haversin θ will be approximately (θ/2)². This approximation gets better as θ approaches zero.

If the latitude and longitude are close together, then φ₂ - φ₁ and ψ₂ - ψ₁, which are what the haversine function is applied to here, will be close to zero, meaning that the formula is approximately

(d/2r)² = ((φ₂ - φ₁) / 2)² + cos(φ₁) cos(φ₂) ((ψ₂ - ψ₁) / 2)²

Now note that this formula has the same form as Euclidean distance in two dimensions with some arbitrary scaling factors (remembering that (kx)² = k² x² so we can move constants in and out of the squares):

kd² = k₂ ∆φ² + k₃ ∆ψ²

Lastly, I assert without proof that those arbitrary scaling factors turn out to be the same ones which convert changes in latitude/longitude to linear distance.

Therefore, the haversine formula does not become inaccurate for small distances; it is precisely the same as an ordinary Euclidean distance calculation, in the limit of small distances.

御守 2024-12-29 15:19:09
  1. 使用几何数据类型的点值创建点
    MyISAM 表

  2. 在这些点上创建 SPATIAL 索引

使用 MBRContains() 查找值:

SELECT  *
FROM    table
WHERE   MBRContains(LineFromText(CONCAT(
        '('
        , @lon + 10 / ( 111.1 / cos(RADIANS(@lon)))
        , ' '
        , @lat + 10 / 111.1
        , ','
        , @lon - 10 / ( 111.1 / cos(RADIANS(@lat)))
        , ' '
        , @lat - 10 / 111.1 
        , ')' )
        ,mypoint)

、 或,在 MySQL 5.1 及更高版本中:

SELECT  *
FROM    table
WHERE   MBRContains
                (
                LineString
                        (
                        Point
                                (
                                @lon + 10 / ( 111.1 / COS(RADIANS(@lat))),
                                @lat + 10 / 111.1
                                ) 
                        Point
                                (
                                @lon - 10 / ( 111.1 / COS(RADIANS(@lat))),
                                @lat - 10 / 111.1
                                ) 
                        ),
                mypoint
                )

这将选择大约在框中的所有点(@lat +/- 10 公里,@lon +/- 10 公里)。

这实际上不是一个盒子,而是一个球形矩形:球体的纬度和经度边界部分。这可能与弗朗茨约瑟夫地的普通矩形有所不同,但在大多数有人居住的地方与它非常接近。

应用额外的过滤来选择圆内的所有内容(不是正方形)

可能应用额外的精细过滤来考虑大圆距离(对于大距离)

点击以下解决方案

  1. Create your points using Point values of Geometry datatypes in
    MyISAM table

  2. Create a SPATIAL index on these points

Use MBRContains() to find the values:

SELECT  *
FROM    table
WHERE   MBRContains(LineFromText(CONCAT(
        '('
        , @lon + 10 / ( 111.1 / cos(RADIANS(@lon)))
        , ' '
        , @lat + 10 / 111.1
        , ','
        , @lon - 10 / ( 111.1 / cos(RADIANS(@lat)))
        , ' '
        , @lat - 10 / 111.1 
        , ')' )
        ,mypoint)

, or, in MySQL 5.1 and above:

SELECT  *
FROM    table
WHERE   MBRContains
                (
                LineString
                        (
                        Point
                                (
                                @lon + 10 / ( 111.1 / COS(RADIANS(@lat))),
                                @lat + 10 / 111.1
                                ) 
                        Point
                                (
                                @lon - 10 / ( 111.1 / COS(RADIANS(@lat))),
                                @lat - 10 / 111.1
                                ) 
                        ),
                mypoint
                )

This will select all points approximately within the box (@lat +/- 10 km, @lon +/- 10km).

This actually is not a box, but a spherical rectangle: latitude and longitude bound segment of the sphere. This may differ from a plain rectangle on the Franz Joseph Land, but quite close to it on most inhabited places.

Apply additional filtering to select everything inside the circle (not the square)

Possibly apply additional fine filtering to account for the big circle distance (for large distances)

here following solution to click

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文