首页 > 解决方案 > 使用folium可视化时丢失geojson的特征属性信息

问题描述

目前我已经制作了一个 geojson 文件(称为output): {"type": "FeatureCollection", "features": [ {"type": "Feature", "geometry": {"type": "Point", "coordinates": [103.815381, 1.279109]}, "properties": {"temperature": 24, "marker-symbol": "park", "marker-color": "#AF4646"}}, {"type": "Feature", "geometry": {"type": "MultiLineString", "coordinates": [[[103.809297, 1.294906], [103.799445, 1.283906], [103.815381, 1.294906]]]}, "properties": {"temperature": 24, "stroke": "#AF4646"}}]}

它包含一个多行字符串类型和一个点类型。预期的输出应该是这样的(使用 geojson.io 可视化),其中所有属性(例如字符串和标记的颜色,标记的森林图标)都保留在此处输入图像描述

我的目标是生成此地图的 html 或图像文件(最佳选择)。所以我转向叶。但是,当我使用命令时:

m = folium.Map(location=[1.2791,103.8154], zoom_start=12) folium.GeoJson(output, name='test').add_to(m) m.save('map.html') 可视化是这样的: 在此处输入图像描述 所有属性信息都被清除了。有什么办法可以保留这些房产信息吗?谢谢。

标签: geojsongeopandasfolium

解决方案


提供的 GeoJSON ( ) 包含规范output中定义的样式属性,传单不支持simplestyleL.geoJSON

leaflet-simplestyle可以使用扩展以支持 simplestyle 规范的 插件L.geoJSON,这是一个关于如何在 folium 中使用它的示例

import folium
from folium.elements import JSCSSMixin
from folium.map import Layer
from jinja2 import Template


class StyledGeoJson(JSCSSMixin, Layer):
    """
    Creates a GeoJson which supports.

    """
    _template = Template(u"""
        {% macro script(this, kwargs) %}

            var {{ this.get_name() }} = L.geoJson({{ this.data }},
                {
                    useSimpleStyle: true,
                    useMakiMarkers: true
                }
            ).addTo({{ this._parent.get_name() }});
        {% endmacro %}
        """)  

    default_js = [
        ('leaflet-simplestyle', 'https://unpkg.com/leaflet-simplestyle'),
    ]

    def __init__(self, data,name=None, overlay=True, control=True, show=True):
        super(StyledGeoJson, self).__init__(name=name, overlay=overlay,
                                       control=control, show=show)
        self._name = 'StyledGeoJson'
        self.data = data

用法

output = {"type": "FeatureCollection", "features": [ {"type": "Feature",  "geometry": {"type": "Point", "coordinates": [103.815381, 1.279109]}, "properties": {"temperature": 24, "marker-symbol": "park", "marker-color": "#AF4646"}},  {"type": "Feature",  "geometry":  {"type": "MultiLineString", "coordinates": [[[103.809297, 1.294906], [103.799445, 1.283906], [103.815381, 1.294906]]]}, "properties": {"temperature": 24, "stroke": "#AF4646"}}]}
m = folium.Map(location=[1.2791,103.8154], zoom_start=14)

StyledGeoJson(output).add_to(m)
m

结果

在此处输入图像描述


推荐阅读