如何使用BigQuery查找相邻的邮政编码

问题描述 投票:0回答:1

我有一个邮政编码 - 让我们说“94304”。我怎样才能找到贴近它的邮政编码?

google-bigquery gis
1个回答
2
投票

更新:现在使用官方公共数据集:

SELECT *
FROM (
  SELECT b.zip_code neighbor, b.city, b.county, ST_DISTANCE(a.zcta_geom, b.zcta_geom) distance
  FROM `bigquery-public-data.geo_us_boundaries.us_zip_codes` a,
       `bigquery-public-data.geo_us_boundaries.us_zip_codes` b
  WHERE a.zip_code != b.zip_code
  AND a.zip_code = '94304'
)
WHERE distance < 2000 # 2 KM
ORDER BY distance

-- 5.4s elapsed, 130 MB processed

enter image description here

使用BigQuery GIS和可用的公共数据,这非常简单快捷:

SELECT *
FROM (
  SELECT b.zipcode neighbor, b.city, b.county, ST_DISTANCE(a.geog, b.geog) distance
  FROM `fh-bigquery.gis.zipcodes` a,
       `fh-bigquery.gis.zipcodes` b
  WHERE a.zipcode != b.zipcode
  AND a.zipcode = '94304'
)
WHERE distance < 2000 # 2 KM
ORDER BY distance

--  Query complete (4.2s elapsed, 130 MB processed)

enter image description here

为了获得这种快速性能,首先我转换了可用的公共数据,以便将文本中的几何图形解析为GIS几何图形:

CREATE TABLE `gis.zipcodes`
AS
SELECT *, ST_GEOGFROMTEXT(zipcode_geom) AS geog
FROM `bigquery-public-data.utility_us.zipcode_area`

基于:

© www.soinside.com 2019 - 2024. All rights reserved.