Skip to content

Instantly share code, notes, and snippets.

@marsam
Last active December 27, 2015 12:09
Show Gist options
  • Select an option

  • Save marsam/7323690 to your computer and use it in GitHub Desktop.

Select an option

Save marsam/7323690 to your computer and use it in GitHub Desktop.
.PHONY: clean
all:
python munlima.py --pip --museos
clean-all:
-rm -f *.csv *.geojson
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import csv
import json
from collections import namedtuple
try:
from urllib.request import urlretrieve
except ImportError:
from urllib import urlretrieve
Dataset = namedtuple('Dataset', ['filename', 'geojson', 'url'])
def museos_geojson(dataset):
if not os.path.exists(dataset.filename):
urlretrieve(dataset.url, dataset.filename)
with open(dataset.geojson, 'w') as geojsonfile:
geojson = {
'type': 'FeatureCollection',
'features': [],
}
with open(dataset.filename) as csvfile:
for row in csv.DictReader(csvfile):
coordinates = [row['LONGITUD'], row['LATITUD']]
if not all(coordinates):
continue
feature = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': coordinates,
},
'properties': {
'marker-symbol': 'museum', # maki-icon
'area': row['AREA'],
'costo': row['COSTO'],
'nombre': row['NOMBRE_DEL_MUSEO'],
'webpage': row['PAGINA_WEB'],
'horario': row['HORARIO_ATENCION'],
'telefono': row['TELEFONO'],
'distrito': row['DISTRITO'],
'direccion': row['DIRECCION'],
'categoria': row['CATEGORIA_DEL_MUSEO'],
},
}
geojson['features'].append(feature)
json.dump(geojson, geojsonfile)
def pip_geojson(dataset):
"""Proyectos de Inversión Pública"""
def marker_color(estado):
estado = estado.lower()
if estado == 'formulacion':
return '#ffb879'
elif estado == 'viable':
return '#60b9ce'
elif estado == 'inversion':
return '#67e667'
else:
return '#f8f8ff'
if not os.path.exists(dataset.filename):
urlretrieve(dataset.url, dataset.filename)
with open(dataset.geojson, 'w') as geojsonfile:
geojson = {
'type': 'FeatureCollection',
'features': [],
}
with open(dataset.filename) as csvfile:
for row in csv.DictReader(csvfile):
coordinates = [row['LONGITUD'], row['LATITUD']]
if not all(coordinates):
continue
feature = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': coordinates,
},
'properties': {
# 'marker-size': 'small',
'marker-color': marker_color(row['ESTADO']),
'pia': row['PIA'],
'pim': row['PIM'],
'siaf': row['SIAF'],
'snip': row['SNIP'],
'estado': row['ESTADO'].title(),
'distrito': row['DISTRITO'].title(),
'proyecto': row['PROYECTO'].strip().title(),
},
}
geojson['features'].append(feature)
json.dump(geojson, geojsonfile)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Convert munlima datasets to geojson. http://lima.datosabiertos.pe/') # nopep8
parser.add_argument('-p', '--pip', dest='pip', action='store_true', help='Proyectos de Inversión Pública') # nopep8
parser.add_argument('-m', '--museos', dest='museos', action='store_true', help='Museos Lima') # nopep8
args = parser.parse_args()
# NOTE: Las uris de los datasets cambian regularmente.
if args.museos:
dataset = Dataset('museos.csv', 'museos.geojson', 'http://lima.datosabiertos.pe/datastreams/79487-museos-de-lima.csv') # nopep8
museos_geojson(dataset)
if args.pip:
datasets = [
Dataset('pip_2011.csv', 'pip_2011.geojson', 'http://lima.datosabiertos.pe/datastreams/76593-proyectos-de-inversion-publica-mml-2011.csv'), # nopep8
Dataset('pip_2012.csv', 'pip_2012.geojson', 'http://lima.datosabiertos.pe/datastreams/76599-proyectos-de-inversion-publica-mml-2012.csv'), # nopep8
Dataset('pip_2013.csv', 'pip_2013.geojson', 'http://lima.datosabiertos.pe/datastreams/76833-proyectos-de-inversion-publica-mml-2013.csv'), # nopep8
]
for dataset in datasets:
pip_geojson(dataset)
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment