Skip to content

Instantly share code, notes, and snippets.

@Bustami
Forked from andybega/geonames-city-coord.R
Created February 24, 2016 03:31
Show Gist options
  • Save Bustami/cc93d4ddc8d05e233b23 to your computer and use it in GitHub Desktop.
Save Bustami/cc93d4ddc8d05e233b23 to your computer and use it in GitHub Desktop.
Look up coordinates for city names
library(geonames)
# list of city names (in Afghanistan)
cities <- c("Gereshk", "Lashkar Gah", "Marjah", "Nad-e Ali")
-5L), class = "data.frame")
# conveninence function to look up and format results
GNsearchAF <- function(x) {
res <- GNsearch(name=x, country="AF")
return(res[1, ])
}
# loop over city names and reformat
GNresult <- sapply(cities, GNsearchAF)
GNresult <- do.call("rbind", GNresult)
GNresult <- cbind(city=row.names(GNresult),
subset(GNresult, select=c("lng", "lat", "adminName1")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment