是否可以更改大叶草标记簇图中使用的默认颜色?

时间:2019-04-12 18:48:22

标签: python colors folium leaflet.markercluster

我正在使用叶片来生成一些地图,而我所包含的功能之一是标记集群叠加层-因为我经常在地图上绘制数千个点。聚类将数量不等的GPS点组合在一起,并在地图图标上方覆盖一个数字,该数字代表将多少个点组合到该簇中。默认情况下,群集中组合在一起的点越少,地图图标将变为绿色,组合在一起的点越多,将越靠近红色光谱。理想情况下,我想扭转这种情况,以便在一个位置中有很多合并点时,该图标将为绿色。而当只有几个合并点时,颜色将为红色。我认为这需要在branca模块中的某个位置进行编辑,但是我不确定,并且通常不太熟悉branca的工作方式。非常感谢您的帮助。

以下是通常如何创建标记簇的示例:

import folium
from folium.plugins import MarkerCluster
#Create the map image and establish the center point
mapImage = folium.Map(location=[40.165505, -99.788130], 
zoom_start=12, 
tiles='OpenStreetMap')

#Create the marker cluster group, which organizes all the gps points put into it
marker_cluster_group = MarkerCluster(name='Cluster Icons')
#This is just a reference to a default google mapping icon, purely optional
pointIcon_url = "http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
#Create the icon object    
icon = folium.features.CustomIcon(pointIcon_url, icon_size=(15, 15))
#Create the marker/gps point and add it to the cluster group
folium.Marker([40.058377, -99.939192], icon=icon).add_to(marker_cluster_group)
#Adding the cluster group to the map image
marker_cluster_group.add_to(mapImage)

2 个答案:

答案 0 :(得分:0)

您可以为MarkerCluster类提供参数icon_create_function,该参数将为群集图标设置样式:

https://github.com/python-visualization/folium/blob/8595240517135d1637ca4cf7cc624045f1d911b3/folium/plugins/marker_cluster.py#L31

在这里您可以看到该函数的外观示例:

https://github.com/Leaflet/Leaflet.markercluster#customising-the-clustered-markers

这是您作为字符串提供给folium的Javascript函数。

答案 1 :(得分:0)

借助@Conengmo的回复,我能够获取所需的信息,并根据需要对其进行修改以创建以下内容。

import folium
from folium.plugins import MarkerCluster
#Create the map image and establish the center point
mapImage = folium.Map(location=[40.165505, -99.788130], 
zoom_start=12, 
tiles='OpenStreetMap')

#Create a variable to store your javascript function (written as a string), which adjusts the default css functionality
#The below are the attributes that I needed for my project, but they can be whatever is needed for you
icon_create_function = """
    function(cluster) {
    var childCount = cluster.getChildCount(); 
    var c = ' marker-cluster-';

    if (childCount < 50) {
        c += 'large';
    } else if (childCount < 300) {
        c += 'medium';
    } else {
        c += 'small';
    }

    return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
    }
    """
#Create the marker cluster group, which organizes all the gps points put into it
marker_cluster_group = MarkerCluster(name='Cluster Icons', icon_create_function=icon_create_function)
#This is just a reference to a default google mapping icon, purely optional
pointIcon_url = "http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
#Create the icon object    
icon = folium.features.CustomIcon(pointIcon_url, icon_size=(15, 15))
#Create the marker/gps point and add it to the cluster group
folium.Marker([40.058377, -99.939192], icon=icon).add_to(marker_cluster_group)
#Adding the cluster group to the map image
marker_cluster_group.add_to(mapImage)