geocode.py

Document Sample
geocode.py Powered By Docstoc
					import sys, urllib from time import sleep GMAP_KEY = "ABQIAAAAszRxYqUs5L07cWGIGjEqBSzXZEw7_ZsuNYLW3ZmroYxAOaxNRQQFtvDvdzs7WqErSny5NWcy1OoNg " def geocode(address): code, accuracy, lat, long = urllib.urlopen("http://maps.google.com/maps/geo?q="+address+ "&output=csv&key=" + GMAP_KEY).read().split(",") code = int(code) accuracy = int(accuracy) if code == 200: return [lat, long, code] else: return [0, 0, code] def success(name, code, lat, long, space, out): global valid if invalid(lat, long) == True: print name, " " * space, "\x1b[31mFail(Invalid latitude or longitude)\x1b[0m\tSkipping..." else: print name, " " * space, "\x1b[32mSuccess\x1b[0m" valid.append(",".join([ str(k) for k in [name,code,lat,long]])) def invalid(lat, long): lat = float(lat) long = float(long) if lat < 0 or long > 0: return True return False valid = [] l = open("files/airports.txt", "r").read().split("\n")[:-1] out = open("files/map.data", "w") for k in l: city, name, state, code = k.split(",") address = urllib.quote_plus(code + " airport") lat, long, status_code = geocode(address) space = 50 - len(name) if status_code != 200: print name, " " * space, "\x1b[31mFail(%d)\x1b[0m\tRetrying..." % status_code address = urllib.quote_plus(code) lat, long, status_code = geocode(address) if status_code != 200: print name, " " * space, "Fail(%d)\tSkipping..." % status_code else: success(name, code, lat, long, space, out)

else: success(name, code, lat, long, space, out) sleep(0.5) out.write(str(len(valid)) + "\n") out.write("\n".join(valid)) out.close()


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:4
posted:1/3/2010
language:English
pages:2