写入文件时添加功能 ID

Adding a feature id when writing to file

为了使用 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)

提前致谢!

您可以使用 geodata.__geo_interface__ 获取 python 个特征集。 document

示例地理数据框:

        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...

从地理数据框生成的结果:

{'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)}