【问题标题】:Adding a feature id when writing to file写入文件时添加功能 ID
【发布时间】: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)

提前致谢!

【问题讨论】:

标签: python geospatial geojson geopandas


【解决方案1】:

您可以使用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)}

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2019-09-07
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多