geojson - 使用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')
可视化是这样的:
所有属性信息都被清除了。有什么办法可以保留这些房产信息吗?谢谢。
解决方案
提供的 GeoJSON ( ) 包含规范output
中定义的样式属性,传单不支持simplestyle
L.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
结果
推荐阅读
- php - CakePHP,如何用实体更新实体
- javascript - 将 Javascript 变量值传递给 php 变量
- reactjs - Firebase/Firestore/React - 是什么阻止用户将一百万个字段添加到他们自己的用户文档中?
- azure - 获取 CloudBlob.DownloadToFileParallelAsync 的进度
- r - 分组没有额外的包
- c++ - 使用 g++ 编译和链接 C++ 程序,出现“未定义引用”错误
- vue.js - 在 Vue CLI 中构建时如何向 index.html 添加 HTML 注释?
- c# - 从 JWT 验证 User.Identity
- sql - PostgreSQL:仅当表存在时才从表中选择
- python - 从 unittest 中调用 subprocess.Popen 失败,但从 __main__ 调用时有效