gpt4 book ai didi

sql - 使用 BigQuery SQL 进行反向地理编码 : How to determine the city closest to a (lat, lon)?

转载 作者:行者123 更新时间:2023-12-02 03:19:13 24 4
gpt4 key购买 nike

我有大量的点 - 我想确定距离每个点最近的城市。如何使用 BigQuery 执行此操作?

最佳答案

这是迄今为止我们研究出的性能最佳的查询:

WITH a AS (
# a table with points around the world
SELECT * FROM UNNEST([ST_GEOGPOINT(-70, -33), ST_GEOGPOINT(-122,37), ST_GEOGPOINT(151,-33)]) my_point
), b AS (
# any table with cities world locations
SELECT *, ST_GEOGPOINT(lon,lat) latlon_geo
FROM `fh-bigquery.geocode.201806_geolite2_latlon_redux`
)

SELECT my_point, city_name, subdivision_1_name, country_name, continent_name
FROM (
SELECT loc.*, my_point
FROM (
SELECT ST_ASTEXT(my_point) my_point, ANY_VALUE(my_point) geop
, ARRAY_AGG( # get the closest city
STRUCT(city_name, subdivision_1_name, country_name, continent_name)
ORDER BY ST_DISTANCE(my_point, b.latlon_geo) LIMIT 1
)[SAFE_OFFSET(0)] loc
FROM a, b
WHERE ST_DWITHIN(my_point, b.latlon_geo, 100000) # filter to only close cities
GROUP BY my_point
)
)
GROUP BY 1,2,3,4,5

enter image description here

关于sql - 使用 BigQuery SQL 进行反向地理编码 : How to determine the city closest to a (lat, lon)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com