如何解决如何在Folium地图中添加“ Circle”大小的图例在python中以及如何注释文本以便在所有情况下都可见? 有人对如何执行此操作有想法吗? Web上对Folium的支持并不广泛,因此找不到类似的东西
在下面找到我的代码。我有一张欧洲地图和一些我想标记的城市及其人口规模。我想用“圆圈”标记那些城市,其大小应与缩放后的“人口”相对应。
只需提一下,这里我仅显示一些英国城市,但总的来说,我具有整个欧洲和许多城市的地图,因此英国的那些城市名称实际上是不可见的。
有人对如何执行此操作有想法吗? Web上对folium的支持并不广泛,因此找不到类似的东西。
import pandas as pd
import numpy as np
import folium
import json
import geopandas as gpd
# Pretty display for notebooks
%matplotlib inline
data = [['Birmingham',1141374,52.4796992,-1.9026911],['Bradford',537173,53.7944229,-1.7519186],['Bristol',463405,51.4538022,-2.5972985],['Edinburgh',518500,55.9533456,-3.1883749],['Glasgow',626410,55.8609825,-4.2488787],['Leeds',789194,53.7974185,-1.5437941],['Liverpool',494814,53.407154,-2.991665]]
cities = pd.DataFrame(data,columns = ['City','Population','Latitude','Longitude'])
europe = #geojson file of Europe dowloaded from https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/administrative-units-statistical-units/countries
import folium.plugins
from folium.features import *
# adding the DivIcon element for the text annotation
class DivIcon(MacroElement):
def __init__(self,html='',size=(30,30),anchor=(0,0),style=''):
"""Todo : docstring here"""
super(DivIcon,self).__init__()
self._name = 'DivIcon'
self.size = size
self.anchor = anchor
self.html = html
self.style = style
self._template = Template(u"""
{% macro header(this,kwargs) %}
<style>
.{{this.get_name()}} {
{{this.style}}
}
</style>
{% endmacro %}
{% macro script(this,kwargs) %}
var {{this.get_name()}} = L.divIcon({
className: '{{this.get_name()}}',iconSize: [{{ this.size[0] }},{{ this.size[1] }}],iconAnchor: [{{ this.anchor[0] }},{{ this.anchor[1] }}],html : "{{this.html}}",});
{{this._parent.get_name()}}.setIcon({{this.get_name()}});
{% endmacro %}
""")
# creating the map
map = folium.Map(location=[53.7974185,zoom_start=4,tiles=None)
style1 = {'fillColor': '#4682b4','fill': True,'weight': 1,'opacity': 0.3,'fillOpacity': 1}
style2 = {'color': 'black','weight': -1,'opacity': 0.2}
folium.GeoJson(
europe,name='Europe',overlay=False,control= False,style_function = lambda x: style2).add_to(map)
# Adding a circle for each selected city
for idx,city in cities.iterrows():
folium.Circle(location=(city['Latitude'],city['Longitude']),popup=city['City'],radius=city['Population']/10000,fillColor= 'red',color= 'red',fill= True,fillOpacity= 0,opacity= 1,weight= 2
).add_to(map)
folium.Marker(location=(city['Latitude'],icon=DivIcon(
size=(75,25),anchor=(50,-3),html=city.City,style='font-size:13px; color:black; text-align: center')).add_to(map)
map
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。