iPhoneのショートカットから電測用マップを更新

処理

ショートカット

現在地から

  • 都道府県名
  • 市区町村名
  • 住所
  • 緯度
  • 経度

を取得してwebhookでスプレッドシートにデータを送信

スプレッドシート

webhookから送られてきたデータを最終行に追加

Githubにプログラム実行のwebhookを送信

Github

webhookを受信すると上記スプレッドシートCSVを取得し

直近3日間のデータのポイントを作成

kmzを更新

ショートカット

f:id:imabari_ehime:20220321001514p:plain

f:id:imabari_ehime:20220321001532p:plain

f:id:imabari_ehime:20220321001549p:plain

f:id:imabari_ehime:20220321001615p:plain

スプレッドシート

function doPost(e) {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('data');

  let ary = [e.parameter.date, e.parameter.pref, e.parameter.city, e.parameter.address, e.parameter.lat, e.parameter.lng, e.parameter.enb, e.parameter.process];

  // 行の最後に値を追加
  sheet.appendRow(ary);

  // 3秒待機
  Utilities.sleep(3000);

  // Githubのアドレス
  const url = "https://api.github.com/repos/xxxxxx/yyyyyy/dispatches";

  let data = { event_type: "on-demand-form" };

  // Githubのtoken設定
  let options = {
    method: "POST",
    headers: {
      Authorization: "token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "Content-Type": "application/json",
    },
    payload: JSON.stringify(data),
  };

  console.log(options);

  UrlFetchApp.fetch(url, options);

}

github.com

import datetime
import pathlib

import pandas as pd
import simplekml

JST = datetime.timezone(datetime.timedelta(hours=+9))

dt_now = datetime.datetime.now(JST).replace(tzinfo=None)
dt_3dy = dt_now - datetime.timedelta(days=3)

# スプレッドシートのCSVのURL
url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSroTLHVCV2xgBucqPyevEtUblVM2cIpJv6SeZTHcbu_GSFQSNUb6KQyc6CDsFWjk5gieDmx126lWRm/pub?gid=0&single=true&output=csv"

df0 = pd.read_csv(url, parse_dates=["日付"])

# eNB-LCIDが重複は一番最後のデータを反映
df0.drop_duplicates(subset=["eNB-LCID"], keep="last", inplace=True)

df1 = df0[(df0["日付"] > dt_3dy) & (df0["処理"] != "削除")].drop(["日付", "処理"], axis=1)

kml = simplekml.Kml(name="temp")

# 開局

temp_img = kml.addfile("temp.png")

# スタイル
temp_normal = simplekml.Style()
temp_normal.iconstyle.scale = 1
temp_normal.iconstyle.icon.href = temp_img

# スタイル
temp_highlight = simplekml.Style()
temp_highlight.iconstyle.scale = 1
temp_highlight.iconstyle.icon.href = temp_img

temp_stylemap = simplekml.StyleMap()
temp_stylemap.normalstyle = temp_normal
temp_stylemap.highlightstyle = temp_highlight


# スタイルマップに登録

kml.document.stylemaps.append(temp_stylemap)

fol = kml.newfolder()

if len(df1) > 0:

    df1["場所"] = df1["市町村"].str.cat(df1["住所"])

    csv_path = pathlib.Path("map", "temp.csv")
    csv_path.parent.mkdir(parents=True, exist_ok=True)

    df1.to_csv(csv_path, encoding="utf_8_sig")

    for i, r in df1.iterrows():

        pnt = fol.newpoint(name=r["場所"])
        pnt.coords = [(r["経度"], r["緯度"])]

        pnt.stylemap = kml.document.stylemaps[0]
        pnt.description = f'eNB-LCID: {r["eNB-LCID"]}'

        ex_data = simplekml.ExtendedData()

        for n, v in r.items():

            ex_data.newdata(name=str(n), value=str(v))

        pnt.extendeddata = ex_data

kmz_path = pathlib.Path("map", "temp.kmz")

kml.savekmz(kmz_path)