【问题标题】:How to add categorical layered data to LayerControl() in python Folium map?如何在 python Folium 地图中将分类分层数据添加到 LayerControl()?
【发布时间】:2021-11-29 06:07:10
【问题描述】:

我有这个分类数据集,我希望每个类别/组都成为我可以打开/关闭的层。

我能够在“LayerControl”上添加组,但它无法按预期正常工作。

import folium

m = folium.Map(location=[39.712183, -104.998424], zoom_start=5)

data = [(36.314292, -117.517516, 'P1'),
             (40.94859, -116.15316, 'P2'),
             (34.14757, -119.81985, 'P3'),
             (46.31292, -117.57516, 'P4'),
             (41.04159, -116.15316, 'P2'),
             (44.22093, -119.821985,'P2'), 
             (42.25308, -117.27589, 'P3'),
             (41.60302, -115.97012, 'P4'),
             (44.35519, -117.94183, 'P4'),
             (44.02027, -117.22198, 'P1'),
             (45.91613, -113.05364, 'P5'),
             (48.17537, -117.90075, 'P1'),
             (37.65961, -117.61321, 'P1')]


for x in range(0, len(data)):
    
    point_layer = folium.FeatureGroup(name = latLong[x][2])

    for lat,lng,nameP in latLong:
        point_layer.add_child(folium.CircleMarker(location=[lat, lng], radius=10,
            popup=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng), 
            tooltip=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng),
            fill=True,  # Set fill to True
            color='red',
            fill_opacity=1.0)).add_to(m)

    
    m.add_child(point_layer)


m.add_child(folium.LayerControl(collapsed=False))  
m.save("Map1.html")

正如您从上方看到的,按钮已关闭,但点/圆圈仍显示在地图上。有解决这个问题的想法吗?

【问题讨论】:

    标签: python leaflet folium


    【解决方案1】:

    要将标记与图层关联,请创建一个点组并将标记设置为属于它。然后,您可以将点组添加到地图中。问题中的代码在循环过程中创建了一个点群,但是我们可以改变这个过程,提前创建一个点群,然后添加到地图中。

    import folium
    
    m = folium.Map(location=[39.712183, -104.998424], zoom_start=5)
    
    latLong = [(36.314292, -117.517516, 'P1'),
                 (40.94859, -116.15316, 'P2'),
                 (34.14757, -119.81985, 'P3'),
                 (46.31292, -117.57516, 'P4'),
                 (41.04159, -116.15316, 'P2'),
                 (44.22093, -119.821985,'P2'), 
                 (42.25308, -117.27589, 'P3'),
                 (41.60302, -115.97012, 'P4'),
                 (44.35519, -117.94183, 'P4'),
                 (44.02027, -117.22198, 'P1'),
                 (45.91613, -113.05364, 'P5'),
                 (48.17537, -117.90075, 'P1'),
                 (37.65961, -117.61321, 'P1')]
    
    # point_layer name list
    all_gp = []
    for x in range(len(latLong)):
        pg = latLong[x][2]
        all_gp.append(pg)
    
    # Create point_layer object
    unique_gp = list(set(all_gp))
    vlist = []
    for i,k in enumerate(unique_gp):
        locals()[f'point_layer{i}'] = folium.FeatureGroup(name=k)
        vlist.append(locals()[f'point_layer{i}'])
    
    # Creating list for point_layer
    pl_group = []
    for n in all_gp:
        for v in vlist: 
            if n == vars(v)['layer_name']:
                pl_group.append(v)
    
    for (lat,lng,nameP),pg in zip(latLong, pl_group):
        folium.CircleMarker(location=[lat, lng], radius=10,
            popup=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng), 
            tooltip=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng),
            fill=True,  # Set fill to True
            color='red',
            fill_opacity=1.0).add_to(pg)
        pg.add_to(m)
    
    m.add_child(folium.LayerControl(collapsed=False))  
    #m.save("Map1.html")
    m
    

    【讨论】:

    • 这已经接近了,谢谢。但是,当添加新的点组(比如说 P6)时,我看到了问题,这意味着必须手动设置 FeatureGroup 和 pl_group。如果它可以动态地接受任意点组/图层,我真的很喜欢?
    • 这是您关于如何对标记进行分组并将它们与图层相关联的问题的答案。处理新组的添加是 Python 的领域,而不是 folium。这包括在您的问题中吗?可以在循环处理中创建局部变量。
    • 已修复,以便在添加新的点编组时,将自动创建并与该编组关联,而无需手动修改。请检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2013-10-29
    • 2020-05-12
    相关资源
    最近更新 更多