如何从Python行字符串列表创建GeoJSON
我有一个python列表,如下所示:如何从Python行字符串列表创建GeoJSON,python,geojson,Python,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.0
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
需要每个项目都是一种功能
类型,它比您当前的路线具有更高的可用性。最简单的解决方案是使用列表理解将每条路线映射到功能
模式中
def路由到功能(idx,路由):
返回{
“类型”:“特征”,
“几何”:路线,
“属性”:{
'name':f'Route#{idx}'
}
}
可以这样使用
geojson.FeatureCollection([
路由到路由功能(i,路由)
对我来说,路线
在枚举中(路由)
])
完美。非常感谢你!