how to convert from longitude and latitude to country or city?

Question:

I need to convert longitude and latitude coordinates to either country or city, is there an example of this in python?

thanks in advance!

Asked By: godzilla

||

Answers:

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')
Answered By: Holy Mackerel

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:

Answered By: pfctdayelise

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
Answered By: linqu

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))
Answered By: tibernut
# Python3 program for reverse geocoding. 

# importing necessary libraries 
import reverse_geocoder as rg 
from pandas import DataFrame
import pandas as pd

def reverseGeocode(coordinates): 
    result = rg.search(coordinates) 
    
    return result

    
# Coorinates tuple.Can contain more than one pair. 

if __name__ == "__main__":
    result = []
    path = "CSV_NAME.CSV"
    df = pd.read_csv(path, error_bad_lines=False)
    for i in range(0, len(df)):
        coordinates =(df["latitude"], df["longitude"]) 
        result.append(reverseGeocode(coordinates))
    print(result)
        
Answered By: Lokesh K V
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.