Can we get the location name based on Longitude and Latitude in Python?

google_map
ipython
python

#1

Hi,

I have Longitude and Latitude of various location in a data set but I am not able to make sense out of it. Is there any Google API and Python library that can help to extract the location name based on given Longitude and Latitude?

Thanks,
Imran


How to handle Lat/Lon features in a model?
#2

This is called reverse geo coding. In R the code would be

reverseGeoCode <- function(latlng) {
latlngStr <- gsub(’ ‘,’%20’, paste(latlng, collapse=","))#Collapse and Encode URL Parameters
library(“RJSONIO”) #Load Library
#Open Connection
connectStr <- paste(‘http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=’,latlngStr, sep="")
con <- url(connectStr)
data.json <- fromJSON(paste(readLines(con), collapse=""))
close(con)
#Flatten the received JSON
data.json <- unlist(data.json)
if(data.json[“status”]==“OK”)
address <- data.json[“results.formatted_address”]
return (address)
}
address <- reverseGeoCode(c(37.4418834, -122.1430195))


#3

Try PyGeocoder - https://chrisalbon.com/python/geocoding_and_reverse_geocoding.html

from pygeocoder import Geocoder

location = Geocoder.reverse_geocode(12.9716,77.5946)
print "City:",location.city
print "Country:",location.country

City: Bangalore
Country: India