【问题标题】:Show different pop-ups for different polygons in a GeoJSON [Folium] [Python] [Map]在 GeoJSON [Folium] [Python] [Map] 中为不同的多边形显示不同的弹出窗口
【发布时间】:2019-07-02 21:40:56
【问题描述】:

我正在使用 folium 来可视化城市中的区域。

我的 GeoJSON 是一个具有多个多边形作为特征的 FeatureCollection。我希望能够为文件中的不同多边形添加不同的弹出窗口。这个想法是在 GEOJSON 文件中显示不同多边形的名称。

我能够向完整的 geoJSON 添加一个弹出窗口。但是,我希望能够为不同的多边形添加不同的弹出窗口(本质上是特征的名称)。

folium.GeoJson(gurgaon_subzone,name='geojson').add_child(folium.Popup("Gurgaon")).add_to(m)

【问题讨论】:

标签: attributes popup geojson folium


【解决方案1】:

有一个解决方法。您需要遍历每个 geoJson 功能并为每个功能创建一个新的 geojson。然后,为每个 geoJson 功能添加一个弹出窗口。然后将所有特征组合在一个层中。在我的代码中,完整的 geoJson 是 data_geojson_dict

layer_geom = folium.FeatureGroup(name='layer',control=False)

for i in range(len(data_geojson_dict["features"])):
    temp_geojson = {"features":[data_geojson_dict["features"][i]],"type":"FeatureCollection"}
    temp_geojson_layer = folium.GeoJson(temp_geojson,
                   highlight_function=lambda x: {'weight':3, 'color':'black'},
                    control=False,
                    style_function=lambda feature: {
                   'color': 'black',
                   'weight': 1},
                    tooltip=folium.features.GeoJsonTooltip(fields=list_tooltip_vars,
                                        aliases=[x.capitalize()+":" for x in list_tooltip_vars], 
                                          labels=True, 
                                          sticky=False))
    folium.Popup(temp_geojson["features"][0]["properties"]["productor"]).add_to(temp_geojson_layer)
    temp_geojson_layer.add_to(layer_geom)

layer_geom.add_to(m)
folium.LayerControl(autoZIndex=False, collapsed=True).add_to(m)

【讨论】:

    【解决方案2】:

    以@David Olmo Pérez 的回答为基础,这对我来说似乎更直观。

    # Create feature group to add to folium.Map object
    layer = folium.FeatureGroup(name='your layer name', show=False)
    
    # load GEOJSON, but don't add it to anything
    temp_geojson = folium.GeoJson('path/to/your/file.geojson')
    
    # iterate over GEOJSON, style individual features, and add them to FeatureGroup
    for feature in temp_geojson.data['features']:
        # GEOJSON layer consisting of a single feature
        temp_layer = folium.GeoJson(feature,
                                    style_function={
                                        'color': '#000000',
                                        'opacity': 0.7,
                                    })
        # lambda to add HTML
        foo = lambda name, source: f"""
            <iframe id="popupIFrame"
                title="{name}"
                width="600"
                height="500"
                align="center"
                src="{source}">
            </iframe>
            """
        # create Popup and add it to our lone feature
        # this example embeds a .png
        folium.Popup(
            html=foo('name of your IFrame',
                     f'path/to/embed/file_{feature["properties"]["some_attribute"]}.png')
        ).add_to(temp_layer)
    
        # consolidate individual features back into the main layer
        temp_layer.add_to(layer)
    
    # add main layer to folium.Map object
    layer.add_to(m)
    

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 2020-07-05
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      相关资源
      最近更新 更多