概要
paiza.ioでelixirやってみた。
練習問題やってみた。
練習問題
jsonをパースして、geojsonを、生成せよ。
サンプルコード
defmodule Main do
def main() do
json = """
{
"bikuya": [{
"lat": "37.929687",
"lon": "140.151111",
"name": "オートショップユニット"
}, {
"lat": "37.929220",
"lon": "140.109344",
"name": "(株)エイジュウプロ"
}, {
"lat": "37.923453",
"lon": "140.093108",
"name": "(株)ホンダウイングロードショウ"
}, {
"lat": "37.911333",
"lon": "140.112004",
"name": "(有)カーセンター葵商会"
}, {
"lat": "37.907591",
"lon": "140.108036",
"name": "塚本サイクル"
}, {
"lat": "37.928429",
"lon": "140.114247",
"name": "サイクルショップ川口"
}, {
"lat": "37.916279",
"lon": "140.099879",
"name": "サイクルショップ中村"
}, {
"lat": "37.917854",
"lon": "140.101416",
"name": "菅原商会"
}, {
"lat": "37.912485",
"lon": "140.108688",
"name": "サイクルセンター卯月"
}, {
"lat": "37.912049",
"lon": "140.105914",
"name": "ホンダ野村モータース"
}, {
"lat": "37.869915",
"lon": "140.103534",
"name": "モーターハウスK"
}, {
"lat": "37.918955",
"lon": "140.109289",
"name": "ライダースサロン・ヤマカ"
}, {
"lat": "37.915746",
"lon": "140.108313",
"name": "中央ホンダ"
}, {
"lat": "37.905011",
"lon": "140.107225",
"name": "香澄ホンダ"
}, {
"lat": "37.912663",
"lon": "140.123328",
"name": "佐藤オート商会"
}, {
"lat": "37.910789",
"lon": "140.121636",
"name": "香坂輪店"
}, {
"lat": "37.958525",
"lon": "140.125682",
"name": "斎藤モータース"
}, {
"lat": "37.956678",
"lon": "140.117820",
"name": "ニューホンダ石山"
}, {
"lat": "37.903901",
"lon": "140.131694",
"name": "二輪屋たかはし"
}, {
"lat": "37.886019",
"lon": "140.104554",
"name": "夢工場"
}, {
"lat": "37.874814",
"lon": "140.103239",
"name": "あべ輪店"
}, {
"lat": "37.905055",
"lon": "140.137379",
"name": "レッドバロン米沢"
}]
}
"""
json = String.replace(json, "\n", "")
json = String.replace(json, "\t", "")
line = String.split(json, "lat")
IO.write """
{"type":"FeatureCollection","features":[
"""
Enum.map(line, fn s ->
mat = Regex.named_captures(~r/\": \"(?<lat>.+)\",\"lon\": \"(?<lon>.+)\",\"name\": \"(?<name>.+)\"}/, s)
print(mat["lat"], mat["lon"], mat["name"])
end)
IO.puts "{}]}"
end
def print(nil, nil, nil) do
end
def print(lat, lon, name) do
IO.write "{\"type\":\"Feature\",\"properties\":{\"name\":\"#{name}\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"#{lon}\",\"#{lat}\"]}},"
end
end
Main.main
実行結果
{"type":"FeatureCollection","features":[
{"type":"Feature","properties":{"name":"オートショップユニット"},"geometry":{"type":"Point","coordinates":["140.151111","37.929687"]}},{"type":"Feature","properties":{"name":"(株)エイジュウプロ"},"geometry":{"type":"Point","coordinates":["140.109344","37.929220"]}},{"type":"Feature","properties":{"name":"(株)ホンダウイングロードショウ"},"geometry":{"type":"Point","coordinates":["140.093108","37.923453"]}},{"type":"Feature","properties":{"name":"(有)カーセンター葵商会"},"geometry":{"type":"Point","coordinates":["140.112004","37.911333"]}},{"type":"Feature","properties":{"name":"塚本サイクル"},"geometry":{"type":"Point","coordinates":["140.108036","37.907591"]}},{"type":"Feature","properties":{"name":"サイクルショップ川口"},"geometry":{"type":"Point","coordinates":["140.114247","37.928429"]}},{"type":"Feature","properties":{"name":"サイクルショップ中村"},"geometry":{"type":"Point","coordinates":["140.099879","37.916279"]}},{"type":"Feature","properties":{"name":"菅原商会"},"geometry":{"type":"Point","coordinates":["140.101416","37.917854"]}},{"type":"Feature","properties":{"name":"サイクルセンター卯月"},"geometry":{"type":"Point","coordinates":["140.108688","37.912485"]}},{"type":"Feature","properties":{"name":"ホンダ野村モータース"},"geometry":{"type":"Point","coordinates":["140.105914","37.912049"]}},{"type":"Feature","properties":{"name":"モーターハウスK"},"geometry":{"type":"Point","coordinates":["140.103534","37.869915"]}},{"type":"Feature","properties":{"name":"ライダースサロン・ヤマカ"},"geometry":{"type":"Point","coordinates":["140.109289","37.918955"]}},{"type":"Feature","properties":{"name":"中央ホンダ"},"geometry":{"type":"Point","coordinates":["140.108313","37.915746"]}},{"type":"Feature","properties":{"name":"香澄ホンダ"},"geometry":{"type":"Point","coordinates":["140.107225","37.905011"]}},{"type":"Feature","properties":{"name":"佐藤オート商会"},"geometry":{"type":"Point","coordinates":["140.123328","37.912663"]}},{"type":"Feature","properties":{"name":"香坂輪店"},"geometry":{"type":"Point","coordinates":["140.121636","37.910789"]}},{"type":"Feature","properties":{"name":"斎藤モータース"},"geometry":{"type":"Point","coordinates":["140.125682","37.958525"]}},{"type":"Feature","properties":{"name":"ニューホンダ石山"},"geometry":{"type":"Point","coordinates":["140.117820","37.956678"]}},{"type":"Feature","properties":{"name":"二輪屋たかはし"},"geometry":{"type":"Point","coordinates":["140.131694","37.903901"]}},{"type":"Feature","properties":{"name":"夢工場"},"geometry":{"type":"Point","coordinates":["140.104554","37.886019"]}},{"type":"Feature","properties":{"name":"あべ輪店"},"geometry":{"type":"Point","coordinates":["140.103239","37.874814"]}},{"type":"Feature","properties":{"name":"レッドバロン米沢"},"geometry":{"type":"Point","coordinates":["140.137379","37.905055"]}},{}]}
成果物
以上。