Append element to json dict (geojson) using Python -
i add style geojson through python. current features not have style elements. want append style , fill. however, when do, nothing added file. same before
import json open('test.json') f: data = json.load(f) feature in data['features']: feature.append("style") feature["style"].append({"fill":color})
sample 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 end results 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"}}]}
when type
for feature in data['features']:
every feature
item of list data['features']
. each item there dictionary, calling wrong method (append
method of lists).
you write
for feature in data['features']: feature.update({"style": {"fill": "red"}})
finally, if want file got initial json structure altered, make sure write updated data structure file:
with open('output2.json', 'w') f: json.dump(data, f)
Comments
Post a Comment