2020年2月20日木曜日

Folium

Folium 0.10.1
https://python-visualization.github.io/folium/

GeoDataFrame を Folium で地理院地図ベースに図化する例です。
※見やすさのため不要なインデントをつけています。

  1.  
  2. import folium
  3.  
  4. ##ポリゴンからコロプレスマップ
  5. #地物の数が多い(数万)とブラウザがクラッシュ。
  6. m = folium.Map([36, 140],
  7. zoom_start=7,
  8. tiles='https://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png',
  9. attr='淡色地図')
  10. m.choropleth(
  11. geo_data=gdf,
  12. name='choropleth',
  13. data=df,#gdfdfは結合していない
  14. columns=['key', 'A'], # dfのkeyと表示データ
  15. key_on='feature.properties.A', # gdfのキー
  16. fill_color='YlGn',# 色パレットを指定
  17. threshold_scale=[0,2,4,6,8,10,12,14], # 段階
  18. fill_opacity=0.7,
  19. line_opacity=0.2,
  20. legend_name='aaa')
  21. m # ポリゴンの多い場合は、コメントアウトして表示させない。
  22. m.save('./AAA.html')
  23.  
  24. ##ポイント
  25. #ポイント表示は数千まで?
  26.  #多い場合は表示させずにhtml保存。
  27. #GeoDataFrameを種類で分割
  28. gdfA=gdf[(gdf['A']==aaa)]
  29. gdfB=gdf[(gdf['A']==bbb)]
  30. #ベースマップ
  31. m = folium.Map([36, 140],
  32. zoom_start=9,
  33. tiles='https://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png',
  34. attr='淡色地図')
  35. #1種類目:赤色でプロット
  36. for index, row in gdfA.iterrows():
  37.   p_txt = '種類:' + row['A']
  38.   p_txt = p_txt + '年月日:' +
  39.           str(row['年'])+'/'+
  40.            str(row['月'])+'/'+
  41.             str(row['日'])
  42.   popup = folium.Popup(p_txt, max_width=2650)
  43.   folium.Circle(#units of meters
  44.   #folium.CircleMarker(#units of pixcels
  45.   radius=100,
  46.   location=[gdfA.Lat[index], gdfA.Lng[index]],
  47.   popup=popup,
  48.   color='red',
  49.   fill_opacity=0.5, line_opacity=1,
  50.   fill=True,
  51.   fill_color='red'
  52.     ).add_to(m)
  53. #2種類目:青色でプロット
  54. for index, row in gdfB.iterrows():
  55.   p_txt = '種類:' + row['A']
  56.   p_txt = p_txt + '年月日:' +
  57.           str(row['年'])+'/'+
  58.            str(row['月'])+'/'+
  59.             str(row['日'])
  60.   popup = folium.Popup(p_txt, max_width=2650)
  61.   folium.Circle(#units of meters
  62.   #folium.CircleMarker(#units of pixcels
  63.   radius=100,
  64.   location=[gdfB.Lat[index], gdfB.Lng[index]],
  65.   popup=popup,
  66.   color='blue',
  67.   fill_opacity=0.5, line_opacity=1,
  68.   fill=True,
  69.   fill_color='blue'
  70.     ).add_to(m)
  71. m # ポイントの多い場合はコメントアウトして表示させない。
  72. m.save('./AAA.html')
  73.  

0 件のコメント:

コメントを投稿