写入文件时添加特性id



为了使用tippicanoe创建tile集,我从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)

提前感谢!

您可以使用geodata.__geo_interface__获取python特性集合。文档

样本geodataframe:

name  id                                               geom
0  polygon A   1  MULTIPOLYGON (((36.00000 11.00000, 36.00000 12...
1  polygon B   2  MULTIPOLYGON (((36.50000 11.50000, 37.50000 11...
2  polygon C   3  MULTIPOLYGON (((36.61799 10.80580, 36.61570 11...

从geodataframe生成的结果:

{'type': 'FeatureCollection',
'features': [{'id': '0',
'type': 'Feature',
'properties': {'id': 1, 'name': 'polygon A'},
'geometry': {'type': 'MultiPolygon',
'coordinates': [(((36.0, 11.0),
(36.0, 12.0),
(37.0, 12.0),
(37.0, 11.0),
(36.0, 11.0)),)]},
'bbox': (36.0, 11.0, 37.0, 12.0)},
{'id': '1',
'type': 'Feature',
'properties': {'id': 2, 'name': 'polygon B'},
'geometry': {'type': 'MultiPolygon',
'coordinates': [(((36.5, 11.5),
(37.5, 11.5),
(37.5, 11.0),
(36.5, 11.0),
(36.5, 11.5)),)]},
'bbox': (36.5, 11.0, 37.5, 11.5)},
{'id': '2',
'type': 'Feature',
'properties': {'id': 3, 'name': 'polygon C'},
'geometry': {'type': 'MultiPolygon',
'coordinates': [(((36.61799, 10.8058),
(36.6157, 11.19321),
(36.86327, 11.29637),
(37.34925, 10.91813),
(37.0054, 10.71182),
(36.61799, 10.8058)),)]},
'bbox': (36.6157, 10.71182, 37.34925, 11.29637)}],
'bbox': (36.0, 10.71182, 37.5, 12.0)}

最新更新