bennylope / pygeocodio

:globe_with_meridians: A Python wrapper for the Geocodio geolocation service API
BSD 3-Clause "New" or "Revised" License
92 stars 21 forks source link

Batch geocoding omits the "query" key #3

Closed MiniCodeMonkey closed 10 years ago

MiniCodeMonkey commented 10 years ago

It looks like the query key is omitting when performing a batch geocoding request. The key is used to match up the address again after it has been geocoded.

>>> from geocodio import GeocodioClient
>>> geocodio = GeocodioClient('API_KEY')
>>> geocoded_addresses = geocodio.geocode([
...         '1600 Pennsylvania Ave, Washington, DC',
...         '3101 Patterson Ave, Richmond, VA, 23221'
...     ])
>>> geocoded_addresses
[{u'input': {u'address_components': {u'city': u'Washington', u'state': u'DC', u'street': u'Pennsylvania', u'number': u'1600', u'suffix': u'Ave'}, u'formatted_address': u'1600 Pennsylvania Ave, Washington, DC'}, u'results': [{u'address_components': {u'city': u'Washington', u'state': u'DC', u'zip': u'20042'}, u'formatted_address': u'Washington, DC 20042', u'location': {u'lat': u'38.893311', u'lng': u'-77.014647'}, u'accuracy': 0.33}, {u'address_components': {u'city': u'Washington', u'state': u'DC', u'zip': u'20043'}, u'formatted_address': u'Washington, DC 20043', u'location': {u'lat': u'38.893311', u'lng': u'-77.014647'}, u'accuracy': 0.33}, {u'address_components': {u'city': u'Washington', u'state': u'DC', u'zip': u'20044'}, u'formatted_address': u'Washington, DC 20044', u'location': {u'lat': u'38.893311', u'lng': u'-77.014647'}, u'accuracy': 0.33}, {u'address_components': {u'city': u'Washington', u'state': u'DC', u'zip': u'20015'}, u'formatted_address': u'Washington, DC 20015', u'location': {u'lat': u'38.969260', u'lng': u'-77.070949'}, u'accuracy': 0.33}, {u'address_components': {u'city': u'Washington', u'state': u'DC', u'zip': u'20013'}, u'formatted_address': u'Washington, DC 20013', u'location': {u'lat': u'38.893311', u'lng': u'-77.014647'}, u'accuracy': 0.33}]}, {u'input': {u'address_components': {u'city': u'Richmond', u'suffix': u'Ave', u'zip': u'23221', u'number': u'3101', u'state': u'VA', u'street': u'Patterson'}, u'formatted_address': u'3101 Patterson Ave, Richmond, VA 23221'}, u'results': [{u'address_components': {u'city': u'Richmond', u'suffix': u'Ave', u'zip': u'23221', u'number': u'3101', u'county': u'Richmond city', u'state': u'VA', u'street': u'Patterson'}, u'formatted_address': u'3101 Patterson Ave, Richmond, VA 23221', u'location': {u'lat': 37.560890255102, u'lng': -77.477400571429}, u'accuracy': 0.8}]}]

See http://geocod.io/docs/?python#toc_7 where the example response shows query

bennylope commented 10 years ago

The query value is stored for looking up addresses, just not in the way represented by the service's JSON response in the docs.

In batch geocoding with pygeocodio, a LocationCollection is made by adding each individual address response into a Location (just slightly augmented Python list and dict objects, respectively). A Location doesn't store a query, because presumably you know what it is - you just used it to geocode an address. When batch geocoding of course, that's problematic. So this module ensures that the order of the results matches the order of the addresses queried, and it stores the queries in a separate dictionary for lookup.

>>> geocoded_addresses.get('3101 Patterson Ave, Richmond, VA, 23221')
{u'input': {u'address_components': {u'city': u'Richmond', u'suffix': u'Ave', u'zip': u'23221', u'number': u'3101', u'state': u'VA', u'street': u'Patterson'}, u'formatted_address': u'3101 Patterson Ave, Richmond, VA 23221'}, u'results': [{u'address_components': {u'city': u'Richmond', u'suffix': u'Ave', u'zip': u'23221', u'number': u'3101', u'county': u'Richmond city', u'state': u'VA', u'street': u'Patterson'}, u'formatted_address': u'3101 Patterson Ave, Richmond, VA 23221', u'location': {u'lat': 37.560890255102, u'lng': -77.477400571429}, u'accuracy': 0.8}]}

Note that to perform the key based lookup you must use the get method. This preserves the list’s index based lookup.

Having batch geocoded some addresses, you can access the dictionary from the returned object via the lookups attribute:

>>> geocoded_addresses.lookups
{'3101 Patterson Ave, Richmond, VA, 23221': 1, ...}

Where the value is the index of the address in the list. This is mostly in the pygeocodio docs although there is an extra attribute that doesn't exist mentioned there (either an old build or I failed to update the docs).

MiniCodeMonkey commented 10 years ago

Hi Ben,

Thanks you got the clarification. This makes perfect sense!

Mathias Hansen

On Sun, Mar 16, 2014 at 11:45 AM, Ben Lopatin notifications@github.com wrote:

Closed #3.

Reply to this email directly or view it on GitHub: https://github.com/bennylope/pygeocodio/issues/3