我有一個看起來像這樣的 python 列表:type(route)>>>listprint(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]]}]如何將其轉(zhuǎn)換為有效的 GeoJSON 文件?我試過了test = FeatureCollection(features=route),但是當我后來轉(zhuǎn)儲它時創(chuàng)建了一個無效的文件。
1 回答

眼眸繁星
TA貢獻1873條經(jīng)驗 獲得超9個贊
看起來FeatureCollection需要每個項目的類型Feature都與您當前的路由具有不同的架構(gòu)。最簡單的解決方案是使用列表理解將每個路由映射到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)
])
添加回答
舉報
0/150
提交
取消