how to convert from longitude and latitude to country or city?
This is called reverse geocoding. There is one library I could find in Python which is focused on this: https://github.com/thampiman/reverse-geocoder
Some related questions with other ideas:
- Offline Reverse Geocoding in Python
- Reverse Geocoding Without Web Access
In general the Google API is the best approach. It was not suitable for my case as I had to process a lot of entries and the api is slow.
I coded a small version that does the same but downloads a huge geometry first and computes the countries on the machine.
import requests
from shapely.geometry import mapping, shape
from shapely.prepared import prep
from shapely.geometry import Point
data = requests.get("https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson").json()
countries = {}
for feature in data["features"]:
geom = feature["geometry"]
country = feature["properties"]["ADMIN"]
countries[country] = prep(shape(geom))
print(len(countries))
def get_country(lon, lat):
point = Point(lon, lat)
for country, geom in countries.iteritems():
if geom.contains(point):
return country
return "unknown"
print(get_country(10.0, 47.0))
# Austria
I use Google's API.
from urllib2 import urlopen
import json
def getplace(lat, lon):
url = "http://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false" % (lat, lon)
v = urlopen(url).read()
j = json.loads(v)
components = j['results'][0]['address_components']
country = town = None
for c in components:
if "country" in c['types']:
country = c['long_name']
if "postal_town" in c['types']:
town = c['long_name']
return town, country
print(getplace(51.1, 0.1))
print(getplace(51.2, 0.1))
print(getplace(51.3, 0.1))
Output:
(u'Hartfield', u'United Kingdom')
(u'Edenbridge', u'United Kingdom')
(u'Sevenoaks', u'United Kingdom')
Google has since removed keyless access to their API. Head over to google and register for a key, you get ~ 1,000 free queries a day. Code in accepted answer should be modified like this (can't add a comment, not enough rep).
from urllib.request import urlopen
import json
def getplace(lat, lon):
key = "yourkeyhere"
url = "https://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false&key=%s" % (lat, lon, key)
v = urlopen(url).read()
j = json.loads(v)
components = j['results'][0]['address_components']
country = town = None
for c in components:
if "country" in c['types']:
country = c['long_name']
if "postal_town" in c['types']:
town = c['long_name']
return town, country
print(getplace(51.1, 0.1))
print(getplace(51.2, 0.1))
print(getplace(51.3, 0.1))