Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

thanks in advance!

like image 421
godzilla Avatar asked Nov 24 '13 00:11

godzilla


People also ask

How do you convert latitude and longitude to location in Python?

Use the geolocator. reverse() function and supply the coordinates (latitude and longitude) to get the location data.

How do I convert latitude and longitude to addresses in Excel?

To get the latitude of the address in cell B2, use the formula = GetLatitude(B2) To get the longitude of the address in cell B2, use the formula = GetLongitude(B2) To get both the latitude and longitude of the address in cell B2, use the formula = GetCoordinates(B2)


1 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') 
like image 57
Holy Mackerel Avatar answered Sep 25 '22 23:09

Holy Mackerel