pandas 将branca.colormap.LinearColormap添加到一个页面中,

umuewwlo  于 2023-02-02  发布在  其他
关注(0)|答案(1)|浏览(133)

我有1个folium.Map,其中包含4个功能组,每个folium.Featuregroup都有一个folium.Choropleth
因此,我希望使用我创建的bins列表为每个要素组设置线性比例,该列表包括数据集中T*_depot_02_depots3_depots4_depots列的分位数结果。
目前我能够使用branca包创建一个线性色标,并将其添加到我的Map中。但这会导致在Map上显示所有线性色标。
我要查找的是,当我使用图层控制从站点1更改到站点2时,显示站点2的线性比例,而不显示站点1、3、4的比例。
我尝试过使用add_toadd_child方法将我的color_map添加到特定的功能组或Choropleth,但没有成功。
那么,如何向特定要素组添加(和显示)线性比例呢?

我的数据(Geopandas Dataframe ):

我的代码:

def add_depot_markers_featuregroup(depot_amount, featuregroup): 
    for i in range(len(depots_locations)):
        if i > depot_amount:
            break
        folium.Marker(
                [depots_locations[i].y, depots_locations[i].x], 
                popup="Depot_{0}".format(i+1),
                icon=folium.Icon(color='cadetblue', icon='solid fa-bicycle', prefix='fa')).add_to(featuregroup)

result_map = folium.Map(location=lat_long_groningen, zoom_start=11, tiles=None)

layer_depot_1 = folium.FeatureGroup(name="1 depot", overlay=False).add_to(result_map)
layer_depot_2 = folium.FeatureGroup(name="2 depots", overlay=False).add_to(result_map)
layer_depot_3 = folium.FeatureGroup(name="3 depots", overlay=False).add_to(result_map)
layer_depot_4 = folium.FeatureGroup(name="4 depots", overlay=False).add_to(result_map)

fs=[layer_depot_1, layer_depot_2, layer_depot_3, layer_depot_4]
for i in range(len(fs)):
    add_depot_markers_featuregroup(i, fs[i])
    depot_column_name = ""
    if i == 0:  
        depot_column_name = "T*_depot_{0}".format(i) 
    else: 
        depot_column_name = "{0}_depots".format(i+1)
    
    bins = list(results[depot_column_name].quantile([0, 0.25, 0.5, 0.75, 1]))

    choropleth = folium.Choropleth(
        results, 
        data=results, 
        key_on='feature.properties.Postcode', 
        columns=["Postcode", depot_column_name], 
        fill_color="YlOrRd",
        fill_opacity = 0.8,
        line_opacity = 0,
        line_weight=1,
        bins=bins,
        highlight=True,
        legend_name = "T* per postalcode area based on depot {0}".format(i+1),
        threshold_scale=bins,
        name="T* of {0} depot per Postalcode area".format(i+1)).geojson.add_to(fs[i])

    highlight_function = lambda x: {
        'fillColor': '#ffffff', 
        'color':'#000000', 
        'fillOpacity': 0.1, 
        'weight': 0.1
    }

    color_map = branca.colormap.LinearColormap(
        colors=['#ffffd4', '#fed98e', '#fe9929', '#d95f0e', '#993404'],
        index=bins, 
        vmin=bins[0], 
        vmax=bins[-1],
        caption='Routelength per postcode area based on {0} depots'.format(i+1)
    ).add_to(result_map)

    folium.GeoJson(data=results, 
        name="tooltip " + depot_column_name,
        tooltip=folium.GeoJsonTooltip(fields=['Postcode', "Deliveries", "Area (km2)", "Number of cyclists", "Beardwood approx", depot_column_name], labels=True, sticky=True),
        style_function=lambda feature: {
            "color": "black",
            "weight": 0.5,
        },
        highlight_function=highlight_function
    ).add_to(choropleth)

    # add legend: https://stackoverflow.com/questions/52911688/python-folium-choropleth-map-colors-incorrect/52981115#52981115
# min_values = results[["T*_depot_0", "2_depots", "3_depots", "4_depots"]].min()
# max_values = results[["T*_depot_0", "2_depots", "3_depots", "4_depots"]].max()
# color_index = range(round(min_values.min()), round(max_values.max()), 5) 
# color_map = branca.colormap.LinearColormap(
#         colors=['#ffffd4', '#fed98e', '#fe9929', '#d95f0e', '#993404'], 
#         index=color_index,
#         vmin=min_values.min(), 
#         vmax=max_values.max(),
#         caption='Routelength per postcode area'
# ).add_to(result_map)
   
folium.TileLayer(overlay=True, show=True, control=False, name="T* openstreet").add_to(result_map)
folium.TileLayer('cartodbdark_matter',overlay=True, show=False, name="T* dark mode").add_to(result_map)
folium.TileLayer('cartodbpositron',overlay=True, show=False, name="T* light mode").add_to(result_map)
folium.LayerControl(collapsed=False).add_to(result_map)
result_map.save("lastmilegroningen.html")
result_map

当前结果

pzfprimi

pzfprimi1#

我想你需要this之类的东西。
这里的基本思想是将每个colormap绑定到你的FeatureGroup,这意味着你需要链接的帖子的这一部分:

from branca.element import MacroElement

from jinja2 import Template

class BindColormap(MacroElement):
    """Binds a colormap to a given layer.

    Parameters
    ----------
    colormap : branca.colormap.ColorMap
        The colormap to bind.
    """
    def __init__(self, layer, colormap):
        super(BindColormap, self).__init__()
        self.layer = layer
        self.colormap = colormap
        self._template = Template(u"""
        {% macro script(this, kwargs) %}
            {{this.colormap.get_name()}}.svg[0][0].style.display = 'block';
            {{this._parent.get_name()}}.on('overlayadd', function (eventLayer) {
                if (eventLayer.layer == {{this.layer.get_name()}}) {
                    {{this.colormap.get_name()}}.svg[0][0].style.display = 'block';
                }});
            {{this._parent.get_name()}}.on('overlayremove', function (eventLayer) {
                if (eventLayer.layer == {{this.layer.get_name()}}) {
                    {{this.colormap.get_name()}}.svg[0][0].style.display = 'none';
                }});
        {% endmacro %}
        """)  # noqa

最后,您没有将colormapFeatureGroup添加到map,而是将它们绑定,然后将它们添加到map,如下所示:

m.add_child(BindColormap(color_map, i)

相关问题