Skip to content

Instantly share code, notes, and snippets.

@tomowarkar
Created September 4, 2022 16:38
Show Gist options
  • Save tomowarkar/c6abb94985b4549a8c3d4f29fd87b231 to your computer and use it in GitHub Desktop.
Save tomowarkar/c6abb94985b4549a8c3d4f29fd87b231 to your computer and use it in GitHub Desktop.
石川県の色塗り
import fiona
import matplotlib.cm as cm
import pandas as pd
from matplotlib.colors import to_hex
from staticmap import Polygon, StaticMap
# src: https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N03-v3_1.html
c = fiona.open("N03-20220101_17_GML/N03-22_17_220101.shp", "r")
df = pd.DataFrame(c)
df = pd.concat([df.properties.apply(pd.Series), df.geometry.apply(pd.Series)], axis=1)
cities = {}
cmap = cm.tab20
# src: https://maps.gsi.go.jp/development/ichiran.html
smap = StaticMap(2400, 1800, url_template="https://cyberjapandata.gsi.go.jp/xyz/blank/{z}/{x}/{y}.png")
for _, r in df.iloc[1:].iterrows():
city = r["N03_004"]
if city not in cities:
cities[city] = to_hex(cmap(len(cities), alpha=0.6), keep_alpha=True)
color = cities[city]
poly = Polygon(r["coordinates"][0], color, color)
smap.add_polygon(poly)
smap.render()
@tomowarkar
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment