python – 在folium中绘制带有地理数据帧的彩色多边形

前端之家收集整理的这篇文章主要介绍了python – 在folium中绘制带有地理数据帧的彩色多边形前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正试图在叶片中绘制雷达数据,我几乎就在那里.我按照这个例子(Contour plot data (lat,lon,value) within boundaries and export GeoJSON)将我的数据转换成GeoJson格式.

  1. nb_class = 20
  2. collec_poly = plt.contourf(lons,lats,np.array(poshdata),nb_class,alpha=0.5)
  3. gdf = collec_to_gdf(collec_poly) # From link above
  4. gdf.to_json()
  5. colors = [p.get_facecolor().tolist()[0] for p in collec_poly.collections]
  6. gdf['RGBA'] = colors
  7. gdf

这将输出两列:几何和RGBA.

  1. RGBA geometry
  2. 0 [0.0,0.0,0.713903743316,1.0] (POLYGON ((-71.57032079644679 42.2775236331535...
  3. 1 [0.0,0.0960784313725,1.0,1.0] (POLYGON ((-71.56719970703125 42.2721176147460...
  4. 2 [0.0,0.503921568627,1.0] (POLYGON ((-71.55678558349609 42.2721176147460...
  5. 3 [0.0,0.896078431373,0.970904490829,1.0] (POLYGON ((-71.52552795410156 42.2849182620049...
  6. 4 [0.325743200506,0.641998734978,1.0] (POLYGON ((-71.49427795410156 42.2939676156927...
  7. 5 [0.641998734978,0.325743200506,1.0] (POLYGON ((-71.47344207763672 42.3003084448852...
  8. 6 [0.970904490829,0.959331880901,1.0] (POLYGON ((-71.26508331298828 42.3200411822557...
  9. 7 [1.0,0.581699346405,1.0] (POLYGON ((-71.15048217773438 42.3333218460720...

从那里我制作了我的folium地图:

  1. import folium
  2. # Picked location between Sudbury and Somerville:
  3. maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=11,tiles="Stamen Toner")
  4. folium.GeoJson(gdf).add_to(maploc)

这创建了我很好的folium地图,但多边形根本没有着色.如何让轮廓充满正确的颜色?并修复不透明度?

enter image description here

最佳答案
不是专家……我刚刚开始使用folium和jupyter,并且有类似的问题,但是有了线条.
你说你有GeoJson和多边形,颜色包含在我认为的json中.

style_function可能会帮助你得到你想要的东西?

以下示例使用此页面生成http://geojson.io/
我所要做的就是使用style_function进行“映射”.
也可以使用自定义函数,请参阅:
https://github.com/python-visualization/folium/blob/master/examples/Colormaps.ipynb

  1. import folium
  2. geoJsonData = {
  3. "features": [
  4. {
  5. "geometry": {
  6. "coordinates": [
  7. [
  8. 12.98583984375,56.70450561416937
  9. ],[
  10. 14.589843749999998,57.604221411628735
  11. ],[
  12. 13.590087890625,58.15331598640629
  13. ],[
  14. 11.953125,57.955674494979526
  15. ],[
  16. 11.810302734375,58.76250326278713
  17. ]
  18. ],"type": "LineString"
  19. },"properties": {
  20. "stroke": "#fc1717","stroke-opacity": 1,"stroke-width": 2
  21. },"type": "Feature"
  22. },{
  23. "geometry": {
  24. "coordinates": [
  25. [
  26. 14.9468994140625,57.7569377956732
  27. ],[
  28. 15.078735351562498,58.06916140721414
  29. ],[
  30. 15.4302978515625,58.09820267068277
  31. ],[
  32. 15.281982421875002,58.318144965188246
  33. ],[
  34. 15.4852294921875,58.36427519285588
  35. ]
  36. ],"properties": {
  37. "stroke": "#1f1a95","type": "Feature"
  38. }
  39. ],"type": "FeatureCollection"
  40. }
  41. m = folium.Map(location=[ 56.7,12.9],zoom_start=6)
  42. folium.GeoJson(geoJsonData,style_function=lambda x: {
  43. 'color' : x['properties']['stroke'],'weight' : x['properties']['stroke-width'],'opacity': 0.6,'fillColor' : x['properties']['fill'],}).add_to(m)
  44. m

git hub上的folium源代码包括几个不错的例子:
    https://github.com/python-visualization/folium/tree/master/examples
在这里您可以找到可供选择的选项:
    http://leafletjs.com/reference.html#path-options

希望这能带给你前进!

猜你在找的Python相关文章