3

I am writing an app with Google App Engine using python and I am turning a large wikipedia list into a spreadsheet and then inputting the list rows into Locations. For example this: http://en.wikipedia.org/wiki/List_of_North_Carolina_state_parks and I need to turn the name of each park into an address, I would imagine they won't be exact but as long as they are almost correct it is alright with me.

Is there any way I can do this using python on the server side? I know about Google's Geocoding service but it is all done with javascript and it is rate limited.

Is there any service that can do this?

UPDATE: geopy is just what I was looking for. I am wondering what the best way to deal with multiple results is. Here is my attempt:

        try:
             place, (lat, lng) = g.geocode(title+", North Carolina")
        except ValueError:
             geocodespot = g.geocode(title+", North Carolina", exactly_one=False)
             place, (lat, lng) = geocodespot[0]

It works just fine but I am wondering if there are any better ideas.

clifgray
  • 4,313
  • 11
  • 67
  • 116

2 Answers2

5

There is the geopy library.

Example (from the getting started page):

from geopy import geocoders

g = geocoders.Google()
place, (lat, lng) = g.geocode("10900 Euclid Ave in Cleveland")
print "%s: %.5f, %.5f" % (place, lat, lng)
    10900 Euclid Ave, Cleveland, OH 44106, USA: 41.50489, -81.61027  
Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
Christian Thieme
  • 1,114
  • 6
  • 6
  • I recently did an integration of geocoding for [Plone](http://plone.org/) using `geopy`. Here's the [integration code](https://github.com/4teamwork/ftw.geo/blob/master/ftw/geo/handlers.py), could be useful as an example, and for error handling and caching strategies. – Lukas Graf Oct 02 '12 at 20:42
  • great this is exactly what I needed. I just added what I did for when multiple entries pop up. do you think this is the best way to handle it? – clifgray Oct 03 '12 at 05:03
1

Google geocode does not require any key to use.

All information about the most recent version can be found:

https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding

all you have to do is make a request to:

(example) http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

then use urllib

import urllib
// pull lat and lng from your parks database and construct a url like:
url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false'

resp = urllib.urlopen(url)
resp.read() // json string convert to python dict

It is rate limited, But it is a free service. It is most certainly not all done with javascript. Why does it matter if it is rate limited if you are just geocoding a static list of nc parks?

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • 1
    Since there's a library like `geopy` that does all this, you shouldn't be using the JSON API directly and constructing URLs by hand. It's quote error prone and has some odd quirks too look out for. For example [non-english character handling](http://stackoverflow.com/questions/12625745/) and [percent encoding](http://stackoverflow.com/questions/12114853/) – Lukas Graf Oct 02 '12 at 20:49
  • I agree with everything you said @LukasGraf but wanted to illustrate that it is very possible and very easy to make geocoding requests server side, that is all – dm03514 Oct 02 '12 at 21:33