如何显示平均值而不是对页标记聚类的计数?

2024-10-03 23:25:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python中的folium包来显示数据的MarkerClusters。在

当你没有放大所有的方式,集群看起来不错,但他们似乎显示了一个计数的子标记内的集群。我理解为什么这是默认行为,但出于我的目的,我真的希望簇显示给定簇内每个单独标记在缩放级别上的平均值。在

下面是我现在的代码:

folium_map = folium.Map(location=[33.97810188618428, -118.2155395906348])
mc = MarkerCluster()
for p in points:
    marker = build_folium_marker(p['f_name'], p['value'], p['lat'], p['lng'])
    mc.add_child(marker)
folium_map.add_children(mc)
folium_map.save('folium_marker_cluster_map.html')

在理想的情况下,MarkerCluster会使用一些参数,让您发送“count”或“average”,但事实并非如此。我是谨慎乐观的有人将能够建议一个合理的简单的修复,不涉及分叉传单(js库的折页是建立在)和编辑js源代码。我不是第一个想在MarkerClusters上显示一个不同于sum的度量的人,特别是集群中标记值的平均值。在


Tags: 数据标记目的addmap方式js集群
1条回答
网友
1楼 · 发布于 2024-10-03 23:25:33

要自定义标记簇icon_create_function函数,下面的示例演示如何重写标记标签以显示自定义值而不是默认值(簇中的标记数):

icon_create_function = '''
    function(cluster) {
        return L.divIcon({
             html: '<b>' + 123 + '</b>',
             className: 'marker-cluster marker-cluster-small',
             iconSize: new L.Point(20, 20)
        });
    }
'''

marker_cluster = MarkerCluster(icon_create_function=icon_create_function)

enter image description here

现在轮到通过marker传递自定义属性了,in Folium default marker不支持它,但是可以引入以下marker类来扩展Marker类:

^{pr2}$

现在在Folium中,一旦marker对象被实例化(其中population是一个自定义属性)

marker = MarkerWithProps(
    location=marker_item['location'],
    props = { 'population': marker_item['population']}
)
marker.add_to(marker_cluster)

它的自定义属性可以通过JavaScript访问:

var markers = cluster.getAllChildMarkers();
var sum = 0;
for (var i = 0; i < markers.length; i++) {
  sum += markers[i].options.props.population;
}

总之,下面是一个示例,演示如何:

  • 通过标记传递自定义属性
  • 计算每个群集标记的自定义标记属性的平均值
  • 显示簇标记的自定义标签

示例

#%%
import json
import folium
from folium import Marker
from folium.plugins import MarkerCluster
from jinja2 import Template


class MarkerWithProps(Marker):
    _template = Template(u"""
        {% macro script(this, kwargs) %}
        var {{this.get_name()}} = L.marker(
            [{{this.location[0]}}, {{this.location[1]}}],
            {
                icon: new L.Icon.Default(),
                {%- if this.draggable %}
                draggable: true,
                autoPan: true,
                {%- endif %}
                {%- if this.props %}
                props : {{ this.props }} 
                {%- endif %}
                }
            )
            .addTo({{this._parent.get_name()}});
        {% endmacro %}
        """)
    def __init__(self, location, popup=None, tooltip=None, icon=None,
                 draggable=False, props = None ):
        super(MarkerWithProps, self).__init__(location=location,popup=popup,tooltip=tooltip,icon=icon,draggable=draggable)
        self.props = json.loads(json.dumps(props))    



map = folium.Map(location=[44, -73], zoom_start=4)

marker_data =(
    {
        'location':[40.67, -73.94],
        'population': 200     
    },
    {
        'location':[44.67, -73.94],
        'population': 300     
    }
)

icon_create_function = '''
    function(cluster) {
        var markers = cluster.getAllChildMarkers();
        var sum = 0;
        for (var i = 0; i < markers.length; i++) {
            sum += markers[i].options.props.population;
        }
        var avg = sum/cluster.getChildCount();

        return L.divIcon({
             html: '<b>' + avg + '</b>',
             className: 'marker-cluster marker-cluster-small',
             iconSize: new L.Point(20, 20)
        });
    }
'''

marker_cluster = MarkerCluster(icon_create_function=icon_create_function)

for marker_item in marker_data:
    marker = MarkerWithProps(
        location=marker_item['location'],
        props = { 'population': marker_item['population']}
    )
    marker.add_to(marker_cluster)

marker_cluster.add_to(map)    

#m.save(os.path.join('results', '1000_MarkerCluster0.html'))
map

结果

enter image description here

相关问题 更多 >