/reverse-geocoder

A fast, offline reverse geocoder in Python

Primary LanguagePythonMIT LicenseMIT

Reverse Geocoder

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

Update: v1.1 released! Python 3 not supported still.

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)