Skip to content
Back to formatted view

Raw Message

Message-ID: <AANLkTinNVs8Fjzh2KOsMcnE+J_dq4kYDZX=E_ohWiRNH@mail.gmail.com>
Date: 2010-12-06T14:19:27Z
From: Barry Rowlingson
Subject: looking up country names from lat long
In-Reply-To: <AANLkTinpSKz0BwrEjoVKQk7oFAcFEPfDaYm6KowQwYxf@mail.gmail.com>

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