Last active
August 29, 2015 14:02
-
-
Save bgreenlee/55475cdc7a601f818628 to your computer and use it in GitHub Desktop.
Convert a location name to tab-separated place name, lat, lon, and time zone. Adapted from http://blog.pamelafox.org/2012/04/converting-addresses-to-timezones-in.html
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
#!/usr/bin/env python | |
# Convert a location name to tab-separated place name, lat, lon, and time zone. | |
# Adapted from http://blog.pamelafox.org/2012/04/converting-addresses-to-timezones-in.html | |
# | |
# Installation: | |
# 1. Get a free Geonames account: http://www.geonames.org/login | |
# Note: after you confirm your account, you'll need to enable it to use the | |
# free web service at http://www.geonames.org/manageaccount | |
# 2. Install/download geopy and geonames libraries: | |
# easy_install geopy | |
# curl -O https://gist.githubusercontent.com/pamelafox/2288222/raw/geonames.py | |
# 3. Edit this file and put your Geonames username here: | |
GEONAMES_USERNAME = 'yourusername' | |
# | |
# Usage: | |
# echo "Seattle" | python loc2tz.py | |
# => Seattle, WA, USA 47.6062095 -122.3320708 America/Los_Angeles | |
# or: | |
# cat list_of_places.txt | python loc2tz.py | |
import fileinput | |
from geopy import geocoders | |
import geonames | |
import logging | |
geocoder_client = geocoders.GoogleV3() | |
geonames_client = geonames.GeonamesClient(GEONAMES_USERNAME) | |
for line in fileinput.input(): | |
result = geocoder_client.geocode(line) | |
if result == None: | |
logging.error("Could not geocode: %s", line) | |
continue | |
place, (lat, lon) = result | |
try: | |
geonames_result = geonames_client.find_timezone({'lat': lat, 'lng': lon}) | |
timezone = geonames_result['timezoneId'] | |
except geonames.GeonamesError, err: | |
logging.error('Could not fetch timezone for (%s, %s): %s' % (str(lat), str(lon), err)) | |
continue | |
print "\t".join([place, str(lat), str(lon), timezone]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment