Skip to main content

Reverse geocode the given latitude / longitude

Project description

Reverse Geocode

Reverse Geocode takes a latitude / longitude coordinate and returns the nearest known country, state, and city. This can be useful when you need to reverse geocode a large number of coordinates so a web API is not practical.

The geocoded locations are from geonames. This data is then structured in to a k-d tree for efficiently finding the nearest neighbour.

Note that as this is point based and not a polygon based lookup it will only give a rough idea of the location/city.

Example usage

Example reverse geocoding a coordinate:

>>> import reverse_geocode
>>> melbourne_coord = -37.81, 144.96
>>> reverse_geocode.get(melbourne_coord)
{'country_code': 'AU', 'city': 'Melbourne', 'latitude': -37.814, 'longitude': 144.96332, 'population': 4917750, 'state': 'Victoria', 'country': 'Australia'}

Example reverse geocoding a list of coordinates:

>>> nyc_coord = 40.71427000, -74.00597000
>>> reverse_geocode.search((melbourne_coord, nyc_coord))
[{'country_code': 'AU', 'city': 'Melbourne', 'latitude': -37.814, 'longitude': 144.96332, 'population': 4917750, 'state': 'Victoria', 'country': 'Australia'},
 {'country_code': 'US', 'city': 'New York City', 'latitude': 40.71427, 'longitude': -74.00597, 'population': 8804190, 'state': 'New York', 'country': 'United States'}]

By default the nearest known location is returned, which may not be as expected when there is a much larger city nearby. For example querying for the following coordinate near NYC will return Seaport:

>>> nyc_coordinate = 40.71, -74.00
>>> reverse_geocode.get(nyc_coordinate)
{'country_code': 'US', 'city': 'Seaport', 'latitude': 40.70906, 'longitude': -74.00317, 'population': 8385, 'state': 'New York', 'county': 'New York County', 'country': 'United States'}

To filter for larger cities a minimum population can be set. Using a minimum population of 100000 with the above coordinate now returns NYC:

>>> reverse_geocode.get(nyc_coordinate, min_population=100000)
{'country_code': 'US', 'city': 'New York City', 'latitude': 40.71427, 'longitude': -74.00597, 'population': 8804190, 'state': 'New York', 'country': 'United States'}

Install

pip install reverse-geocode

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

reverse_geocode-1.6.5.tar.gz (3.3 MB view details)

Uploaded Source

Built Distribution

reverse_geocode-1.6.5-py3-none-any.whl (3.3 MB view details)

Uploaded Python 3

File details

Details for the file reverse_geocode-1.6.5.tar.gz.

File metadata

  • Download URL: reverse_geocode-1.6.5.tar.gz
  • Upload date:
  • Size: 3.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for reverse_geocode-1.6.5.tar.gz
Algorithm Hash digest
SHA256 032aa42e76c76bc6729557eb247a6cc4b7817cb4e5eaef48437115f20ad7ae41
MD5 8e89304c03fe17a523c4d95eafb2eeba
BLAKE2b-256 8b7c552b672bee743ff06f5165e67aa1bbbeea97a11d8242da8ff934ac74743d

See more details on using hashes here.

File details

Details for the file reverse_geocode-1.6.5-py3-none-any.whl.

File metadata

File hashes

Hashes for reverse_geocode-1.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3937d390da78dd1eb17129d93c827398331e604258cd95624ad43471ba8f30f5
MD5 af4ea64bc45f80a65fb2d634ebf1068e
BLAKE2b-256 c9bd61f9997710048902cef3c08572cfb502031dfaf8915157372ac15210a1aa

See more details on using hashes here.

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