Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Tellus から台風情報を取得する

livebooks/tellus/typhoon.livemd

Tellus から台風情報を取得する

Mix.install([
  {:req, "~> 0.5"},
  {:kino, "~> 0.14"},
  {:kino_maplibre, "~> 0.1"}
])

情報の設定

# Tellus のトークンを入力する
token_input = Kino.Input.password("Token")
# 台風情報の商品ID
typhoon_product_id = "c34f78fc-3b20-4a0a-a692-c402a0d8cdbe"

このノートブックではゲヒルン株式会社の台風情報APIを使用しています

©Gehirn Inc.

Tellus の認証

auth_url = "https://www.tellusxdp.com/api/manager/v2/auth/token/"
json_header = {"Content-Type", "application/json"}

get_product_info = fn product_id ->
  request_body = %{product_id: product_id}
  auth_header = {"Authorization", "Bearer " <> Kino.Input.read(token_input)}

  auth_url
  |> Req.post!(json: request_body, headers: [auth_header, json_header])
  |> Map.get(:body)
end
typhoon_product_info = get_product_info.(typhoon_product_id)
# 認証ヘッダーの設定
auth_header = {"Authorization", "Bearer " <> typhoon_product_info["token"]}

台風一覧の取得

typhoon_events_url = "#{typhoon_product_info["base_url"]}/archive/typhoon/events.json"

typhoon_events =
  typhoon_events_url
  |> Req.get!(headers: [auth_header])
  |> Map.get(:body)

台風情報の取得

get_typhoon_points = fn event_id ->
  url = "#{typhoon_product_info["base_url"]}/archive/typhoon/#{event_id}_point.json"

  url
  |> Req.get!(headers: [auth_header])
  |> Map.get(:body)
end
typhoon_points = get_typhoon_points.("TC2111")

台風情報の地図へのプロット

marker_list =
  typhoon_points["features"]
  |> Enum.map(fn feature ->
    coordinates = feature["geometry"]["coordinates"]

    %{
      "lat" => Enum.at(coordinates, 1),
      "lng" => Enum.at(coordinates, 0)
    }
  end)
MapLibre.new(center: {140.0, 35.0}, zoom: 3)
|> MapLibre.add_table_source("marker_list", marker_list, {:lng_lat, ["lng", "lat"]})
|> MapLibre.add_layer(
  id: "marker_list_circle_1",
  source: "marker_list",
  type: :circle,
  paint: [circle_color: "#18862e", circle_radius: 8, circle_opacity: 0.5]
)