为 folium 中的多边形着色
Colorize polygons in folium
我想在 python 中用 folium 在地图上打印一些彩色多边形,我需要一些帮助。我遇到的问题是我所有的多边形只得到一种颜色。这是我 运行 循环中的代码的一部分:
import folium
from shapely.geometry import Polygon
def map_color_rsrp(rsrp):
if (int(rsrp) in range(-70, -50)):
return '#10FF00'
elif (int(rsrp) in range(-90, -70)):
return 'green'
elif (int(rsrp) in range(-110, -90)):
return 'blue'
elif (int(rsrp) in range(-130, -110)):
return '#FF7000'
else: # range(-150, -130)
return 'grey'
# create map
grid_map = folium.Map((51.443787, 7.262206), zoom_start=14)
# create FeatureGroups for RSRP & RSRQ
rsrp_grid_fg = folium.FeatureGroup(name="RSRP", overlay=True)
grid_poly_rows = [
[-78.2, [(7.251043000000002, 51.444325000000035), (7.251043000000002, 51.44462500000004), (7.251343000000002, 51.44462500000004), (7.251343000000002, 51.444325000000035)]],
[-89.3, [(7.251343000000002, 51.444325000000035), (7.251343000000002, 51.44462500000004), (7.251643000000002, 51.44462500000004), (7.251643000000002, 51.444325000000035)]],
[-113.7, [(7.251643000000002, 51.44402500000003), (7.251643000000002, 51.444325000000035), (7.2519430000000025, 51.444325000000035), (7.2519430000000025, 51.44402500000003)]],
[-112.3, [(7.251643000000002, 51.444325000000035), (7.251643000000002, 51.44462500000004), (7.2519430000000025, 51.44462500000004), (7.2519430000000025, 51.444325000000035)]],
[-133.7, [(7.2519430000000025, 51.44402500000003), (7.2519430000000025, 51.444325000000035), (7.252243000000003, 51.444325000000035), (7.252243000000003, 51.44402500000003)]]
]
for row in grid_poly_rows:
mean_rsrp = row[0]
rsrp_tooltip_str = str(mean_rsrp)
rsrp_color = map_color_rsrp(mean_rsrp)
style_ = {
'fillColor': rsrp_color,
'color': rsrp_color,
'weight': 1,
'fillOpacity': 0.5}
folium.GeoJson(Polygon(row[1]),
style_function=lambda x: style_,
tooltip=rsrp_tooltip_str
).add_to(rsrp_grid_fg)
rsrp_grid_fg.add_to(grid_map)
# add LayerControl
folium.LayerControl(collapsed=False).add_to(grid_map)
grid_map.save(outfile="grid.html")
我觉得 style_function 有问题。任何建议表示赞赏!提前致谢!
干杯 ninjab3s
我做到了!我找到了这个post,说明lambda风格的函数没有立即执行。
以下更改使我的代码工作:
for row in grid_poly_rows:
mean_rsrp = row[0]
rsrp_tooltip_str = str(mean_rsrp)
folium.GeoJson(Polygon(row[1]),
style_function=lambda x, mean_rsrp=mean_rsrp: {
'fillColor': map_color_rsrp(mean_rsrp),
'color': map_color_rsrp(mean_rsrp),
'weight': 1,
'fillOpacity': 0.5},
tooltip=rsrp_tooltip_str
).add_to(rsrp_grid_fg)
下次我会先深入挖掘 post
我想在 python 中用 folium 在地图上打印一些彩色多边形,我需要一些帮助。我遇到的问题是我所有的多边形只得到一种颜色。这是我 运行 循环中的代码的一部分:
import folium
from shapely.geometry import Polygon
def map_color_rsrp(rsrp):
if (int(rsrp) in range(-70, -50)):
return '#10FF00'
elif (int(rsrp) in range(-90, -70)):
return 'green'
elif (int(rsrp) in range(-110, -90)):
return 'blue'
elif (int(rsrp) in range(-130, -110)):
return '#FF7000'
else: # range(-150, -130)
return 'grey'
# create map
grid_map = folium.Map((51.443787, 7.262206), zoom_start=14)
# create FeatureGroups for RSRP & RSRQ
rsrp_grid_fg = folium.FeatureGroup(name="RSRP", overlay=True)
grid_poly_rows = [
[-78.2, [(7.251043000000002, 51.444325000000035), (7.251043000000002, 51.44462500000004), (7.251343000000002, 51.44462500000004), (7.251343000000002, 51.444325000000035)]],
[-89.3, [(7.251343000000002, 51.444325000000035), (7.251343000000002, 51.44462500000004), (7.251643000000002, 51.44462500000004), (7.251643000000002, 51.444325000000035)]],
[-113.7, [(7.251643000000002, 51.44402500000003), (7.251643000000002, 51.444325000000035), (7.2519430000000025, 51.444325000000035), (7.2519430000000025, 51.44402500000003)]],
[-112.3, [(7.251643000000002, 51.444325000000035), (7.251643000000002, 51.44462500000004), (7.2519430000000025, 51.44462500000004), (7.2519430000000025, 51.444325000000035)]],
[-133.7, [(7.2519430000000025, 51.44402500000003), (7.2519430000000025, 51.444325000000035), (7.252243000000003, 51.444325000000035), (7.252243000000003, 51.44402500000003)]]
]
for row in grid_poly_rows:
mean_rsrp = row[0]
rsrp_tooltip_str = str(mean_rsrp)
rsrp_color = map_color_rsrp(mean_rsrp)
style_ = {
'fillColor': rsrp_color,
'color': rsrp_color,
'weight': 1,
'fillOpacity': 0.5}
folium.GeoJson(Polygon(row[1]),
style_function=lambda x: style_,
tooltip=rsrp_tooltip_str
).add_to(rsrp_grid_fg)
rsrp_grid_fg.add_to(grid_map)
# add LayerControl
folium.LayerControl(collapsed=False).add_to(grid_map)
grid_map.save(outfile="grid.html")
我觉得 style_function 有问题。任何建议表示赞赏!提前致谢!
干杯 ninjab3s
我做到了!我找到了这个post
for row in grid_poly_rows:
mean_rsrp = row[0]
rsrp_tooltip_str = str(mean_rsrp)
folium.GeoJson(Polygon(row[1]),
style_function=lambda x, mean_rsrp=mean_rsrp: {
'fillColor': map_color_rsrp(mean_rsrp),
'color': map_color_rsrp(mean_rsrp),
'weight': 1,
'fillOpacity': 0.5},
tooltip=rsrp_tooltip_str
).add_to(rsrp_grid_fg)
下次我会先深入挖掘 post