如何在Folium地图中添加“ Circle”大小的图例在python中以及如何注释文本以便在所有情况下都可见? 有人对如何执行此操作有想法吗? Web上对Folium的支持并不广泛,因此找不到类似的东西

问题描述

在下面找到我的代码我有一张欧洲地图和一些我想标记的城市及其人口规模。我想用“圆圈”标记那些城市,其大小应与缩放后的“人口”相对应。

  1. 我正在尝试在此地图上添加“圆圈”大小的图例,但我找不到解决方法。使用Circle Layer时,无法添加图例。

  2. 我也在尝试使用DivIcon在地图上注释城市名称,并且确实可以,但是有些城市彼此之间距离太近,因此,如果我可以将这些城市的名称进一步移动,我希望远离相应的点,并用一条线连接它们。

只需提一下,这里我仅显示一些英国城市,但总的来说,我具有整个欧洲和许多城市的地图,因此英国的那些城市名称实际上是不可见的。

有人对如何执行此操作有想法吗? 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 (将#修改为@)