[R-sig-Geo] looking up country names from lat long

Mihai Valcu valcu at orn.mpg.de
Sun Dec 12 21:29:08 CET 2010


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:
> On Mon, Dec 6, 2010 at 1:41 PM, Josh Tewksbury<tewksjj at gmail.com>  wrote:
>> Hello - here is what I am sure is a quick question from someone new to R in
>> spatial stats. I have a long list of lat long coordinates and I would like
>> to do a location country check to extrapolate a country name from each
>> lat/long point.
>>
>> so, for example, my data look like
>>
>> lat, long
>> 14.42, -89.92
>> 14.42, -89.75
>> 14.42, -89.58
>> 14.42, -89.42
>> 14.42, -89.25
>>
>> continued for the terrestrial surface of the earth.
>>
>> If I want to add country names, so I can use those names as tags to merge in
>> other data, is there an easy lookup that will allow me to do that, so I end
>> up with:
>>
>> lat, long, country
>> 14.42, -89.92, Guatemala
>> 14.42, -89.75, Guatemala
>> 14.42, -89.58, Guatemala
>> 14.42, -89.42, El Salvador
>> 14.42, -89.25, Honduras
>   My 'geonames' package (on CRAN) can do lookups via the geonames.org
> web service:
>
>> GNcountryCode(lat=14.24,lng=-89.92)
> $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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

-- 
| Mihai Valcu
| Max Planck Institute for Ornithology
| Behavioural Ecology and Evolutionary Genetics
| Eberhard-Gwinner-Straße 7
| D-82319 Starnberg (Seewiesen)
| Phone: +49 (0)8157-932-343
| Fax:   +49-(0)8157-932-400
| http://orn.mpg.de/mitarbeiter/valcu.html



More information about the R-sig-Geo mailing list