Skip to content

looking up country names from lat long

4 messages · Josh Tewksbury, Barry Rowlingson, Mihai Valcu +1 more

#
On Mon, Dec 6, 2010 at 1:41 PM, Josh Tewksbury <tewksjj at gmail.com> wrote:
My 'geonames' package (on CRAN) can do lookups via the geonames.org
web service:
$distance
[1] 0

$countryName
[1] "Guatemala"

$countryCode
[1] "GT"

 its being very slow at the moment though. You might have speedier responses.

 It can only look up one at a time, so you'll have to loop through
your data frame with the usual R loop/apply-type patterns.

Barry
6 days later
#
An offline country map + overlay should work pretty fast.

##########
require(rgdal)
setwd(tempdir())
download.file("http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/110m-admin-0-countries.zip",destfile 
= "temp")
unzip(zipfile = "temp")
w = readOGR(tempdir(), '110m_admin_0_countries')
# some random points of interest
poi = SpatialPointsDataFrame(spsample(w, 10, "random"), data = 
data.frame(id = 1:10))
poi at data$cntry =  w at data[overlay(poi, w), "NAME"]
plot(w)
plot(poi, add = T, col = 2)
text(poi, labels = poi$cntry, col = 2, cex = 1)
##########

Best wishes
Mihai
On 12/6/2010 3:19 PM, Barry Rowlingson wrote:

  
    
1 day later
#
On 12/12/2010 09:29 PM, Mihai Valcu wrote:
# in principle, both "@data" can be taken out;
# a alternative way would use over:

poi$cntry = over(poi, w)$NAME