【问题标题】:Search Box and Layer control on multiple layers folium Python多层folium Python上的搜索框和图层控制
【发布时间】:2020-12-12 00:31:30
【问题描述】:

我想在地图上添加一些标记,并在上面有一个搜索框和一个图层选择。特别是在标记的某些属性上具有搜索框,在其他属性上具有图层选择。

这是我正在使用的代码。我试过有多个图层,一个用于我想用来选择它们的属性的每个值,然后将它们合并到一个唯一的图层中。但随后搜索不起作用。

Here is what I get 如您所见,图层可以工作,但搜索找不到标记。

    import folium
    from folium import features
    cl_type='Static_clustering'
    m = folium.Map([45.75345246,12.34117475], zoom_start=20)
    var = np.random.choice([-1,1],size=2)*np.clip(np.abs(np.random.normal(0.0005,scale = 0.0001,size=2)),10**(-6),10**(-4))
    points = {'type': 'FeatureCollection',
     'features': [{'type': 'Feature',
       'properties': {'Codice': 500732, 'Categoria': 'D1', 'Cluster': 3},
       'geometry': {'type': 'Point', 'coordinates': [12.34117475, 45.75345246]},
       'id': '0'},
      {'type': 'Feature',
       'properties': {'Codice': 500735, 'Categoria': 'A2', 'Cluster': 2},
       'geometry': {'type': 'Point', 'coordinates': [12.34127475, 45.75345246]},
       'id': '1'}]}
       
    gj = folium.GeoJson(points)
    feature_group_cl = {cl:folium.FeatureGroup('cluster '+str(cl)) for cl in [2,3]}
    tot_layer = folium.FeatureGroup('all')
    
    for feature in gj.data['features']:
        if feature['geometry']['type'] == 'Point':
            marker = folium.Marker(location=list(reversed(feature['geometry']['coordinates'])),
                icon=folium.Icon(color='red'), Codice =feature['properties']['Codice'],
                popup='Hello'
            )
            #marker.add_to(m)
            marker.add_to(feature_group_cl[feature['properties']['Cluster']])
            
    for feat_group in feature_group_cl.values():
        feat_group.add_to(tot_layer)
        feat_group.add_to(m)
    tot_layer.add_to(m)
    folium.LayerControl().add_to(m)
    
    Search(
        layer=tot_layer,
        search_label="Codice",
    ).add_to(m)
    
    
    m

【问题讨论】:

    标签: python layer folium


    【解决方案1】:

    您可以将搜索框连接到 folium GeoJson 对象而不是图层对象。为了只有一次地图上的点,您可以使用参数show=False 将geojson 数据放入另一个folium.FeatureGroup。您可以像这样修改代码的结尾:

    geo_layer = folium.FeatureGroup('geojson', show=False)
    tot_layer.add_to(m)
    geo_layer.add_to(m)
    gj.add_to(geo_layer)
    Search(
        layer=gj,
        search_label="Codice",
    ).add_to(m)
    folium.LayerControl().add_to(m)
    

    【讨论】:

    • 感谢您的解决方案!虽然通过这种方式我看到我会将点加倍,所以地图变得非常沉重......因为搜索将在 gj 点上,并且相同的点也会出现在 tot_layer 中,以便我可以进行过滤在图层上。有没有办法只在点的副本上进行搜索和分层?
    • 非常感谢!有没有办法在标记的属性上而不是在整个标记集上指定不同的图层控件......好像它更像是一个过滤器而不是一个图层控件?
    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2014-09-08
    • 1970-01-01
    相关资源
    最近更新 更多