Python 创建一个对开本地图上的图例
本文件目前不完整: 根据不完整文档的索引,图例和图层是或将被支持的。我花了一些时间在网上寻找例子,但到目前为止什么也没找到。如果有人知道如何创建这些东西,或者能给我指点文档或教程,我将不胜感激。尝试使用Python 创建一个对开本地图上的图例,python,folium,Python,Folium,本文件目前不完整: 根据不完整文档的索引,图例和图层是或将被支持的。我花了一些时间在网上寻找例子,但到目前为止什么也没找到。如果有人知道如何创建这些东西,或者能给我指点文档或教程,我将不胜感激。尝试使用 feature_group = FeatureGroup(name='Layer1') feature_group2 = FeatureGroup(name='Layer2') 然后添加到地图中 map = folium.Map(zoom_start=6) # coordinates to
feature_group = FeatureGroup(name='Layer1')
feature_group2 = FeatureGroup(name='Layer2')
然后添加到地图中
map = folium.Map(zoom_start=6)
# coordinates to locate your marker
COORDINATE = [(333,333)] # example coordinate
COORDINATE2 = [(444,444)]
# add marker to your map
folium.Marker(location=COORDINATE).add_to(feature_group)
folium.Marker(location=COORDINATE2).add_to(feature_group2)
map.add_child(feature_group)
map.add_child(feature_group2)
# turn on layer control
map.add_child(folium.map.LayerControl())
我也遇到了同样的问题,并使用这个快速黑客攻击Folium生成的HTML来添加一个图例。它不是特别优雅,但很管用。因为我只需要这几次,所以我手动将图例生成为一个图像(legend.png),但是我想如果您经常这样做,您可以创建一个脚本来自动创建图例。我在Folium将地图输出到的HTML文件的相应部分中添加了以下组件:
<style> #background_img {
position : absolute;
background:url('legend.png');
width : 16.9%;
height: 17.7%;
right: 20px;
bottom: 50px;
z-index: 99;
background-repeat: no-repeat;
background-size: contain;
}
</style>
<div id="background_img" class="backgroundimg" ></div>
#背景#{
位置:绝对位置;
背景:url('legend.png');
宽度:16.9%;
身高:17.7%;
右:20px;
底部:50px;
z指数:99;
背景重复:无重复;
背景尺寸:包含;
}
您还需要将地图样式元素的z索引更改为小于99的值,以便图例位于地图上方 Folium现在有了一种在0.15版本中轻松添加图像的方法
from folium.plugins import FloatImage
image_file = 'image.PNG'
FloatImage(image_file, bottom=0, left=86).add_to(mymap)
你可以很容易地添加一个图例
#specify the min and max values of your data
colormap = branca.colormap.linear.YlOrRd_09.scale(0, 8500)
colormap = colormap.to_step(index=[0, 1000, 3000, 5000, 8500])
colormap.caption = 'Incidents of Crime in Victoria (year ending June 2018)'
colormap.add_to(world_map)
你可以在这里看到我完整的例子
如果将html添加到标记或多段线的名称参数中,则图层控件中的文本/标签颜色可以达到一半:
import folium
print( folium.__version__)
import numpy as np
lon_ct = 50
fkt=10
num = 60
m = folium.Map((lon_ct, 6), tiles='stamentoner', zoom_start=6 )
lats = (lon_ct * np.cos(np.linspace(0, 2*np.pi, num))/fkt ) + lon_ct
lons = (lon_ct * np.sin(np.linspace(0, 2*np.pi, num))/fkt ) + 10
colors = np.sin(5 * np.linspace(0, 2*np.pi, num))
lgd_txt = '<span style="color: {col};">{txt}</span>'
for idx, color in enumerate( ['red', 'blue']): # color choice is limited
print(color)
fg = folium.FeatureGroup(name= lgd_txt.format( txt= color+' egg', col= color))
pl = folium.features.PolyLine(
list(zip(lats, lons - idx*fkt)),
color=color,
weight=10, )
fg.add_child(pl)
m.add_child( fg)
folium.map.LayerControl('topleft', collapsed= False).add_to(m)
m
importfolium
印刷品(对开本)
将numpy作为np导入
lon_ct=50
fkt=10
num=60
m=folium.Map((lon\u ct,6),tiles='stamentoner',zoom\u start=6)
lats=(lon_ct*np.cos(np.linspace(0,2*np.pi,num))/fkt)+lon_ct
lons=(lon_ct*np.sin(np.linspace(0,2*np.pi,num))/fkt)+10
colors=np.sin(5*np.linspace(0,2*np.pi,num))
lgd_txt='{txt}'
对于idx,枚举中的颜色(['red','blue']):#颜色选择有限
印刷品(彩色)
fg=folium.FeatureGroup(name=lgd_txt.format(txt=color+'egg',col=color))
pl=folium.features.PolyLine(
列表(邮政编码(lats、lons-idx*fkt)),
颜色=颜色,
重量=10,)
前景。添加子项(pl)
m、 添加子对象(前景)
folium.map.LayerControl('topleft',collazed=False)。将_添加到(m)
M
资料来源:
如果您了解一点HTML:
item_txt = """<br> {item} <i class="fa fa-map-marker fa-2x" style="color:{col}"></i>"""
html_itms = item_txt.format( item= "mark_1" , col= "red")
legend_html = """
<div style="
position: fixed;
bottom: 50px; left: 50px; width: 200px; height: 160px;
border:2px solid grey; z-index:9999;
background-color:white;
opacity: .85;
font-size:14px;
font-weight: bold;
">
{title}
{itm_txt}
</div> """.format( title = "Legend html", itm_txt= html_itms)
map.get_root().html.add_child(folium.Element( legend_html ))
item_txt=”““
{item}”
html\u itms=item\u txt.format(item=“mark\u 1”,col=“red”)
图例_html=“”
{title}
{itm_txt}
“”格式(title=“图例html”,itm\U txt=html\U itms)
map.get_root().html.add_child(folium.Element(legend_html))
链接
链接
m.get_root().html
- 进行进一步的操作
branca.element.element
这真的很好。
m.get_root().html
<branca.element.Element at 0x7f5e1ca61250>