-
-
Save pgtwitter/1cd0b3e2aea0a755538e1400a6f96241 to your computer and use it in GitHub Desktop.
気象庁の天気図をレイヤー毎に分ける(加工データの扱いについてはこちらを参照 https://www.jma.go.jp/jma/kishou/info/coment.html )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip | |
import xml.etree.ElementTree as ET | |
import pathlib | |
def map(distDir, root, ns): | |
ret = root.findall("./g/g[@id='map']", ns) | |
mapSettings = [ | |
['sea', 'path', 'fill', '#DEF'], | |
['fillmap', 'polyline', 'stroke', '#000'], | |
['latlon', 'polyline', 'stroke', '#555'] | |
] | |
for c, t, k, v in mapSettings: | |
elemSVG = ET.Element("svg") | |
elemG = ret[0].findall(f"./g[@class='{c}']", ns) | |
polylines = elemG[0].findall(f".//{t}", ns) | |
for pl in polylines: | |
pl.set(k, v) | |
elemSVG.append(elemG[0]) | |
with open(f"{distDir}/map_{c}.svg", "wb") as f: | |
ET.ElementTree(elemSVG).write(f, encoding="utf-8") | |
def others(distDir, root, ns): | |
ret = root.findall("./g/g[@id='weatherChart']/g", ns) | |
lys = [e.get("class") for e in ret] | |
if len(ret) == 0: | |
return | |
for ly in lys: | |
elemSVG = ET.Element("svg") | |
rets = root.findall(f"./g/g[@id='weatherChart']/g[@class='{ly}']", ns) | |
if len(rets) == 0 or len(rets[0]) == 0: | |
continue | |
for ret in rets: | |
if ly == "occFront" or ly == "stnFront" or ly == "coldFront" or ly == "warmFront": | |
frontSettings = [ | |
["occSymbol", "#FF0", "#FF0"], | |
["occLine", "#FF0", "none"], | |
["redSymbol", "#F00", "#F00"], | |
["blueSymbol", "#00F", "#00F"], | |
["redLine", "#F00", "none"], | |
["blueLine", "#00F", "none"] | |
] | |
for c, s, f in frontSettings: | |
symbols = ret.findall(f"./*[@class='{c}']", ns) | |
for e in symbols: | |
e.set("fill", f) | |
e.set("stroke", s) | |
else: | |
polylines = ret.findall(f".//polyline", ns) | |
for pl in polylines: | |
pl.set("stroke", "#000") | |
paths = ret.findall(f".//path", ns) | |
for pt in paths: | |
pt.set("stroke", "#000") | |
for ret in rets: | |
elemSVG.append(ret) | |
with open(f"{distDir}/{ly}.svg", "wb") as f: | |
ET.ElementTree(elemSVG).write(f, encoding="utf-8") | |
def getRootAndNS(filepath): | |
try: | |
with gzip.open(filepath, "rb") as f: | |
file_content = f.read() | |
except: | |
try: | |
with open(filepath, "r") as f: | |
file_content = f.read() | |
except: | |
raise "Read error." | |
root = ET.fromstring(file_content) | |
ns = {"": "http://www.w3.org/2000/svg"} | |
ns["ns0"] = ns[""] | |
ET.register_namespace("", ns[""]) | |
return root, ns | |
# https://www.data.jma.go.jp/fcd/yoho/wxchart/quickdaily.html?show=20230601 | |
# https://www.data.jma.go.jp/fcd/yoho/data/wxchart/quick/202306/SPAS_COLOR_202306010300.svgz | |
# 加工データの扱いについてはこちらを参照 https://www.jma.go.jp/jma/kishou/info/coment.html | |
srcFilepath = "./ASAS_COLOR_202306250000.svgz" | |
# srcFilepath = "./SPAS_COLOR_202306150300.svgz" | |
srcFinename = (srcFilepath.split("/")[-1]).split(".")[0] | |
distDir = f"/tmp/{srcFinename}" | |
pathlib.Path(distDir).mkdir(parents=True, exist_ok=True) | |
root, ns = getRootAndNS(srcFilepath) | |
map(distDir, root, ns) | |
others(distDir, root, ns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment