Skip to content

Instantly share code, notes, and snippets.

@andybega
Last active June 8, 2022 17:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save andybega/6159870 to your computer and use it in GitHub Desktop.
Save andybega/6159870 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")))
@aster28
Copy link

aster28 commented Jan 23, 2018

It ask for username which I added but still I get the same error, can you let me know how it can be solved, Thanks.

@julievdh
Copy link

julievdh commented Feb 4, 2018

Hi - same issue as above. What is the username requirement?

@juanchiem
Copy link

juanchiem commented Dec 16, 2018

To work with the geonames package you will need to sign up for a free account and take a note of your username or it will not work.

library(geonames)
options(geonamesUsername="yourid")

previosuly your account should be enabled to use the free webservices at the login page
(http://www.geonames.org/enablefreewebservice)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment