A simple script written as an experiment in geocoding addresses in a database. An address such as "100 Any Street, Anytown, CA, 10010" is passed to a Google Maps URL, and the latitude/longitude coordinates are extracted from the returned XML.
XML methods are not used in this script, but simple string searches instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # gLatLong.py - M.Keranen (mksql@yahoo.com) - 01/09/2006
# ------------------------------------------------------
# Get lat/long coordinates of an address from Google Maps
import os,urllib
addr = raw_input('\nAddress or (Lat,Long): ')
while addr <> '':
url = ''
if addr[0]=='(':
center = addr.replace('(','').replace(')','')
lat,lng = center.split(',')
url = 'http://maps.google.com/maps?q=%s+%s' % (lat,lng)
else:
# Encode query string into URL
url = 'http://maps.google.com/?q=' + urllib.quote(addr) + '&output=js'
print '\nQuery: %s' % (url)
# Get XML location
xml = urllib.urlopen(url).read()
if '<error>' in xml:
print '\nGoogle cannot interpret the address.'
else:
# Strip lat/long coordinates from XML
lat,lng = 0.0,0.0
center = xml[xml.find('{center')+10:xml.find('}',xml.find('{center'))]
center = center.replace('lat:','').replace('lng:','')
lat,lng = center.split(',')
url = 'http://maps.google.com/maps?q=%s+%s' % (lat,lng)
if url<>'':
print 'Map: %s' % (url)
os.startfile(url)
addr = raw_input('\nAddress or (Lat,Long): ')
|
Tags: web
geopy. Interested users may also want to check out geopy at http://exogen.case.edu/projects/geopy/, it also has support for this method.