Skip to content

Instantly share code, notes, and snippets.

@junichim
Last active November 10, 2021 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junichim/46b81abb292149d49a1dae60725cea1f to your computer and use it in GitHub Desktop.
Save junichim/46b81abb292149d49a1dae60725cea1f to your computer and use it in GitHub Desktop.
避難所検索@伊勢の避難所データ加工のためのスクリプト
"""
伊勢市のオープンデータライブラリの災害時避難所情報のkmlファイルから
避難所名、緯度軽度のCSVファイルを生成するスクリプト
注意
避難所名に含まれている改行文字は半角スペースに変換している
"""
import xml.etree.ElementTree as et
import csv
import argparse
def parseKml(kmlfile, csvfile):
ns = {'kml': 'http://www.opengis.net/kml/2.2'}
tree = et.ElementTree(file=kmlfile)
root = tree.getroot()
doc = root[0]
placemarks = doc.findall("kml:Placemark", ns)
with open(csvfile, "wt") as file:
writer = csv.writer(file)
for p in placemarks:
name = p.findall("./kml:name", ns)[0]
latlonstr = p.findall("./kml:Point/kml:coordinates", ns)[0]
latlon = latlonstr.text.split(",")
# 名称、緯度、経度の並びで出力
row = [name.text.replace("\n", " "), latlon[1], latlon[0]]
writer.writerow(row)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="伊勢市のオープンデータの災害時避難所のkmlファイルから緯度経度のCSVファイルを生成する")
parser.add_argument("kmlfile", help="災害時避難所のkmlファイル名")
parser.add_argument("csvfile", help="出力ファイル名")
args = parser.parse_args()
parseKml(args.kmlfile, args.csvfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment