Reverse Geocode With Administrative Boundaries.


  1. Get a shape file of administrative boundaries.

2. Read the .shp file into a list of geojson.

import fiona
sh ='your_shape_file.shp')

3. Transform the coordinates to what you want.

4. Sample some points inside and outside the administrative boundaries.

from shapely.geometry import shape #to read geojson
#get 1 administrative boundary for example
ab = sh[0]
poly = shape(ab['geometry'])
pts_inside = poly.buffer( -0.0001)
pts_outside = poly.buffer(0.0001)

5. Build a KDTree from points inside and outside of the boundaries.
(Thinks to Bob Haffner.)

6. As a result, any point can be reverse geocode to , in this case, 7 thousands of administrations in nano second.