I have just downloaded several hundred identically formatted geojson files. The problem is none of them have any attributes in their file structure just coordinates. I want to add the following lines at the top of every file using either python2 or python3:
content = {'"type": "FeatureCollection",'
'"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },'
'"features": ['
'{ "type": "Feature", "properties": { "id": "%s" }, "geometry":' %i
}
Where i is just the filename. I have tried a variety of things but nothing seems to get all the way there. There always seems to be an error with the string, the combination or the output. I would also like to add a ]}
at the end but that is fairly straightforward append. Any help would be awesome.
import json
with open('TestLayer.geojson', 'w') as geojson_file:
data = json.load(geojson_file)
# data is now a dict,
content = {'"type": "FeatureCollection",'
'"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },'
'"features": ['
'{ "type": "Feature", "properties": { "id": "%s" }, "geometry":'
}
data = content+data
json.dump(data, geojson_file)
Best Answer
In your "code example" you seem to mix string and dict.
Though I would use bash (preferably
sed
), you can perfectly use pythonsjson
module to edit geojson files.