Created
November 29, 2013 21:23
-
-
Save trevormunoz/7712187 to your computer and use it in GitHub Desktop.
Quick and dirty geocoding and mapping of KAP Project metadata
This file contains 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
{ | |
"metadata": { | |
"name": "kap-geocoding" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "from csv import DictReader\nfrom geopy import geocoders\nimport geojson\nimport requests\n\n# Use data from KAP Project master spreadsheet\u2014exported as CSV\nkap_metadata = '/Users/libraries/Code/kap-data/KAP.Metadata-master.csv'\n\n# Set up a connection to a geocoding web service \u2014 in this case, Geonames\ngn = geocoders.GeoNames(username='trevormunoz')\n\nplaces = {}\nwith open(kap_metadata, 'rU') as csvfile:\n reader = DictReader(csvfile)\n for row in reader:\n # Skip rows with no geographic information\n if row['Settlement (City)'] is not '':\n places[\"{0}, {1}, {2}, {3}\".format(row['Settlement (City)'], row['Region (State)'], row['Country'], row['Continent'])] = 0\n \n\n#print(\"{0} places found in the data set\".format(len(places.keys())))\nfeature_list = []\n\n# Send each string representing a place to the geocoding service.\nfor p in places.keys():\n try:\n name, coords = gn.geocode(p)\n \n # Need to flip Lat and Long to render correctly as GeoJSON, \n # see: http://gis.stackexchange.com/questions/54065/leaflet-geojson-coordinate-problem\n geoJSON_coords = (coords[1], coords[0])\n \n map_point = geojson.Point(geoJSON_coords)\n map_feature = geojson.Feature(geometry=map_point, properties={\"Name\" : name, \"popupContent\": p})\n feature_list.append(map_feature)\n \n #Insert blank features where geocoding returns None\n except TypeError:\n map_feature = geojson.Feature(geometry=[], properties={\"Name\" : \"null\", \"popupContent\": p})\n feature_list.append(map_feature)\n\n# Dump the GeoJSON and write to a file \ndata_dump = geojson.dumps(geojson.FeatureCollection(feature_list))\nwith open('/Users/libraries/Code/kap-data/KAP.Locations.json', 'w') as outfile:\n outfile.write(data_dump)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "# Check that the generated GeoJSON is valid using GeoJSON Lint\nvalidate_endpoint = 'http://geojsonlint.com/validate'\ntest_request = requests.post(validate_endpoint, data=data_dump)\n\nprint test_request.json()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "{u'status': u'ok'}\n" | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment