Skip to content

Instantly share code, notes, and snippets.

@Scarysize
Created April 5, 2017 09:56
Show Gist options
  • Save Scarysize/0e3c5124a6b543ce6b2bcdaf3c0dea19 to your computer and use it in GitHub Desktop.
Save Scarysize/0e3c5124a6b543ce6b2bcdaf3c0dea19 to your computer and use it in GitHub Desktop.
Handle (Geo-)JSON with python
import json
with open('path/to/json/file') as data_file:
# a python dictionary, similar to a JS object
data = json.load(data_file)
# iterate over features, 'features' is a python list
for feature in data['features']:
# access everything via brackets, no dot notation
properties = feature['properties']
#...
# construct a new geoJson
geoJson = {
'type': 'FeatureCollection',
'features': []
}
# write geojson to file
with open('target/file', 'w') as outfile:
json.dump(geoJson, outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment