Skip to content

Country names from coordinates

4 messages · Miluji Sb, Bacou, Melanie, Roger Bivand

#
I have the following data at 0.5 degree by 0.5 degree.

temp <- dput(head(ptsDF,10))
structure(list(longitude = c(-68.25, -67.75, -67.25, -68.25,
-67.75, -67.25, -71.25, -70.75, -69.25, -68.75), latitude = c(-54.75,
-54.75, -54.75, -54.25, -54.25, -54.25, -53.75, -53.75, -53.75,
-53.75), GDP = c(1.683046, 0.3212307, 0.0486207, 0.1223268, 0.0171909,
0.0062104, 0.22379, 0.1406729, 0.0030038, 0.0057422)), .Names =
c("longitude",
"latitude", "GDP"), row.names = c(4L, 17L, 30L, 43L, 56L, 69L,
82L, 95L, 108L, 121L), class = "data.frame")

I would like add the corresponding country names to each of the
coordinates. This is what I have done:

library(data.table)
library(rgdal)
library(reshape2)
library(dplyr)
library(tidyr)
library(lubridate)
library(maps)
library(countrycode)
library(ggplot2)
library(raster)

coord_cells <-temp [,c(1,2)]
pts_cells <-
SpatialPoints(coord_cells,proj4string=CRS(proj4string(worldmap)))
indices_cells <- over(pts_cells, worldmap,na.rm=TRUE)
foo_cells<-indices_cells[,c(3,5)] # Keep ISO3 and country names only
new_data <- cbind(foo_cells, temp)

However, I get a large number of NAs for coordinates which should have
corresponding countries. What am I doing wrong? Any suggestions? Thank you.

Sincerely,

Milu
#
Here is an approach using raster::extract(). I assume your point 
locations are unprojected.

library(raster)
library(tmap)

data(World)
proj4string(World)
# [1] "+proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 
+units=m +no_defs +towgs84=0,0,0"

pts <- SpatialPointsDataFrame(temp [, c(1,2)], temp, 
proj4string=CRS("+init=epsg:4326"))
# Reproject World
World <- spTransform(World, CRS("+init=epsg:4326"))
# Extract and append World attributes at pts point locations
pts <- extract(World, pts, sp=TRUE)

--Mel.
On 7/25/2016 9:01 AM, Miluji Sb wrote:
#
On Mon, 25 Jul 2016, Bacou, Melanie wrote:

            
Offline, OK, but online you may use the geonames package, which requires a 
valid geonames user name:

library(geonames)
options(geonamesUsername="<me>")
res <- vector(mode="list", length=nrow(ptsDF))
for (i in 1:nrow(ptsDF)) res[[i]] <- try(GNcountryCode(ptsDF$latitude[i],
  ptsDF$longitude[i], radius=50))
res

using radius= to catch a slightly offshore point in Chile in the example.

Roger

  
    
#
Dear Melanie,

Your solution works great! Thank you so much.

Dear Roger,

While this solution also works, I get the following error:  "error code 19
from server: the hourly limit of 2000 credits for milu has been exceeded.
Please throttle your requests or use the commercial service".

It seems there's a limit to the amount of requests to the packages. Thank
you though!

Is there a way to plot the data for a particular country (e.g. Germany) by
latitude and longitude?

temp <- dput(head(ptsDF,10))
structure(list(longitude = c(-68.25, -67.75, -67.25, -68.25,
-67.75, -67.25, -71.25, -70.75, -69.25, -68.75), latitude = c(-54.75,
-54.75, -54.75, -54.25, -54.25, -54.25, -53.75, -53.75, -53.75,
-53.75), GDP = c(1.683046, 0.3212307, 0.0486207, 0.1223268, 0.0171909,
0.0062104, 0.22379, 0.1406729, 0.0030038, 0.0057422)), .Names =
c("longitude",
"latitude", "GDP"), row.names = c(4L, 17L, 30L, 43L, 56L, 69L,
82L, 95L, 108L, 121L), class = "data.frame")

I have tried the following but doesn't work. Thanks again

pts_germany <- pts[(pts$country=="Germany"),]

mapCountryData(deu, nameColumnToPlot= "GDP", mapRegion="Germany",
catMethod="fixedWidth")
On Mon, Jul 25, 2016 at 8:38 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote: