Created
July 1, 2015 00:37
-
-
Save avdata99/97441be804ddd3ca6b39 to your computer and use it in GitHub Desktop.
Escuelas de Cordoba 2013. GeoJson to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# pasar a CSV el recurso de datos en | |
# http://democraciaconcodigos.github.io/election-2013/ | |
# lista de escuelas de cordoba a octubre de 2013 | |
import sys | |
import json | |
import codecs | |
path = None | |
out_file = 'result.csv' | |
for arg in sys.argv: | |
if arg == '-h' or arg == '--help': | |
print "%s USAGE" % sys.argv[0] | |
print " --path=/origin/file.geojson the starting GeoJson file " | |
print " --out=/dest/file.csv destination csv file (result.csv if none)" | |
exit() | |
if arg.split('=')[0] == '--path': path = arg.split('=')[1] | |
if arg.split('=')[0] == '--out': out_file = arg.split('=')[1] | |
if not path: | |
print "Use --path for loading geoJson file" | |
exit(1) | |
# f = codecs.open(path, 'r', encoding='utf8') | |
f = open(path, 'r') | |
js = json.load(f) | |
f.close() | |
# we spect someting like | |
""" | |
{"type": "FeaturesCollection", "features": [ ... ] } | |
""" | |
results = [] | |
for feature in js['features']: | |
f = {} # this feature | |
""" each feature is like (we just can reach one level | |
{"geometry": | |
{"type": "Point", | |
"coordinates": [-64.18965445684096, -31.414637452814794] | |
}, | |
"type": "Feature", | |
"properties": | |
{"establecim": "CENTRO EDUC.NIVEL MEDIO ADULTO", | |
"overall_total": 1658, | |
"fake_id": "1-7", | |
"seccion": "1", | |
"circuito": "00001", | |
"votos": | |
{"217": 67, "512": 156, "191": 91, "514": 68, "003": 389, "9006": 0, "9005": 26, "9004": 17, "9003": 3, "047": 95, "505": 225, "503": 315, "501": 206}, | |
"direccion": "DEAN FUNES 417" | |
} | |
} | |
""" | |
f['escuela'] = feature['properties']['establecim'] | |
f['direccion'] = feature['properties']['direccion'] | |
f['seccion'] = feature['properties']['seccion'] | |
f['circuito'] = feature['properties']['circuito'] | |
f['latitud'] = feature['geometry']['coordinates'][0] | |
f['longitud'] = feature['geometry']['coordinates'][1] | |
if f.get('escuela', None): f['escuela'] = f['escuela'].replace('"', "'") | |
if f.get('direccion', None): f['direccion'] = f['direccion'].replace('"', "'") | |
results.append(f) | |
f = codecs.open(out_file, 'w', encoding='utf8') | |
f.write('Escuela,Direccion,Seccion,Circuito,Latitud,Longitud') | |
for r in results: | |
f.write('\n"%s","%s",%s,%s,%s,%s' % (r['escuela'], r['direccion'], | |
r['seccion'], r['circuito'], r['latitud'], r['longitud'])) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment