Skip to content

Instantly share code, notes, and snippets.

@rajbot
Created February 14, 2011 22:14
Show Gist options
  • Save rajbot/826686 to your computer and use it in GitHub Desktop.
Save rajbot/826686 to your computer and use it in GitHub Desktop.
Get Lat/Long coords from street addresses using Yahoo Query Language
#!/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:
print
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