如何从 Python 行字符串列表创建 GeoJSON
How to Create GeoJSON from Python List of LineStrings
我有一个 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
的类型,它比您当前的路线具有 different schema。最简单的解决方案是使用列表理解将每个路由映射到 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)
])
我有一个 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
的类型,它比您当前的路线具有 different schema。最简单的解决方案是使用列表理解将每个路由映射到 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)
])