问题描述
我有一个 GEOJSON 文件,用于在 ArcGIS 中创建要素类。我的 JSON 缺少一些属性,我注意到 ArcGIS 的 JSONToFeatures 工具添加了相邻对象的缺失属性。
例如,我的原始 json 看起来像这样(注意第二个功能缺少 'site_id' 属性):
{"type": "Feature","geometry":
{"type": "polygon","coordinates": [[[-xxx.xxxx,xx.xxxxxxx],....]]]},"properties":
{"id": "0","address": "xx xxxxx","site_id": "111111111"}},{"type": "Feature","properties":
{"id": "1","address": "xx xxxxx"}},
使用 Arc JSONToFeatures 工具处理此 json 会生成一个如下所示的表:
对象 ID | ID | 地址 | site_id |
---|---|---|---|
0 | xx | xxx | 1111111 |
1 | xx | xxx | 1111111 |
注意:第一条记录中的 site_id 被添加到第二条记录中。
在创建要素类之前,我正在使用 Python 的请求和 json 模块来下载和准备 JSON 文件。 JSON 有 100,000 个对象,这是一个相对常规的过程,而不是一次性任务。
我的问题是:有没有办法在将 JSON 转换为要素类之前准备它以避免添加不正确的属性?或者,有没有办法让 Arc 用这些缺失的属性来表现自己?
解决方法
我找到了使用 JSON 模块的 setdefault 函数的解决方案。
url_ = r"https://...."
with urllib.request.urlopen(url_) as url:
data = json.loads(url.read().decode('utf-8','replace'))
for items in data['features']:
if 'ID' not in items['properties']:
del items
else:
items['properties'].setdefault("site_id",'000000000')
with open(r'F:\JSON.json','w') as outfile:
json.dump(data,outfile)