Created
February 14, 2011 22:14
-
-
Save rajbot/826686 to your computer and use it in GitHub Desktop.
Get Lat/Long coords from street addresses using Yahoo Query Language
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/python | |
import urllib | |
import json | |
import time | |
address_file = "libraries.txt" | |
f = open(address_file) | |
for line in f: | |
address = line.strip() | |
if ', , ,' == address: | |
continue | |
#print address | |
yql = 'http://query.yahooapis.com/v1/public/yql?q=' + urllib.quote('''select * from geo.places where text="%s"''' % (address)) + '&format=json&callback=' | |
urlh = urllib.urlopen(yql) | |
json_str = urlh.read() | |
urlh.close() | |
json_obj = json.loads(json_str) | |
if isinstance(json_obj['query']['results']['place'], list): | |
place = json_obj['query']['results']['place'][0] | |
else: | |
place = json_obj['query']['results']['place'] | |
lat = place['centroid']['latitude'] | |
long = place['centroid']['longitude'] | |
print "%s, %s" % (lat, long) | |
time.sleep(10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment