【发布时间】:2021-05-27 16:10:03
【问题描述】:
为了使用tippicanoe 创建Tilesets,我从postgis 检索空间数据并将其写入带有geopandas 的.geojson 文件:
geodata = gpd.read_postgis("test", engine1, geom_col='geom')```
geodata.to_file("test.geojson", driver='GeoJSON')
我的问题是:是否有一种内置方法或方便的方法可以将 featureID 添加到特征集合中的所有特征?我需要如下所示的输出,将 id 设置为功能级别,而不是属性。
{
"type": "FeatureCollection",
"features": [
{
"id": "1",
"type": "Feature",
"properties": {
"atco_code": "300000492FZ",
},
"geometry": {
"type": "Point",
"coordinates": (-1.1372773238238423, 52.346655194010665),
},
},
{
"id": "2",
"type": "Feature",
"properties": {
"atco_code": "0600CR19133",
},
"geometry": {
"type": "Point",
"coordinates": (-2.518177475249135, 53.063122731640604),
},
},
],
}
我当前的工作流程是将文件写入 .geojson,再次读取,注入 id 并再次保存。这很不方便!
with open("test.geojson") as f:
gj = geojson.load(f)
for i in range(0,len(gj["features"])):
gj["features"][i]["id"] = gj["features"][i]["properties"]["id"]
with open("test.geojson", 'w') as outfile:
geojson.dump(gj, outfile)
提前致谢!
【问题讨论】:
-
对我的回答有什么意见吗?
-
当有人回答你的问题时,你应该怎么做-stackoverflow.com/help/someone-answers
标签: python geospatial geojson geopandas