查找与其他用户的距离时出现问题 Sql Server 2005 - Haversine

发布于 2024-11-15 07:35:10 字数 895 浏览 4 评论 0原文

我有一张包含用户的表,我需要根据经纬度找到最近的用户。我也需要距离。我两天来一直在研究半正矢公式和替代方案,但我遇到了一些错误。

举例来说,我使用马来西亚吉隆坡的经纬度(大约 3.2, 102)。

使用毕达哥拉斯:

SELECT top 10 *, 
    SQRT(SQUARE(ABS(latitude - 3.2)) + SQUARE(ABS(longitude - 102))) AS distance 
FROM lfuser 
ORDER BY distance

它给了我“正确”的结果(接近该点的用户)。但我无法从中得到距离(以公里为单位),而且我知道这并不完全正确。所以我尝试使用半正矢:

SELECT TOP 10 *, 
    ROUND(2 * ASIN(SQRT(POWER(SIN(((3.2 / 180) * PI() 
        - (latitude / 180) * PI()) / 2) ,2) + COS((3.2 / 180) * PI()) 
        * COS((latitude / 180) * PI()) * POWER(SIN(((102 / 180) * PI() 
        - (longitude / 180) * PI()) / 2), 2))) * 6367, 4) AS distance 
FROM lfuser 
ORDER BY distance

奇怪的是,这给了我其他地方的用户。实际上,前 1 个结果是加纳的用户,其 lat=5.55 和 long=-0.20。

这让我发疯......我当然可以通过将毕达哥拉斯结果乘以 110 公里来计算近似距离,但我想要更正确的结果。

希望有人能指出哪里不对。

请不要发布半正矢公式等不同实现的链接。我已经研究它们两天了。

I have a table with users and I need to find the nearest users based on lat, long. I also need the distance. I have been struggling with the haversine formula and alternatives for two days, but I am facing some errors.

Let's say as an example that I use lat, long for Kuala Lumpur, Malaysia (roughly 3.2, 102).

Using pythagoras:

SELECT top 10 *, 
    SQRT(SQUARE(ABS(latitude - 3.2)) + SQUARE(ABS(longitude - 102))) AS distance 
FROM lfuser 
ORDER BY distance

It gives me "correct" results (users near that point). But I can't get the distance (in KM) from that and I know it is not entirely correct. So I have tried to use haversine:

SELECT TOP 10 *, 
    ROUND(2 * ASIN(SQRT(POWER(SIN(((3.2 / 180) * PI() 
        - (latitude / 180) * PI()) / 2) ,2) + COS((3.2 / 180) * PI()) 
        * COS((latitude / 180) * PI()) * POWER(SIN(((102 / 180) * PI() 
        - (longitude / 180) * PI()) / 2), 2))) * 6367, 4) AS distance 
FROM lfuser 
ORDER BY distance

The strange thing is that this gives me users elsewhere. Actually the top 1 result is a user in Ghana with lat=5.55 and long=-0.20.

This is driving me crazy... I could of course calculate an approximate distance by multiplying the pythagoras result with 110 kms but I would like the more correct result.

I hope someone can point out what is wrong.

Please don't post links to different implementations of haversine formula etc. I have been looking at them for two days now.

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

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

发布评论

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

评论(2

眼眸印温柔 2024-11-22 07:35:10

过去我在 SQL 2005 中使用过Haversine,所以我尝试转换我的过程以适应您的情况。如果工作正常,请告诉我

我认为正确的吉隆坡经度和纬度是 3.139003, 101.686855

declare @myLatitude decimal(12,6), @myLongitude decimal(12,6), @EarthRadius decimal(12,6)
select @myLatitude = 3.139003, @myLongitude = 101.686855, @EarthRadius = 6378.137

SELECT top 10 *, 
    (@EarthRadius * ACOS((SIN(PI() *  @myLatitude /180) 
     * SIN(PI() * Latitude/180))
     + (COS(PI() *  @myLatitude /180) 
     * COS(PI() * Latitude/180) 
     * COS(PI() * Longitude/180 - PI() * @myLongitude /180))))
    as distance
FROM lfuser 
ORDER BY distance

希望它有帮助

In the past i'd used Haversine in SQL 2005 so i tried to convert my procedure to adapt for your case. Let me know if work fine

I supposed also the correct Kuala Lumpur Longitude and Latitude are 3.139003, 101.686855

declare @myLatitude decimal(12,6), @myLongitude decimal(12,6), @EarthRadius decimal(12,6)
select @myLatitude = 3.139003, @myLongitude = 101.686855, @EarthRadius = 6378.137

SELECT top 10 *, 
    (@EarthRadius * ACOS((SIN(PI() *  @myLatitude /180) 
     * SIN(PI() * Latitude/180))
     + (COS(PI() *  @myLatitude /180) 
     * COS(PI() * Latitude/180) 
     * COS(PI() * Longitude/180 - PI() * @myLongitude /180))))
    as distance
FROM lfuser 
ORDER BY distance

Hope it helps

看透却不说透 2024-11-22 07:35:10

现在只是一个快速回答。如果您使用的是 SQL Server 2008,您应该使用地理类型(在您的情况下为点)来存储位置。有一些内置方法可以处理点之间的距离。从此处此处

您可能想在专门从事该领域的 gis.stackexchange.com 上提问。

Just a quick answer for now. If you are using SQL server 2008 you should use the geo types (point in your case) to store the location. There are built-in methods for handling distance between points. Start here and here

You might want to ask this question on: gis.stackexchange.com which specialises in this area.

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