[英]Append element to json dict (geojson) using Python
I would like to add style to my geojson through Python. 我想通过Python向我的geojson添加样式。 The current features currently do not have any style elements.
当前的要素当前没有任何样式元素。 I want to append style and then fill.
我想附加样式然后填充。 However, when I do, nothing is added to the file.
但是,当我这样做时,什么都没有添加到文件中。 It is the same as before
和以前一样
import json
with open('test.json') as f:
data = json.load(f)
for feature in data['features']:
feature.append("style")
feature["style"].append({"fill":color})
Sample GeoJson 样例GeoJson
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "STATEFP": "17", "COUNTYFP": "019", "TRACTCE": "005401", "BLKGRPCE": "2", "GEOID": "170190054012", "NAMELSAD": "Block Group 2", "MTFCC": "G5030", "FUNCSTAT": "S", "ALAND": 574246.000000, "AWATER": 4116.000000, "INTPTLAT": "+40.1238204", "INTPTLON": "-088.2038105", "GISJOIN": "G17001900054012", "STUSPS": "IL", "SHAPE_AREA": 578361.706954, "SHAPE_LEN": 3489.996273, "census_block_income_YEAR": "2009-2013", "census_block_income_STATE": "Illinois", "census_block_income_STATEA": 17, "census_block_income_COUNTY": "Champaign County"}}]}
I'm trying to get the end results to be: 我试图使最终结果是:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "STATEFP": "17", "COUNTYFP": "019", "TRACTCE": "005401", "BLKGRPCE": "2", "GEOID": "170190054012", "NAMELSAD": "Block Group 2", "MTFCC": "G5030", "FUNCSTAT": "S", "ALAND": 574246.000000, "AWATER": 4116.000000, "INTPTLAT": "+40.1238204", "INTPTLON": "-088.2038105", "GISJOIN": "G17001900054012", "STUSPS": "IL", "SHAPE_AREA": 578361.706954, "SHAPE_LEN": 3489.996273, "census_block_income_YEAR": "2009-2013", "census_block_income_STATE": "Illinois", "census_block_income_STATEA": 17, "census_block_income_COUNTY": "Champaign County"},"style"{fill:"red"}}]}
You are working with list of dictionaries here, dictionary hasn't method append
, you can create new key like here: 您在这里使用词典列表,字典没有方法
append
,您可以在此处创建新键:
for feature in data['features']:
feature["style"] = {"fill":color}
Seems that you need rewrite file with JSON: 似乎您需要使用JSON重写文件:
with open('test.json', 'w') as f:
json.dump(data, f)
When you type 当您键入
for feature in data['features']:
every feature
will be an item of the list that is data['features']
. 每个
feature
都将是data['features']
列表中的一项。 Each item there is a dictionary, so you are calling the wrong method ( append
is a method of lists). 每个项目都有一个字典,因此您在调用错误的方法(
append
是列表的方法)。
You could write 你可以写
for feature in data['features']:
feature.update({"style": {"fill": "red"}})
Finally, if you want the file from which you got the initial json structure to be altered, make sure to write the now updated data structure back to a file: 最后,如果要更改用于更改初始json结构的文件,请确保将现在更新的数据结构写回到文件中:
with open('output2.json', 'w') as f:
json.dump(data, f)
There is no append
method in a dictionary. 词典中没有
append
方法。 One should use update
. 一个应该使用
update
。
import pprint as pp
for feature in data['features']:
feature.update({'style':{'fill': 'red'}})
pp.pprint(data)
Output: 输出:
{'crs': {'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'},
'type': 'name'},
'features': [{'properties': {'ALAND': 574246.0,
'AWATER': 4116.0,
'BLKGRPCE': '2',
'COUNTYFP': '019',
'FUNCSTAT': 'S',
'GEOID': '170190054012',
'GISJOIN': 'G17001900054012',
'INTPTLAT': '+40.1238204',
'INTPTLON': '-088.2038105',
'MTFCC': 'G5030',
'NAMELSAD': 'Block Group 2',
'SHAPE_AREA': 578361.706954,
'SHAPE_LEN': 3489.996273,
'STATEFP': '17',
'STUSPS': 'IL',
'TRACTCE': '005401',
'census_block_income_COUNTY': 'Champaign County',
'census_block_income_STATE': 'Illinois',
'census_block_income_STATEA': 17,
'census_block_income_YEAR': '2009-2013'},
'style': {'fill': 'red'},
'type': 'Feature'}],
'type': 'FeatureCollection'}
You never write your changes back to the file. 您永远不会将所做的更改写回到文件中。 Add the following to the end of your code:
在代码末尾添加以下内容:
with open('test.json','w') as f:
json.dump(data, f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.