【问题标题】:Adding a unique feature id to GeoJSON in QGIS or Python在 QGIS 或 Python 中向 GeoJSON 添加唯一的特征 ID
【发布时间】:2016-03-22 17:02:24
【问题描述】:

根据 GeoJSON 格式规范

“如果一个特征有一个常用的标识符,该标识符应该作为特征对象的成员包含在名称“id”中。”

我的问题是如何将它添加到我的 GeoJSON?

如果我将它创建为属性,然后将其保存为 QGIS 中的 GeoJSON,它最终会出现在属性中,而不是特征中。

这就是我想做的:

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name":"urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "id":"1", "properties": { "Namn":.................

这是 QGIS 产生的:

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "id": 1, "Name".................. 

我也试过 PyGeoj https://github.com/karimbahgat/PyGeoj。这有一个添加唯一 id 的功能,但它也在属性下添加它。

如果我打开 GeoJSON 并手动写入它,那么它可以工作,但我不想对我的所有层都这样做,其中一些包含许多特性。

【问题讨论】:

    标签: python geojson qgis


    【解决方案1】:

    我想我会在这里写下我是如何解决它的,以防其他人遇到同样的问题。

    一个简单的python脚本:

    #create empty file to be writen to
    file = open("kommun_id.geojson", "w")
    count = 0
    
    #read original file
    with open('kommun.geojson', 'r')as myfile:
        for line in myfile:
    
           #lines that don't need to be edited with an 'id'
           if not line.startswith('{ "type": '):
                file.write(line)
           else:
                #lines that need to be edited
                count = count +1
                idNr = str(count)
                file.write(line[0:20] + '"id":'+ '"'+ idNr + '",' +line[21:])
    
    file.close()
    

    它可能不适用于所有 geoGJSON,但它适用于我使用 QGIS 创建的那些

    【讨论】:

      【解决方案2】:

      您可以通过在 ogr2ogr 中使用 preserve_fid 标志来解决此问题。您将“转换” QGIS 转储的错误 GeoJSON 为您想要的格式,并暴露 id 字段。

      ogr2ogr -f GeoJSON fixed.geojson qgis.geojson -preserve_fid
      

      这里 qgis.geojson 是 QGIS 创建和修复的。geojson 是带有暴露 id 字段的新版本。

      【讨论】:

      • 这会创建一个增量 id,但不会从 geojson 本身的“id”属性中获取,如果我希望从属性中获取外部 id 字段怎么办?
      【解决方案3】:

      这是因为 ogr2ogr 不知道将哪个 QGIS 字段用作 geoJSON ID。

      根据QGIS issue(谢谢@gioman),解决方法是在导出时手动指定自定义选项中的 ID 字段 --> 图层设置:

      如果您需要对已创建的 geoJSON 执行此操作,可以使用独立的 ogr2ogr 命令行程序:

      ogr2ogr output.geojson input.geojson -lco id_field=id 
      

      如果您需要从多个文件中的属性转换 ID,可以在 Bash for 循环中使用命令行工具 jq

      for file in *.geojson; do jq '(.features[] | select(.properties.id != null)) |= (.id = .properties.id)' $file > "$file"_tmp; mv "$file"_tmp $file;done
      

      【讨论】:

      • 这可行,但是请注意,它需要至少为2.3GDAL 版本,如GDAL GeoJSON driver docs 中所示。我无法找到QGIS 达到GDAL 2.3 的确切版本,但它肯定不在3.2 Bonn 中,它肯定在当前3.22 Białowieża
      【解决方案4】:

      我遇到了同样的问题,我用这个小 Python 脚本进行了管理。

       import json
      
       with open('georef-spain-municipio.geojson') as json_file:
            data = json.load(json_file)
            i = 0
            for p in data['features']:
                 i+=1
                 p['id'] = i
      
       with open('georef-spain-municipio_id.geojson', 'w') as outfile:
       json.dump(data, outfile)
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2016-10-10
        • 2011-06-01
        • 2021-08-23
        • 1970-01-01
        • 1970-01-01
        • 2019-02-26
        • 1970-01-01
        • 2022-08-17
        • 1970-01-01
        相关资源
        最近更新 更多