0

Using R and relevant GIS packages, I would like to determine which countries are within an arbitrary bounding box and extract the corresponding ISO codes. For example...

require(maps)
require(rgdal)
require(raster)

e1<-extent(c(-109,-79,10,28))
bbox1<-bbox(e1)

How can I then use this bbox or four coordinates to determine which countries recognized in the GADM database fall within this bounding box?

  • Which "RGDAL database"? – Spacedman Oct 24 '17 at 17:21
  • Sorry, that was a typo. I meant the GADM database. I've edited the question above, thanks in advance! – Thraupidae Oct 24 '17 at 20:24
  • Hmm tricky... The country has to be completely within your extent? And what about countries around the +180/-180 line? You might have to download all the countries from GADM in order to test them, or at least get the bounding boxes of every country.... – Spacedman Oct 24 '17 at 20:59
1

This is close. First use map("world") with your limits to get the names:

> cnames  = map("world", xlim=c(-109,-79),ylim=c(10,28))$names

Need to chop off the sub-region names and get all the unique countries into a data frame:

> cnames = data.frame(name = unique(gsub(":.*","",cnames)))
> cnames
             name
1          Belize
2        Colombia
3      Costa Rica
4            Cuba
5  Cayman Islands
6       Guatemala
7        Honduras
8          Mexico
9       Nicaragua
10    El Salvador
11            USA

Now join that with the ISO3 codes:

> dplyr::left_join(data.frame(name = cnames), getData("ISO3"), c("name"="NAME"))
             name ISO3
1          Belize  BLZ
2        Colombia  COL
3      Costa Rica  CRI
4            Cuba  CUB
5  Cayman Islands  CYM
6       Guatemala  GTM
7        Honduras  HND
8          Mexico  MEX
9       Nicaragua  NIC
10    El Salvador  SLV
11            USA <NA>

Aaaand fail because "USA" is spelt out "United States" in the ISO3 codes. Otherwise you could loop over the ISO3 codes and call getData.

The full GADM download is 400Mb, from the GADM web site. Get that, and its probably easiest to read in using the sf package and use st_intersects or some other st_operation function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.