【问题标题】:How to get location from latitude and longitude in T-SQL?如何从 T-SQL 中的经纬度获取位置?
【发布时间】:2021-03-01 18:54:59
【问题描述】:

我有一个 locationId 的映射表以及它们的中心纬度和中心经度值,如下所示-

| Location Id | Center_lat | Center_long |
|-------------|------------|-------------|
| 1           | 50.546     | 88.344      |
| 2           | 48.546     | 86.344      |
| 3           | 52.546     | 89.344      |

我有另一个表,我在其中为用户获取具有纬度和经度的连续位置数据,如下所示 -

+---------+------------+-------------+
| User Id | Center_lat | Center_long |
+---------+------------+-------------+
|     101 |     50.446 |      88.314 |
|     102 |     48.446 |      86.314 |
|     103 |     52.446 |      89.314 |
+---------+------------+-------------+

如果所有用户的纬度和经度值位于与位置ID对应的经纬度值的1000米范围内,我想获取所有用户的locationId。如何在 T-SQL 中完成?

决赛桌应如下所示 -

+---------+------------+-------------+------------+
| User Id | Center_lat | Center_long | LocationId |
+---------+------------+-------------+------------+
|     101 |     50.546 |      88.344 |          1 |
|     102 |     48.546 |      86.344 |          2 |
|     103 |     52.546 |      89.344 |          3 |
+---------+------------+-------------+------------+

【问题讨论】:

标签: sql sql-server tsql inner-join spatial


【解决方案1】:

您可以使用将纬度/经度对转换为geography 对象,然后使用stdistance()

select u.*, l.location_id
from users u
inner join locations l
    on geography::point(u.center_lat, u.center_long, 4326).stdistance(geography::point(l.center_lat, l.center_long, 4326)) < 1000

请注意,将此信息存储为 points 会更有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多