简体   繁体   English

在 QGIS 或 Python 中向 GeoJSON 添加唯一的要素 ID

[英]Adding a unique feature id to GeoJSON in QGIS or Python

According to the GeoJSON Format Specification根据 GeoJSON 格式规范

"If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id"." “如果一个特征有一个常用的标识符,那么这个标识符应该作为名称为“id”的特征对象的一个​​成员被包含在内。”

My question is how do I add this to my GeoJSON?我的问题是如何将它添加到我的 GeoJSON 中?

If I create it as an attribute and then save it as GeoJSON in QGIS it ends up in Properties instead of in Features.如果我将其创建为属性,然后在 QGIS 中将其另存为 GeoJSON,则它最终会出现在属性中,而不是在功能中。

This is what I want to do:这就是我想要做的:

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

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

This is what QGIS produces:这就是 QGIS 产生的:

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

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

I have also tried PyGeoj https://github.com/karimbahgat/PyGeoj .我也试过 PyGeoj https://github.com/karimbahgat/PyGeoj This has a function to add a unique id but it also adds it under properties.这具有添加唯一 id 的功能,但它也将其添加到属性下。

If I open the GeoJSON and write it in by hand then it works but I don't want to do this for all of my layers, some of which contain many features.如果我打开 GeoJSON 并手动编写它,那么它就可以工作,但我不想对我的所有图层都这样做,其中一些图层包含许多功能。

Thought I would write here how I have solved it in case anyone else comes across the same problem.我想我会在这里写下我是如何解决它的,以防其他人遇到同样的问题。

A simple python script:一个简单的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()

It might not work for all geoGJSONs but it works for the ones I have created with QGIS它可能不适用于所有 geoGJSON,但适用于我用 QGIS 创建的那些

You can fix this by using the preserve_fid flag in ogr2ogr.您可以通过使用 ogr2ogr 中的preserve_fid标志来解决此问题。 You're going to "convert" the bad GeoJSON that QGIS dumps to the format you want, with the id field exposed.您将把 QGIS 转储的错误 GeoJSON“转换”为您想要的格式,并暴露 id 字段。

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

Here qgis.geojson is the one the QGIS created and fixed.geojson is the new version with the exposed id field.这里 qgis.geojson 是 QGIS 创建的,fixed.geojson 是带有公开 id 字段的新版本。

This is due to ogr2ogr not knowing which QGIS field to use as the geoJSON ID.这是因为 ogr2ogr 不知道将哪个 QGIS 字段用作 geoJSON ID。

Per this QGIS issue (thank you @gioman), a workaround is to manually specify the ID field in Custom Options --> Layer Settings when you do the export:根据这个QGIS 问题(谢谢@gioman),一种解决方法是在导出时在自定义选项 --> 图层设置中手动指定 ID 字段:

在此处输入图片说明

If you need to do this to geoJSONs that have already been created, you can use the standalone ogr2ogr command-line program:如果您需要对已经创建的 geoJSONs 执行此操作,您可以使用独立的ogr2ogr命令行程序:

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

If you need to convert the IDs from properties inside multiple files, you can use the command-line tool jq inside a Bash for-loop:如果需要从多个文件中的属性转换 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

I had the same problem and i managed with this little python script.我遇到了同样的问题,我用这个小 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)

I hope it helps我希望它有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM