Skip to content
A fork of the geocoding library geopy. Returns accuracy scores. Allows viewport biasing.
Python
Find file
Pull request Compare This branch is 1 commit ahead, 7 commits behind datadesk:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
geopy
.gitignore
LICENSE
README.textile
setup.py

README.textile

                                              ,,                                          
`7MMM.     ,MMF'                              db                           `7MMF'            db      
  MMMb    dPMM                                                               MM             ;MM:     
  M YM   ,M MM   ,6"Yb. `7MMpdMAo.`7MMpdMAo.`7MM  `7MMpMMMb.  .P"Ybmmm       MM            ,V^MM.    
  M  Mb  M' MM  8)   MM   MM   `Wb  MM   `Wb  MM    MM    MM :MI  I8         MM           ,M  `MM    
  M  YM.P'  MM   ,pm9MM   MM    M8  MM    M8  MM    MM    MM  WmmmP"         MM      ,    AbmmmqMA   
  M  `YM'   MM  8M   MM   MM   ,AP  MM   ,AP  MM    MM    MM 8M              MM     ,M   A'     VML  
.JML. `'  .JMML.`Moo9^Yo. MMbmmd'   MMbmmd' .JMML..JMML  JMML.YMMMMMb      .JMMmmmmMMM .AMA.   .AMMA.
                          MM        MM                       6'     dP                               
                        .JMML.    .JMML.                     Ybmmmd'                                 

A fork of the geocoding library geopy, modified, with a few new toys.

Includes:

  • An option to return an accuracy score along with the result
  • An option to utilize Google’s viewport biasing system that looks for results in a frame you provide (i.e. Downtown Los Angeles)

How it works

First fire up geopy and load the Google geocoder like you normally would.

$ python
>>> from geopy import geocoders
>>> g = geocoders.Google(BENS_GOOGLE_API_KEY)

Now run an address like you typically would and see the usual output. Let’s try the address of the Los Angeles Times.

>>> point_generator = g.geocode('202 W. 1st St., Los Angeles, Los Angeles, CA', exactly_one=False)
>>> from pprint import pprint
>>> pprint(list(point_generator))
[(u'202 W 1st St, Los Angeles, CA 90731, USA',
  (33.743286099999999, -118.28143780000001)),
 (u'202 W 1st St, Los Angeles, CA 90012, USA',
  (34.052993000000001, -118.24482500000001))]

Unfortunately, we get two results in LA for the same address. Let’s use my new optional keyword argument, ‘return_accuracy’ so see how good they are.

>>> point_generator = g.geocode('202 W. 1st St., Los Angeles, Los Angeles, CA', exactly_one=False, return_accuracy=True)

You’ll see that the generator now includes the accuracy score at the tail end. You can decode the number here. Eight is pretty good.

>>> pprint(list(point_generator))
[(u'202 W 1st St, Los Angeles, CA 90731, USA',
  (33.743286099999999, -118.28143780000001),
  u'8'),
 (u'202 W 1st St, Los Angeles, CA 90012, USA',
  (34.052993000000001, -118.24482500000001),
  u'8')]

But they’re both at the same high accuracy level. Why? Because that address occurs both in Downtown L.A. and in the long-ago annexed neighborhood of San Pedro.

So, let’s say we know the general area to look, but don’t have a zipcode to search with. How could we zero in on the right address? Try adding a viewport to ask Google to limit results to a particular extent. The viewport is two things, the centroid (y,x) followed by the distance between the centroid and the extent corners (min, max) in decimal degrees. For this example, I used the viewport of the Downtown neighborhood defined by Mapping L.A. You can read more about viewport biasing here.

>>> centroid = (34.039020123166175, -118.24590404333648)
>>> span = (0.037236705899272908, 0.034925735835917911)

Now check this out. The viewport will limit our results to just the downtown address. One trick is to remove the city designation here, as it can just muddle things up.

>>> point_generator = g.geocode(
  '202 W. 1st St.',
  exactly_one=False,
  return_accuracy=True,
  viewport_centroid=centroid,
  viewport_span=span
  )
>>> pprint(list(point_generator))
[(u'202 W 1st St, Los Angeles, CA 90012, USA',
  (34.052993000000001, -118.24482500000001),
  u'8')]

If you’re really enthusiastic, check out this dummy GeoDjango model that calculates the viewport for you.

Something went wrong with that request. Please try again.