Skip to main content

pygeocode -- Wrapper for several public geocoding APIs

Project description

Description

pygeocode leverages multiple public geocoding APIs including Google’s Geocoding API and Yahoo’s Place Finder. Having several APIs available is specially useful when the user has reached one API’s rate limit or when an API returns ambiguous results, or no results at all.

Installation

You can use pip or easy_install

  • pip install pygeocode

  • easy_install pygeocode

Examples

Using Google’s Geocoding API:

from pygeocode import geocoder

address = '1600 Amphitheatre Pkwy, Mountain View, CA'
res = geocoder.geocode_google(address)
print res['lat'], res['lng']

Using Yahoo’s Place Finder API:

from pygeocode import geocoder

app_id = <you_app_id>
address = '1600 Amphitheatre Pkwy, Mountain View, CA'
res = geocoder.geocode_yahoo(address, app_id)
print res['lat'], res['lng']

Full example:

import optparse
import functools
import logging

from pygeocode import geocoder

log = logging.getLogger(__name__)

def main(address, appid=None):
    yahoo_geocoder = functools.partial(
        geocoder.geocode_yahoo,
        appid=appid,
        )
    geocoders = [yahoo_geocoder, geocoder.geocode_google]
    for geocoder_ in geocoders:
        try:
            res = geocoder_(address)
        except geocoder.GeocoderError, e:
            log.error(str(e))
        else:
            return res

if __name__ == '__main__':
    parser = optparse.OptionParser(
        usage='%prog ADDRESS [OPTS]',
        )
    parser.add_option(
        '--yahoo-appid',
        help='The Yahoo Application ID to be used in the API call',
        )
    parser.add_option(
        '-v', '--verbose',
        help='Verbose mode [default %default]',
        action="store_true", dest="verbose"
        )
    parser.set_defaults(
        verbose=False,
        )

    options, args = parser.parse_args()
    try:
        (address,) = args
    except ValueError:
        parser.error('Wrong number of arguments.')

    logging.basicConfig(
        level=logging.DEBUG if options.verbose else logging.INFO,
        format='%(asctime)s.%(msecs)03d %(name)s: %(levelname)s: %(message)s',
        datefmt='%Y-%m-%dT%H:%M:%S',
        )

    res = main(address, appid=options.yahoo_appid)

    if res:
        out = '"{address}" is at coordinates {lat},{lng}'.format(
            address=address,
            lat=res['lat'],
            lng=res['lng'],
            )
    else:
        out = 'No results found for "{address}"'.format(
            address=address,
            )

    print out

Building

External dependencies

  • python-dev

  • python-setuptools

  • python-virtualenv

Developing

Go to the root directory of the project and run:

# I like to install the virtual environment in its own
# hidden repo but you don't have to
virtualenv .virtual
# I leave the magic to Ruby developers (.virtual/bin/activate)
# but you don't have to agree with me
.virtual/bin/python setup.py develop
# Install development and testing dependecies. Pip doesn't seem
# to handle extras_require yet: https://github.com/pypa/pip/issues/7.
# So, use easy_install
# At this point, pygeocode will already be in easy-install.pth.
# So easy_install will not attempt to download it
.virtual/bin/easy_install pygeocode[test]

You can now run the tests:

.virtual/bin/nosetests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygeocode-0.0.3.tar.gz (5.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page