共用方式為


H3 快速入門 (Databricks SQL)

此頁面上的 H3 地理空間函數快速入門說明下列各項:

  • 如何將地理位置數據集載入 Unity 目錄。
  • 如何將緯度和經度數據行轉換成 H3 單元格數據行。
  • 如何將郵遞區編碼多邊形或多多邊形 WKT 資料行轉換成 H3 單元格數據行。
  • 如何查詢從拉瓜迪亞機場到曼哈頓金融區的上車和下車分析。
  • 如何在地圖上轉譯 H3 匯總計數。

筆記本和查詢的範例

準備 Unity Catalog 資料

在此筆記本中,我們會:

  • 從 Databricks Filesystem 設定公用計程車數據集。
  • 設定 NYC 郵遞區編碼 數據集。

準備 Unity Catalog 資料

取得筆記本

Databricks SQL 查詢與 Databricks Runtime 11.3 LTS 和更新版本

查詢 1: 確認已設定基礎資料。 請參閱筆記本

use catalog geospatial_docs;
use database nyc_taxi;
show tables;
-- Verify initial data is setup (see instructions in setup notebook)
-- select format_number(count(*),0) as count from yellow_trip;
-- select * from nyc_zipcode;

查詢 2:H3 紐約市郵遞區號 - 在解析 時套用 12

use catalog geospatial_docs;
use database nyc_taxi;
-- drop table if exists nyc_zipcode_h3_12;
create table if not exists nyc_zipcode_h3_12 as (
  select
    explode(h3_polyfillash3(geom_wkt, 12)) as cell,
    zipcode,
    po_name,
    county
  from
    nyc_zipcode
);
-- optional: zorder by `cell`
optimize nyc_zipcode_h3_12 zorder by (cell);
select
  *
from
  nyc_zipcode_h3_12;

查詢 3:H3 計程車車程 - 在解析 時套用 12

use catalog geospatial_docs;
use database nyc_taxi;
-- drop table if exists yellow_trip_h3_12;
create table if not exists yellow_trip_h3_12 as (
  select
    h3_longlatash3(pickup_longitude, pickup_latitude, 12) as pickup_cell,
    h3_longlatash3(dropoff_longitude, dropoff_latitude, 12) as dropoff_cell,
    *
  except
    (
      rate_code_id,
      store_and_fwd_flag
    )
  from
    yellow_trip
);
-- optional: zorder by `pickup_cell`
-- optimize yellow_trip_h3_12 zorder by (pickup_cell);
select
  *
from
  yellow_trip_h3_12
 where pickup_cell is not null;

查詢 4: H3 LGA 上車 - 2500 萬人從拉瓜迪亞上車 (LGA)

use catalog geospatial_docs;
use database nyc_taxi;
create
or replace view lga_pickup_h3_12 as (
  select
    t.*
  except(cell),
    s.*
  from
    yellow_trip_h3_12 as s
    inner join nyc_zipcode_h3_12 as t on s.pickup_cell = t.cell
  where
    t.zipcode = '11371'
);
select
  format_number(count(*), 0) as count
from
  lga_pickup_h3_12;
-- select
  --   *
  -- from
  --   lga_pickup_h3_12;

查詢 5: H3 金融區下車 - 金融區總下車人數為 3400 萬

use catalog geospatial_docs;
use database nyc_taxi;
create
or replace view fd_dropoff_h3_12 as (
  select
    t.*
  except(cell),
    s.*
  from
    yellow_trip_h3_12 as s
    inner join nyc_zipcode_h3_12 as t on s.dropoff_cell = t.cell
  where
    t.zipcode in ('10004', '10005', '10006', '10007', '10038')
);
select
  format_number(count(*), 0) as count
from
  fd_dropoff_h3_12;
-- select * from fd_dropoff_h3_12;

查詢 6: H3 LGA-FD - 82 萬 7 千人從 LGA 上車,在 FD 下車

use catalog geospatial_docs;
use database nyc_taxi;
create
or replace view lga_fd_dropoff_h3_12 as (
  select
    *
  from
    fd_dropoff_h3_12
  where
    pickup_cell in (
      select
        distinct pickup_cell
      from
        lga_pickup_h3_12
    )
);
select
  format_number(count(*), 0) as count
from
  lga_fd_dropoff_h3_12;
-- select * from lga_fd_dropoff_h3_12;

查詢 7: 依郵遞區號的 LGA-FD - 依郵遞區號 + 橫條圖計算 FD 下車人次

use catalog geospatial_docs;
use database nyc_taxi;
select
  zipcode,
  count(*) as count
from
  lga_fd_dropoff_h3_12
group by
  zipcode
order by
  zipcode;

查詢 8: H3 的 LGA-FD - 使用 H3 資料格計算 FD 下車人次 + 地圖標記視覺效果

use catalog geospatial_docs;
use database nyc_taxi;
select
  zipcode,
  dropoff_cell,
  h3_centerasgeojson(dropoff_cell) :coordinates [0] as dropoff_centroid_x,
  h3_centerasgeojson(dropoff_cell) :coordinates [1] as dropoff_centroid_y,
  format_number(count(*), 0) as count_disp,
  count(*) as `count`
from
  lga_fd_dropoff_h3_12
group by
  zipcode,
  dropoff_cell
order by
  zipcode,
  `count` DESC;

LGA-FD H3 計數 1

LGA-FD H3 計數 2

Databricks Runtime 11.3 LTS 和更新版本筆記本

快速入門-Python:H3 紐約市計程車從拉瓜迪亞至曼哈頓

取得筆記本

在筆記本 + kepler.gl 中使用 Spark Python 繫結,與 Databricks SQL 中的快速入門結構相同。

快速入門-Scala:H3 紐約市計程車從拉瓜迪亞至曼哈頓

取得筆記本

在 筆記本 + kepler.gl 中透過 Python 資料格使用 Spark Scala 繫結,與 Databricks SQL 中的快速入門結構相同。

快速入門-SQL:H3 紐約市計程車從拉瓜迪亞至曼哈頓

取得筆記本

在 筆記本 + kepler.gl 中透過 Python 資料格使用 Spark SQL 繫結,與 Databricks SQL 中的快速入門結構相同。