1
/
5

Qiita記事Part.47-Overture MapsをSnowflakeで使ってみた(Snowflake Marketplace)

こんにちは、ナイトレイインターン生の保科です。
Wantedlyをご覧の方に、ナイトレイのエンジニアがどのようなことをしているか知っていただきたく、Qiitaに公開している記事をストーリーに載せています。

今回はGISエンジニアの徳竹さんの記事です。
少しでも私たちに興味を持ってくれた方は下に表示される募集記事もご覧ください↓↓


Amazon, Meta, Microsoft、tomtom, esriなどが協力し整備しているオープンな地図データである「Overture Maps」が2024年7月24日にGA版としてリリースされました!

Home - Overture Maps Foundation
Overture is powering current and next-generation map products by creating reliable, easy-to-use, and interoperable open map data. Our data supports developers who build map services or use geospatial data.
https://overturemaps.org/


昨年のβ版データ公開時点ではダウンロードするにはS3経由・Azure経由でダウンロードする必要がありましたが、今はPythonのコマンドラインツールが公開されており、サクッと利用しやすくなっています。

GitHub - OvertureMaps/overturemaps-py: overture-py
overture-py. Contribute to OvertureMaps/overturemaps-py development by creating an account on GitHub.
https://github.com/OvertureMaps/overturemaps-py


Pythonのコマンドラインツールは使いやすいのですが、元のデータセットが巨大なため、もっと利用しやすい方法は無いかなと思っていたところ、SnowflakeのマーケットプレイスにCARTOがデータセット一式を公開しているとの記事を見つけました!BigQueryでも利用できるみたいです。
(2024年5月2日から公開されているみたいです)

Overture Maps data now on the cloud: how to use it with CARTO
Announcing Overture Maps data in the cloud! Seamlessly access, analyze, and visualize data on global buildings, POIs, boundaries & more with CARTO!
https://carto.com/blog/overture-maps-data-now-on-the-cloud-use-it-with-carto


マーケットプレイスで公開されているということは、利用登録してあとはクエリするだけでOKということです!便利!

Snowsightにログインして、データ製品 → Marketplace → overtureで検索
すれば6つのデータセットがヒットします。
今回はPlacesBuildingsを使ってみます。

PlacesはPOIのデータセットです。
施設や場所の名前、カテゴリー、住所、ジオメトリ(ポイント)などが格納されています。

Buildingsは建物ポリゴンのデータセットです。
建物名称や階数などが付与されているフィーチャーもあるようです。

それぞれのデータの詳細説明はこちらです。

Data Guides | Overture Maps Documentation
https://docs.overturemaps.org/guides/


クエリしてQGISで表示してみた

Placesデータセット

addressesカラムとgeometryカラムのそれぞれを利用して、日本国内の場所情報を取得してみましょう。

データ量は以下のとおりとなっていました。

