So this is an alternative that uses the reverse geocoding API on Google. This code is based in part on this reference.
Calling your dataframe above df
,
reverseGeoCode <- function(latlng) {
require("XML")
require("httr")
latlng <- as.numeric(latlng)
latlngStr <- gsub(' ','%20', paste(round(latlng,2), collapse=","))
url <- "http://maps.google.com"
path <- "/maps/api/geocode/xml"
query <- list(sensor="false",latlng=latlngStr)
response <- GET(url, path=path, query=query)
if (response$status !=200) {
print(paste("HTTP Error:",response$status),quote=F)
return(c(NA,NA))
}
xml <- xmlInternalTreeParse(content(response,type="text"))
status <- xmlValue(getNodeSet(xml,"//status")[[1]])
if (status != "OK"){
print(paste("Query Failed:",status),quote=F)
return(c(NA,NA))
}
xPath <- '//result[1]/address_component[type="country"]/long_name[1]'
country <- xmlValue(getNodeSet(xml,xPath)[[1]])
xPath <- '//result[1]/address_component[type="administrative_area_level_1"]/long_name[1]'
state <- xmlValue(getNodeSet(xml,xPath)[[1]])
return(c(state=state,country=country))
}
st.cntry <- t(apply(df,1,function(x)reverseGeoCode(x[2:3])))
result <- cbind(df,st.cntry)
result
# Point_Name Longitude Latitude state country
# 1 University of Arkansas 36.06783 -94.17365 Arkansas United States
# 2 Lehigh University 40.60146 -75.36006 Pennsylvania United States
# 3 Harvard University 42.37939 -71.11590 Massachusetts United States
In the API definition, "administrative_area_level_1" is the highest administrative area below country. In the US these are states. In other countries the definition varies (might be provinces, for example).
Incidentally, I'm fairly certain you have latitude and longitude reversed.