I have a list of around 1000+ churches that contains the name of the church, simple address, the residing priest and some contains mass schedules. I thought it will be nice if we can get the latitude and longitude based on the given address.
The way I do it before is to manually search the address in Google Maps and get the latitude/location by centering the marker and executing the javascript below:
Without any prior knowledge about python scripts, I decided to give it a try and automate the process.
The list of churches is stored in a google spreadsheet so I initially thought the best way to access this is to use the Google Data API client for Python. I tried it and it works perfectly but then I found another library called gspread that provides an easier way to access google spreadsheets. Maybe Google Data API is easy as well but the website lacks enough examples necessary for a python newbie like me.
I already have python (2.7.3) installed on my mac so I only need to install gspread library:
First thing that you need to do is to login to your google account.
and then open up the worksheet by title (which I think you cannot do in Google Data API client)
Next, we need to iterate for each row and get the location of the church by using Google Maps API’s geocoding service.
Get the number of rows and use that for our for loop
I use startindex so that we can easily resume from the last row in case of any application error. wks.cell(i+1, 1) will fetch the name of the church that we can use to call the geocoding service.
We use urllib2 (import urllib2) to perform HTTP Get and json library (import json) to parse the json response. After that, getting the location data is just as easy as
and finally update our Google Spreadsheet with the location data
I made a few minor changes to the script so that it can accept command line params and also a bit of error handling. You can check out the source below.
It was really fun and I think I’m going to invest some time to learn python.