Monday, September 8, 2014

GoogleV3 geocoder

This weekend I was playing with GoogleV3 geocoder and Python, as I needed some geocoded data for one of the projects.

The script itself is essentially copy and paste of the example code. However, I added a timeout call to the function in order to prevent any crashes if the server is not responsive. Additionally, I added an if-else statement in order to check whether the geocoder returned a valid value before writing it to file (again, in order to prevent any crashes).

It was certainly pleasing to see that so hundreds of addresses can be geocoded to lat/long within seconds. The only limitation was that a public API allows only about 1,000 or so calls per 24 hours (presumably for a given IP address).

I also wonder how accurate the geocoder is - it would be nice to plot all the data on the map and check how well it corresponds to what's on the ground.

import os, sys,re, time, csv
from geopy.geocoders import GoogleV3

geolocator = GoogleV3()
resultFile = "output.txt"

fileWrite = open(resultFile,'w')
fileRead = open("input.csv")
fileRead.readline() # read the first line

for line in fileRead:
    theaddress = splitline[3] # array index of address in my sample data
    theaddress += (", San Francisco, CA")
    result = geolocator.geocode(theaddress, timeout=20)
    if result:
        address,(latitude, longitude) = result
    else:
        print("Error: geocode failed on input %s"%(theaddress))
    fileWrite.write(address,(latitude, longitude) + "\n")

fileRead.close()
fileWrite.close()
print("done")

No comments :

Post a Comment