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
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Point",
"coordinates": [
-78.1207962036133,
-2.29917001724243
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "few clouds",
"observation": "SEMC 272100Z 01009KT 340V040 9999 FEW026 26/18 Q1014",
"dewPoint": "18",
"humidity": 61,
"ICAO": "SEMC",
"windSpeed": "09",
"cloudsCode": "FEW",
"temperature": "26",
"stationName": "Coronel E Carvajal Airport",
"datetime": "2015-09-27 21:00:00",
"windDirection": 10,
"hectoPascAltimeter": 1014
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-76.8675003052,
0.0930560007691
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "few clouds",
"observation": "SENL 272100Z 03004KT 9999 FEW023 32/21 Q1010",
"dewPoint": "21",
"humidity": 52,
"ICAO": "SENL",
"windSpeed": "04",
"cloudsCode": "FEW",
"temperature": "32",
"stationName": "Nueva Loja Airport",
"datetime": "2015-09-27 21:00:00",
"windDirection": 30,
"hectoPascAltimeter": 1010
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-78.488214,
-0.141144
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "scattered clouds",
"observation": "SEQM 272100Z 05008KT 9999 SCT040 26/04 Q1020 NOSIG RMK A3014",
"dewPoint": "4",
"humidity": 24,
"ICAO": "SEQM",
"windSpeed": "08",
"cloudsCode": "SCT",
"temperature": "26",
"stationName": "Mariscal Sucre International Airport",
"datetime": "2015-09-27 21:00:00",
"windDirection": 50,
"hectoPascAltimeter": 1020
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-80.00982284545898,
-3.4438549437345154
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "few clouds",
"observation": "SERO 272100Z 32004KT 250V020 9999 FEW013 BKN040 28/24 Q1008",
"dewPoint": "24",
"humidity": 78,
"ICAO": "SERO",
"windSpeed": "04",
"cloudsCode": "FEW",
"temperature": "28",
"stationName": "Santa Rosa Regional Airport",
"datetime": "2015-09-27 21:00:00",
"windDirection": 320,
"hectoPascAltimeter": 1008
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-77.7,
0.8166666666666667
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "broken clouds",
"observation": "SETU 272100Z 04006KT 9999 BKN023 13/09 Q1030",
"dewPoint": "9",
"humidity": 76,
"ICAO": "SETU",
"windSpeed": "06",
"cloudsCode": "BKN",
"temperature": "13",
"stationName": "Tulcan / El Rosal",
"datetime": "2015-09-27 21:00:00",
"windDirection": 40,
"hectoPascAltimeter": 1030
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-76.95,
-0.45
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "few clouds",
"observation": "SECO 272100Z 09005KT 050V140 9999 FEW030 33/22 Q1009",
"dewPoint": "22",
"humidity": 52,
"ICAO": "SECO",
"windSpeed": "05",
"cloudsCode": "FEW",
"temperature": "33",
"stationName": "COCA/FRANCISCO",
"datetime": "2015-09-27 21:00:00",
"windDirection": 90,
"hectoPascAltimeter": 1009
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-80.96666666666667,
-2.2
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "scattered clouds",
"observation": "SESA 272100Z 22012KT 9999 SCT010 OVC015 25/20 Q1008",
"dewPoint": "20",
"humidity": 73,
"ICAO": "SESA",
"windSpeed": "12",
"cloudsCode": "SCT",
"temperature": "25",
"stationName": "Salinas / General Ulpiano Paez",
"datetime": "2015-09-27 21:00:00",
"windDirection": 220,
"hectoPascAltimeter": 1008
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-80.66666666666667,
-0.95
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "few clouds",
"observation": "SEMT 272100Z 19013KT 9999 FEW023 29/22 Q1007 RMK A2975",
"dewPoint": "22",
"humidity": 66,
"ICAO": "SEMT",
"windSpeed": "13",
"cloudsCode": "FEW",
"temperature": "29",
"stationName": "Manta",
"datetime": "2015-09-27 21:00:00",
"windDirection": 190,
"hectoPascAltimeter": 1007
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-90.26666666666667,
-0.45
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "scattered clouds",
"observation": "SEGS 272100Z 14017KT SCT023 27/20 Q1010 A2984",
"dewPoint": "20",
"humidity": 65,
"ICAO": "SEGS",
"windSpeed": "17",
"cloudsCode": "SCT",
"temperature": "27",
"stationName": "SEYMOUR(EQ-AFB/C",
"datetime": "2015-09-27 21:00:00",
"windDirection": 140,
"hectoPascAltimeter": 1010
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-89.58333333333333,
-0.9
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "scattered clouds",
"observation": "SEST 272100Z 15013KT 9999 SCT020 BKN100 25/20 Q1007 RMK A2976",
"dewPoint": "20",
"humidity": 73,
"ICAO": "SEST",
"windSpeed": "13",
"cloudsCode": "SCT",
"temperature": "25",
"stationName": "San Cristobal Galapagos ",
"datetime": "2015-09-27 21:00:00",
"windDirection": 150,
"hectoPascAltimeter": 1007
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-78.56666666666666,
-1.1833333333333333
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "broken clouds",
"observation": "SEAM 272100Z 16004KT 9999 BKN026 19/13 Q1026",
"dewPoint": "13",
"humidity": 68,
"ICAO": "SEAM",
"windSpeed": "04",
"cloudsCode": "BKN",
"temperature": "19",
"stationName": "Ambato / Chachoan",
"datetime": "2015-09-27 21:00:00",
"windDirection": 160,
"hectoPascAltimeter": 1026
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-80.4,
-3.533333333333333
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "scattered clouds",
"observation": "SPME 272100Z 33006KT 9999 SCT012 27/22 Q1007 RMK BIRD HAZARD RWY 32/14 PP000",
"dewPoint": "22",
"humidity": 74,
"ICAO": "SPME",
"windSpeed": "06",
"cloudsCode": "SCT",
"temperature": "27",
"stationName": "Tumbes",
"datetime": "2015-09-27 21:00:00",
"windDirection": 330,
"hectoPascAltimeter": 1007
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-79.86666666666666,
-2.1333333333333333
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "scattered clouds",
"observation": "SEGU 272100Z 17011KT 9999 SCT026 30/22 Q1006 NOSIG RMK A2971",
"dewPoint": "22",
"humidity": 62,
"ICAO": "SEGU",
"windSpeed": "11",
"cloudsCode": "SCT",
"temperature": "30",
"stationName": "Guayaquil / Simon Bolivar",
"datetime": "2015-09-27 21:00:00",
"windDirection": 170,
"hectoPascAltimeter": 1006
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-78.66666666666667,
-1.6166666666666667
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "broken clouds",
"observation": "SERB 272100Z 05020KT 9999 BKN040 18/10 Q1026",
"dewPoint": "10",
"humidity": 59,
"ICAO": "SERB",
"windSpeed": "20",
"cloudsCode": "BKN",
"temperature": "18",
"stationName": "RIOBAMBA/CHIMBOR",
"datetime": "2015-09-27 21:00:00",
"windDirection": 50,
"hectoPascAltimeter": 1026
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-77.61666666666666,
0.8166666666666667
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "broken clouds",
"observation": "SKIP 272100Z 08009KT 9999 BKN018 13/09 A3042",
"dewPoint": "9",
"humidity": 76,
"ICAO": "SKIP",
"windSpeed": "09",
"cloudsCode": "BKN",
"stationName": "Ipiales / San Luis",
"datetime": "2015-09-27 21:00:00",
"windDirection": 80,
"temperature": "13"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-78.96666666666667,
-2.8666666666666667
]
},
"type": "Feature",
"properties": {
"weatherCondition": "in vicinity: showers ",
"clouds": "broken clouds",
"observation": "SECU 272100Z 03015KT 9999 VCSH BKN040 20/09 Q1024",
"dewPoint": "9",
"humidity": 49,
"ICAO": "SECU",
"windSpeed": "15",
"cloudsCode": "BKN",
"temperature": "20",
"stationName": "Cuenca / Mariscal Lamar",
"weatherConditionCode": "VCSH",
"datetime": "2015-09-27 21:00:00",
"windDirection": 30,
"hectoPascAltimeter": 1024
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-78.61666666666666,
-0.9166666666666666
]
},
"type": "Feature",
"properties": {
"weatherCondition": "n/a",
"clouds": "scattered clouds",
"observation": "SELT 272100Z 15014KT 9999 SCT040 20/07 Q1026",
"dewPoint": "7",
"humidity": 42,
"ICAO": "SELT",
"windSpeed": "14",
"cloudsCode": "SCT",
"temperature": "20",
"stationName": "Latacunga",
"datetime": "2015-09-27 21:00:00",
"windDirection": 150,
"hectoPascAltimeter": 1026
}
}
]
}
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