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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Mon Dec 13 22:08:11 CET 2010



On 12/12/2010 09:29 PM, Mihai Valcu wrote:
> 
> 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"]

# in principle, both "@data" can be taken out;
# a alternative way would use over:

poi$cntry = over(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
>>
>>
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list