Skip to main content

A simplistic Python Geocoder (Google, Bing, OSM & more)

Project description

Geocoder version build

A simplistic Python Geocoder.

Geocoder is an Apache2 Licensed Geocoding library, written in Python.

>>> import geocoder
>>> g = geocoder.google('Moscone Center')
>>> g.latlng
(37.784173, -122.401557)
>>> g.city
'San Francisco'
...

Installation

You can install, upgrade, uninstall Geocoder with these commands:

$ pip install geocoder
$ pip install --upgrade geocoder
$ pip uninstall geocoder

Documentation

Search with Google

Using the Geocoder API from Google, this is a simplistic approach to return you all the same results that Google would provide.

>>> import geocoder
>>> g = geocoder.google('1600 Amphitheatre Pkwy, Mountain View, CA')
>>> g.latlng
(37.784173, -122.401557)
>>> g.postal
'94043'
>>> g.city
'Mountain View'
>>> g.country
'United States'
...

Long names with Google

When using Google, the default results are using the short names, here is how you would retrieve the long names as the results.

>>> g = geocoder.google(<address>, short_name=False)
...

Getting JSON

The web uses JSON and GeoJSON, here is how to return your Geocoded address into this format.

>>> g = geocoder.google('1600 Amphitheatre Parkway, Mountain View, CA')
>>> g.json
{'address': '1600 Amphitheatre Parkway, Mountain View, CA 94043, USA',
'bbox': {'northeast': {'lat': 37.4233474802915, 'lng': -122.0826054197085},
'southwest': {'lat': 37.4206495197085, 'lng': -122.0853033802915}},
'city': 'Mountain View',
'country': 'United States',
'lat': 37.4219985,
'lng': -122.0839544,
'location': '1600 Amphitheatre Parkway, Mountain View, CA 94043, USA',
'ok': True,
'postal': '94043',
'provider': 'Google',
'quality': 'ROOFTOP',
'status': 'OK'}
...

GeoJSON is a widely used, open format for encoding geographic data, and is supported by a number of popular applications.

