geojsonをfoliumで綺麗な地図を作る
市区町村の境界を地図上に描きたいという一心で、geojsonの可視化を実施。
geojsonとは
地図座標を記載したjsonファイルです。
{"type": "FeatureCollection", "features": [{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[139.275299,34.511135],[139.268494,34.516533],[139.268494,34.527065],[139.275299,34.532135],[139.279694,34.532066],[139.280304,34.534466],[139.280807,34.534401],[139.280594,34.532066],[139.283096,34.532066],[139.284103,34.532001],[139.288208,34.530533],[139.293106,34.5242],[139.281998,34.512466],[139.275299,34.511135]]]]},"properties":{"cartodb_id":688,"cityname_k":"利島村","prefname_k":"東京都","_5keta_code1":"133621","city_code":"13362","cityname_e":"Toshima Mura","prefname_e":"Tokyo To"}},{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[139.354401,35.527534],[139.3517,35.523598],[139.354996,35.519264],[139.347305,35.517399],[139.350296,35.508999],[139.347595,35.508331],
今回のデータは
今回はgitからデータを拝借してきました。
https://github.com/txslgr/geojson
コーディング
pythonで読み取る場合、特別なモジュールではなく、標準のjsonモジュールで読込む方が楽との事で、作ってみます。
import json
import codecs
import folium
def add_map(map,one):
for pos_lst in one['geometry']['coordinates']:
for pos_lst2 in pos_lst:
locations = [[pos[1],pos[0]] for pos in pos_lst2]
line = folium.PolyLine(locations=locations)
map.add_child(line)
with codecs.open(r'city_geojson.geojson','r','utf8') as fp:
dat = json.load(fp)
home_pos = dat['features'][0]['geometry']['coordinates'][0][0][0]
home_pos = [home_pos[1],home_pos[0]]
map = folium.Map(location=home_pos, zoom_start=11)
for one in dat['features']:
add_map(map,one)
map.save(outfile=r'map.html')
出来上がり!
おすすめ記事
エラーを解消したい ModuleNotFoundError: No module named ‘openpyxl’ - Python
MMDのVMD形式をPythonで読込む
全くの初心者がnetlifyとX Domain(X Server)でサーバーを独自ドメイン化した話
Raspberry pi Liteの非X Window環境でJackdを動かす
OpenCVで動画作成 VideoWriter - Python徹底解説
プログラムは独学が良いか、スクールが良いか?【無償カウンセリング、無料体験あり】
Supponsered
外部サイト
↓プログラムを学んでみたい場合、学習コースなどもおすすめです!