python - 如何从 Python 的 LineString 列表创建 GeoJSON
问题描述
我有一个看起来像这样的 python 列表:
type(route)
>>>list
print(route)
[{'type': 'LineString',
'coordinates': [[-94.586472, 39.098705],
[-64.586487, 39.098716],
[-64.585969, 39.094146],
[-64.586037, 39.093936],
[-64.586037, 39.093936],
[-64.586046, 39.093933]]},
{'type': 'LineString',
'coordinates': [[-94.581459, 39.093506],
[-64.581451, 39.09351],
[-64.581444, 39.093506],
[-64.581459, 39.093433],
[-64.581726, 39.093418],
[-64.588631, 39.087582]]},
{'type': 'LineString',
'coordinates': [[-94.584312, 39.042758],
[-64.584312, 39.042758],
[-64.583225, 39.099256],
[-64.584328, 39.09932]]}]
如何将其转换为有效的 GeoJSON 文件?我试过test = FeatureCollection(features=route)
了,但是当我后来转储它时创建了一个无效文件。
解决方案
看起来FeatureCollection
需要每个项目都是一种类型,Feature
其模式与您当前的路线不同。最简单的解决方案是使用列表推导将每条路由映射到Feature
架构中。
def route_to_feature(idx, route):
return {
'type': 'Feature',
'geometry': route,
'properties': {
'name': f'Route #{idx}'
}
}
可以这样使用。
geojson.FeatureCollection([
route_to_feature(i, route)
for i, route
in enumerate(routes)
])
推荐阅读
- paypal - “帐户可选”功能是否可用于 PayPal 的订阅?
- python - 解析用逗号分隔的json数据?
- excel - 如何使用 VBA 循环目标查找列
- python - 如何获得图表中 2 个 DataFrame 列之间的交集
- excel - VBA 访问数据结构
- sql - SQL Server - 不存在数据的完整结果
- android - 如何在 Android 中使用折线显示轨道路线
- node.js - nodemon - 由于更改而重新启动......永远不会重新启动
- scala - 如何使用 JohnSnowLabs NLP 拼写校正模块 NorvigSweetingModel?
- java - 为什么 JIT 在启动时会编译一些方法?