>>> import simplejson as json
>>> g = geocoder.google('Ottawa, ON')
>>> json.dumps(g.geojson, indent=4)
{
"geometry": {
    "type": "Point",
    "coordinates": [
        -75.69719309999999,
        45.4215296
    ]
},
"crs": {
    "type": "name",
    "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
},
"type": "Feature",
"properties": {
    "status": "OK",
    "city": "Ottawa",
    "ok": true,
    "country": "Canada",
...

Using Proxies & Timeout

There many obvious reasons why you would need to use proxies, here is the basic syntax on how to successfully use them.

Timeouts are used to stop the connection if it reaches a certain time.

>>> proxies = '111.161.126.84:80'
>>> g = geocoder.google('Ottawa', proxies=proxies, timeout=5.0)
<[OK] Geocoder Google [Ottawa, ON, Canada]>
...

Reverse Geocoding

Using Google’s reverse geocoding API, you are able to input a set of coordinates and geocode its location.

>>> latlng = (48.85837, 2.2944813)
>>> g = geocoder.reverse(latlng)
<[OK] Geocoder Google [Eiffel Tower, Paris, France]>
...

Bounding Box (Extent)

>>> g = geocoder.osm('1600 Amphitheatre Pkwy, Mountain View, CA')
>>> g.bbox
{'northeast': {'lat': 37.4233474802915, 'lng': -122.0826054197085},
'southwest': {'lat': 37.4206495197085, 'lng': -122.0853033802915}}
>>> g.southwest
{'lat': 37.4206495197085, 'lng': -122.0853033802915}
>>> g.south
37.4206495197085
...

Geocoding IP Address

Retrieves geocoding data from MaxMind’s GeoIP2 services

>>> g = geocoder.ip('74.125.226.99')
>>> g.address
'Mountain View, California United States'
>>> g.latlng
(37.4192, -122.0574)

Geocoding your current IP address, simply use me as the input.

>>> g = geocoder.ip('me')
>>> g.address
'Ottawa, Ontario Canada'
>>> g.latlng
(45.4805, -75.5237)
...

Population Data from City

Retrieves population data from Geonames’s Web Service API.

>>> pop = geocoder.population('Springfield, Virginia')
>>> pop
30484
...

Geocoder Attributes

  • address
  • location
  • city
  • state
  • country
  • postal
  • quality
  • status
  • population (integer)
  • ok (boolean)
  • x, lng, longitude (float)
  • y, lat, latitude (float)
  • latlng, xy (tuple)
  • bbox {southwest, northeast}
  • southwest {lat, lng}
  • northeast {lat, lng}
  • south, west, north, east (float)

Geocoding Providers

## Priority Geocoders
>>> geocoder.google(<location>)
>>> geocoder.reverse(<latlng>)
>>> geocoder.ip(<ip>)

## Secondary Geocoders
>>> geocoder.osm(<location>)
>>> geocoder.mapquest(<location>)
>>> geocoder.arcgis(<location>)
>>> geocoder.geonames(<location>, username='XXXXX')
>>> geocoder.bing(<location>, key='XXXXX')
>>> geocoder.nokia(<location>, app_id='XXXXX', app_code='XXXXX')
>>> geocoder.tomtom(<location>, key='XXXXX')
...

Support

This project is free & open source, it would help greatly for you guys reading this to contribute, here are some of the ways that you can help make this Python Geocoder better.

Feedback

Please feel free to give any feedback on this module. If you find any bugs or any enhancements to recommend please send some of your comments/suggestions to the Github Issues Page.

Project details


Release history Release notifications

History Node

1.38.1

History Node

1.38.0

History Node

1.37.0

History Node

1.36.0

History Node

1.35.0

History Node

1.34.0

History Node

1.33.0

History Node

1.32.1

History Node

1.32.0

History Node

1.31.0

History Node

1.30.1

History Node

1.29.3

History Node

1.29.2

History Node

1.29.1

History Node

1.29.0

History Node

1.28.0

History Node

1.27.0

History Node

1.26.0

History Node

1.25.0

History Node

1.24.1

History Node

1.24.0

History Node

1.23.2

History Node

1.23.1

History Node

1.23.0

History Node

1.22.6

History Node

1.22.5

History Node

1.22.4

History Node

1.22.3

History Node

1.21.2

History Node

1.21.1

History Node

1.21.0

History Node

1.20.1

History Node

1.20.0

History Node

1.19.0

History Node

1.18.1

History Node

1.18.0

History Node

1.17.5

History Node

1.17.4

History Node

1.17.3

History Node

1.17.2

History Node

1.17.1

History Node

1.17.0

History Node

1.16.3

History Node

1.16.2

History Node

1.16.1

History Node

1.16.0

History Node

1.15.1

History Node

1.15.0

History Node

1.14.0

History Node

1.13.0

History Node

1.12.0

History Node

1.11.2

History Node

1.11.1

History Node

1.10.1

History Node

1.10.0

History Node

1.9.0

History Node

1.8.2

History Node

1.8.1

History Node

1.8.0

History Node

1.7.2

History Node

1.7.1

History Node

1.6.4

History Node

1.6.3

History Node

1.6.2

History Node

1.6.0

History Node

1.5.3

History Node

1.5.1

History Node

1.5.0

History Node

1.4.1

History Node

1.4.0

History Node

1.3.1

History Node

1.3.0

History Node

1.2.4

History Node

1.2.3

History Node

1.2.2

History Node

1.2.1

History Node

1.2.0

History Node

1.1.5

History Node

1.1.4

History Node

1.1.3

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.9

History Node

1.0.8

History Node

1.0.7

History Node

1.0.6

History Node

1.0.4

History Node

1.0.3

History Node

1.0.2

History Node

1.0.1

History Node

1.0.0

History Node

0.9.1

History Node

0.9.0

History Node

0.8.4

History Node

0.8.3

History Node

0.8.2

History Node

0.8.1

History Node

0.8.0

History Node

0.7.3

History Node

0.7.2

History Node

0.7.1

History Node

0.7.0

History Node

0.6.2

History Node

0.6.1

History Node

0.6.0

This version
History Node

0.5.9

History Node

0.5.8

History Node

0.5.7

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
geocoder-0.5.9.tar.gz (14.0 kB) Copy SHA256 hash SHA256 Source None Jun 5, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page