LoginSignup
4
1

More than 1 year has passed since last update.

Elixir 挑戦記 - 地理空間データを扱う

Last updated at Posted at 2023-03-06

Elixir 挑戦記

  • 統合的アプローチによる学習
  • とりあえず手を動かしてみるシリーズ
  • 調べたり取り組んだりした内容をひたすら書いていく
  • あとで整理してまとめたい

地理空間データについて

こちらの記事が分かりやすかったです

Elixirで扱いやすいファイルフォーマット

ラスタ形式 → GeoTiff

  • Evisionで読込、表示 Evision.imread/1
  • 衛星データはこれが多い

ベクタ形式 → GeoJson

  • Jason+Geoで読み込み File.read!/1 |> Jason.decode!/1 |> Geo.JSON.decode!/1
  • MapLibreで表示 MapLibre.add_geo_source/3
  • gdalwarpも対応 (COG取得用にSystem.cmd/3から使いたい)
  • geojson.io で簡単に作成できる (他形式からの変換も可)
  • 行政区域データ筆ポリゴン でサポートされている

Livebookで地理空間データを読み込んでみる

Setup

Mix.install(
  [
    {:evision, "~> 0.1"},
    {:geo, "~> 3.4"},
    {:jason, "~> 1.4"},
    {:kino, "~>0.8"},
    {:kino_maplibre, "~> 0.1.3"}
  ]
)

必要なパッケージをインストール

GeoTiff

LandBrowserからGeoTiffを取得1

tif_path = "./sample.tif"

Evision.imread(tif_path)

Mar-02-2023 17-08-01.gif

非常にシンプルで良き

画像、データ構造のどちらも確認できるのは便利

GeoJson

行政区域データ(大分県令和4年度)を利用2
参考:行政区域コード

geojson_path = "./N03-22_44_220101.geojson" # 大分県令和4年度の行政区域データ

geojson_data =
  File.read!(geojson_path)
  |> Jason.decode!()
  |> Geo.JSON.decode!()

city_code = "44201" # 大分市の行政区域コード

city_geojson = %Geo.GeometryCollection{
  geometries: Enum.filter(geojson_data.geometries, &(&1.properties["N03_007"] == city_code))
}

coordinates_list = 
  city_geojson.geometries
  |> Enum.map(& &1.coordinates)
  |> List.flatten()

longitudes = 
  coordinates_list
  |> Enum.map(& elem(&1, 0))

latitudes =
  coordinates_list
  |> Enum.map(& elem(&1, 1))

center = {
  (Enum.min(longitudes) + Enum.max(longitudes)) / 2,
  (Enum.min(latitudes) + Enum.max(latitudes)) / 2
}

city_map =
  MapLibre.new(center: center, zoom: 8, style: :terrain)
  |> MapLibre.add_geo_source("city_geojson", city_geojson)
  |> MapLibre.add_layer(
    id: "city_geojson",
    source: "city_geojson",
    type: :fill,
    paint: [fill_color: "#00ff00", fill_opacity: 0.5]
  )

Screenshot 2023-03-02 20.32.32.jpg

中心座標の計算に少し手間がかかるが、綺麗に表示できた
(SmartCellを使って手動設定も可能、とりあえず確認したいだけならそれで十分そう)

参考

  1. “ The source data were downloaded from AIST’s LandBrowser,
    (https://landbrowser.airc.aist.go.jp/landbrowser/)produced from ESA remote sensing data”)

  2. 出典:国土交通省国土数値情報ダウンロードサイト

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1