Skip to content
Learn more
Please note that GitHub no longer supports Internet Explorer versions 7 or 8.

We recommend upgrading to the latest Internet Explorer, Google Chrome, or Firefox.

If you are using IE 9 or later, make sure you turn off "Compatibility View".

A fast, offline reverse geocoder in Python
Python
branch: master

README.md

Reverse Geocoder

A Python library for offline reverse geocoding. It improves on an existing library called reverse_geocode developed by Richard Penman.

v1.0 does not support Python 3.

About

Ajay Thampi @thampiman opensignal.com ajaythampi.com

Features

  1. Besides city/town and country code, this library also returns the nearest latitude and longitude and also administrative regions 1 and 2.
  2. This library also uses a parallelised implementation of K-D trees which promises an improved performance especially for large inputs.

The K-D tree is populated with cities that have a population > 1000. The source of the data is GeoNames.

Installation

$ pip install reverse_geocoder

Package can be found on PyPI.

Update: v1.1 released containing Brandon's and David's fixes

Usage

The library supports two modes:

  1. Mode 1: Single-threaded K-D Tree (similar to reverse_geocode)
  2. Mode 2: Multi-threaded K-D Tree (default)
import reverse_geocoder as rg

coordinates = (51.5214588,-0.1729636),(13.9280531,100.3735803)

results = rg.search(coordinates) # default mode = 2

print results

The above code will output the following:

    [{'admin1': 'England',
      'admin2': 'Greater London',
      'cc': 'GB',
      'lat': '51.51116',
      'lon': '-0.18426',
      'name': 'Bayswater'},
     {'admin1': 'Nonthaburi',
      'admin2': '',
      'cc': 'TH',
      'lat': '13.91783',
      'lon': '100.42403',
      'name': 'Bang Bua Thong'}]

If you'd like to use the single-threaded K-D tree, set mode = 1 as follows:

results = rg.search(coordinates,mode=1)

Performance

The performance of modes 1 and 2 are plotted below for various input sizes.

Performance Comparison

Mode 2 runs ~2x faster for very large inputs (10M coordinates).

Acknowledgements

  1. Major inspiration is from Richard Penman's reverse_geocode library
  2. Parallelised implementation of K-D Trees is extended from this article by Sturla Molden
  3. Geocoded data is from GeoNames

License

The MIT License (MIT)

Something went wrong with that request. Please try again.