select count(id)
from overture_maps__places.carto.place
;
/*
COUNT(ID)
52849527
*
select count(id)
from overture_maps__places.carto.place
;
/*
COUNT(ID)
52849527
*

addressesカラムでクエリする

まずはaddressesカラムを利用してみます。
以下のようにクエリして、場所名、カテゴリ、住所関連情報、wktを出力しました。
カラムによってはJSON形式になっているので展開が必要です。

addressescountryキーに国情報があるので、それを利用しました。

select
    id,
    parse_json(names):primary as name,
    parse_json(categories):main as main_category,
    parse_json(addresses):list[0].element.country as country,
    parse_json(addresses):list[0].element.locality as locality,
    parse_json(addresses):list[0].element.postcode as postcode,
    parse_json(addresses):list[0].element.freeform as freeform,
    st_aswkt(geometry) as wkt
from
    OVERTURE_MAPS__PLACES.CARTO.PLACE
where
    parse_json(addresses):list[0].element.country = 'JP'
limit 1000
;
select
    id,
    parse_json(names):primary as name,
    parse_json(categories):main as main_category,
    parse_json(addresses):list[0].element.country as country,
    parse_json(addresses):list[0].element.locality as locality,
    parse_json(addresses):list[0].element.postcode as postcode,
    parse_json(addresses):list[0].element.freeform as freeform,
    st_aswkt(geometry) as wkt
from
    OVERTURE_MAPS__PLACES.CARTO.PLACE
where
    parse_json(addresses):list[0].element.country = 'JP'
limit 1000
;

上記を可視化するとこんな分布になっています!
何故か国外のポイントになっている施設が多数存在します😅


※背景地図:国土地理院 地理院地図(以降、同様)

国外にプロットされたデータについて詳しくチェックすると、geometry以外の情報は合っていそうなものが多数でした。データセット作成時の不具合?

(※マーケットプレイス上のデータだけの現象なのか、もとのデータも同様なのかをOverture Maps Explorerで簡易的に確認しましたが、同様の結果でした。)

geometryでクエリする

次に、addressesの情報ではなく、geometryをもとに抽出してみました。
東京都内のデータを抽出したいので、こちらもSnowflake Marketplaceに公開されているtruestar様のJAPANESE PREFECTURE DATAを利用しました。

-- 東京都離島を除くポリゴンを利用
select
    a.id,
    parse_json(names):primary as name,
    parse_json(categories):main as main_category,
    parse_json(addresses):list[0].element.country as country,
    parse_json(addresses):list[0].element.locality as locality,
    parse_json(addresses):list[0].element.postcode as postcode,
    parse_json(addresses):list[0].element.freeform as freeform,
    st_aswkt(geometry) as wkt
from
    OVERTURE_MAPS__PLACES.CARTO.PLACE a
    join PREPPER_OPEN_DATA_BANK__JAPANESE_PREFECTURE_DATA.E_PODB.E_PR_FD20 b
    on st_within(a.geometry, b.polygon_jp_pref_3_light) -- 東京都ポリゴン内のポイントだけを抽出
where
    b.pref_name = '東京都'
limit 1000
;
-- 東京都離島を除くポリゴンを利用
select
    a.id,
    parse_json(names):primary as name,
    parse_json(categories):main as main_category,
    parse_json(addresses):list[0].element.country as country,
    parse_json(addresses):list[0].element.locality as locality,
    parse_json(addresses):list[0].element.postcode as postcode,
    parse_json(addresses):list[0].element.freeform as freeform,
    st_aswkt(geometry) as wkt
from
    OVERTURE_MAPS__PLACES.CARTO.PLACE a
    join PREPPER_OPEN_DATA_BANK__JAPANESE_PREFECTURE_DATA.E_PODB.E_PR_FD20 b
    on st_within(a.geometry, b.polygon_jp_pref_3_light) -- 東京都ポリゴン内のポイントだけを抽出
where
    b.pref_name = '東京都'
limit 1000
;

結果はこんな感じです!
お店から観光スポット、史跡など、幅広くカバーされています。

Buildingsデータセット

次に、buildingsで試してみましょう!
件数はこんな感じです。(Placesと二桁違う)

select count(id)
from overture_maps__buildings.carto.building
;
/*
COUNT(ID)
2354376929
 */
select count(id)
from overture_maps__buildings.carto.building
;
/*
COUNT(ID)
2354376929
 */

Placesのときと同様に東京都内のデータを抽出してみましょう!
と思い下記のクエリを実行したのですが、全然完了しません...。

select
    a.id,
    st_aswkt(geometry) as wkt
from
    overture_maps__buildings.carto.building a
    join PREPPER_OPEN_DATA_BANK__JAPANESE_PREFECTURE_DATA.E_PODB.E_PR_FD20 b
    on st_within(a.geometry, b.polygon_jp_pref_3_light)
    and b.pref_name = '東京都'
limit 10
;
select
    a.id,
    st_aswkt(geometry) as wkt
from
    overture_maps__buildings.carto.building a
    join PREPPER_OPEN_DATA_BANK__JAPANESE_PREFECTURE_DATA.E_PODB.E_PR_FD20 b
    on st_within(a.geometry, b.polygon_jp_pref_3_light)
    and b.pref_name = '東京都'
limit 10
;


ポリゴン同士の計算は重いのかもしれないので、bboxカラムの数字を利用して抽出を試してみましょう!
ということで下記のクエリを試しましたが、こちらでも全然クエリが完了せず...。
bboxカラムの値をパースしてから評価しているので、むしろ重い処理になってそうですね。

with target_pref as (
    select
        st_xmax(polygon_jp_pref_3_light) as xmax,
        st_xmin(polygon_jp_pref_3_light) as xmin,
        st_ymax(polygon_jp_pref_3_light) as ymax,
        st_ymin(polygon_jp_pref_3_light) as ymin
    from
        PREPPER_OPEN_DATA_BANK__JAPANESE_PREFECTURE_DATA.E_PODB.E_PR_FD20
    where
        pref_name = '東京都'
)
select
    id,
    st_aswkt(geometry) as wkt
from
    overture_maps__buildings.carto.building
where
    parse_json(bbox):xmax <= (select xmax from target_pref)
    and parse_json(bbox):xmin >= (select xmin from target_pref)
    and parse_json(bbox):ymax <= (select ymax from target_pref)
    and parse_json(bbox):ymin >=  (select ymin from target_pref)
limit 10
;
with target_pref as (
    select
        st_xmax(polygon_jp_pref_3_light) as xmax,
        st_xmin(polygon_jp_pref_3_light) as xmin,
        st_ymax(polygon_jp_pref_3_light) as ymax,
        st_ymin(polygon_jp_pref_3_light) as ymin
    from
        PREPPER_OPEN_DATA_BANK__JAPANESE_PREFECTURE_DATA.E_PODB.E_PR_FD20
    where
        pref_name = '東京都'
)
select
    id,
    st_aswkt(geometry) as wkt
from
    overture_maps__buildings.carto.building
where
    parse_json(bbox):xmax <= (select xmax from target_pref)
    and parse_json(bbox):xmin >= (select xmin from target_pref)
    and parse_json(bbox):ymax <= (select ymax from target_pref)
    and parse_json(bbox):ymin >=  (select ymin from target_pref)
limit 10
;

悔しいのでマーケットプレイスからのクエリは諦めて、Pythonのコマンドラインツールを利用して、渋谷区内の建物ポリゴンをダウンロードしてみます。
※ローカルにダウンロードするので、データサイズを気にして対象範囲を渋谷区に狭めています。

以下のクエリで渋谷区のbboxの値を取得して、コマンドラインツール用のコマンドを生成します。

select
    concat(
        'overturemaps download --bbox=',
        st_xmin(polygon_jp_city_light),
        ',',
        st_ymin(polygon_jp_city_light),
        ',',
        st_xmax(polygon_jp_city_light),
        ',',
        st_ymax(polygon_jp_city_light),
        ' -f geojson --type=building -o shibuya_building.geojson'
    ) as command
from
    PREPPER_OPEN_DATA_BANK__JAPANESE_CITY_DATA.E_PODB.E_CI_FD20
where
    pref_name = '東京都'
    and city_name = '渋谷区'
;
select
    concat(
        'overturemaps download --bbox=',
        st_xmin(polygon_jp_city_light),
        ',',
        st_ymin(polygon_jp_city_light),
        ',',
        st_xmax(polygon_jp_city_light),
        ',',
        st_ymax(polygon_jp_city_light),
        ' -f geojson --type=building -o shibuya_building.geojson'
    ) as command
from
    PREPPER_OPEN_DATA_BANK__JAPANESE_CITY_DATA.E_PODB.E_CI_FD20
where
    pref_name = '東京都'
    and city_name = '渋谷区'
;

実行します。

overturemaps download --bbox=139.661756,35.641658,139.72356,35.691991 -f geojson --type=building -o shibuya_building.geojson
overturemaps download --bbox=139.661756,35.641658,139.72356,35.691991 -f geojson --type=building -o shibuya_building.geojson

取得できました!(約8万件)
そこまで時間かからずにダウンロード完了しました。

おわりに

Placesのデータセットからは施設や場所の情報が簡単に取得できるので活用の機会が多そうですね!
Buildingsは必要なエリアに絞った形での取得はクエリに時間がかかりすぎて取得できずでした…。(Pythonのコマンドラインツールは早かった。)
Snowflake上での利用頻度が高いのであれば、bboxカラムをパースしたマテリアライズド・ビューを作っておくとかでしょうか?要検討したいです!

Invitation from 株式会社ナイトレイ
If this story triggered your interest, have a chat with the team?
株式会社ナイトレイ's job postings

Weekly ranking

Show other rankings
Like 保科 汐里's Story
Let 保科 汐里's company know you're interested in their content