Skip to content

Instantly share code, notes, and snippets.

@turicas
Last active October 1, 2015 19:17
Show Gist options
  • Save turicas/4cf2e2f8df77732feeb8 to your computer and use it in GitHub Desktop.
Save turicas/4cf2e2f8df77732feeb8 to your computer and use it in GitHub Desktop.
rows and geonames
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.
# coding: utf-8
# pip install rows
import json
from collections import OrderedDict
from datetime import date, datetime
from decimal import Decimal
import rows
from rows import fields
fields = OrderedDict([('geonameid', fields.IntegerField),
('name', fields.TextField),
('asciiname', fields.TextField),
('alternatenames', fields.TextField),
('latitude', fields.DecimalField),
('longitude', fields.DecimalField),
('feature_class', fields.TextField),
('feature_code', fields.TextField),
('country_code', fields.TextField),
('cc2', fields.TextField),
('admin1_code', fields.TextField),
('admin2_code', fields.TextField),
('admin3_code', fields.TextField),
('admin4_code', fields.TextField),
('population', fields.IntegerField),
('elevation', fields.IntegerField),
('dem', fields.IntegerField),
('timezone', fields.TextField),
('modification_date', fields.DateField)])
def fmt(value):
if isinstance(value, (Decimal, date, datetime)):
return str(value)
else:
return value
def create_geojson(entries, filename, latitude_key='latitude',
longitude_key='longitude'):
features = []
for entry in entries:
if '__dict__' in dir(entry):
entry = entry.__dict__
properties = {key: fmt(value)
for key, value in entry.items()}
lat, lon = properties[latitude_key], properties[longitude_key]
del properties[latitude_key]
del properties[longitude_key]
feature = {'type': 'Feature',
'geometry': {'coordinates': [lon, lat], 'type': 'Point', },
'properties': properties, }
features.append(feature)
geojson = {'type': 'FeatureCollection', 'features': features, }
json_string = json.dumps(geojson, indent=2).replace(', \n', ',\n')
with open(filename, 'w') as fobj:
fobj.write(json_string)
def populated_places():
# download EC.txt from: 'http://download.geonames.org/export/dump/EC.zip'
equador = rows.import_from_csv('EC.txt', delimiter='\t', fields=fields)
create_geojson([place for place in equador if place.population > 0],
'equador.geojson')
def weather():
# get JSON from: 'http://api.geonames.org/weatherJSON?north=1.28626&south=-4.38181&east=-76.17776&west=-90.96654&username=demo&maxRows=100'
weather = json.load(open('weather-equador.json'))
create_geojson(weather['weatherObservations'],
'equador-weather.geojson',
latitude_key='lat',
longitude_key='lng')
if __name__ == '__main__':
populated_places()
weather()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment