[Working with R 3.2.2]

I am trying to extract climate mean values country by country from GADM and WorldCLim (www.worldclim.org). The code I am using is the following one:

#Install necessary packages

#Load necessary packages

#Set working directory

#Specify target ISO country code and path to downloaded shapefile
country_name <- "FIN"                         
shp          <- shapefile(".../srtm_country-master/srtm/tiles.shp")       #Path to SRTM Tiles (can be found in subfolder srtm)

[Data downloaded from: https://github.com/sikli/srtm_country]

#Get country geometry first
country <- getData("GADM", country = country_name, level=2)

#Intersect country geometry with tile grid
intersects <- gIntersects(country, shp, byid=T)
tiles      <- shp[intersects[,1],]

#Download tiles
srtm_list  <- list()
for(i in 1:length(tiles)) {
  lon <- extent(tiles[i,])[1]  + (extent(tiles[i,])[2] - extent(tiles[i,])[1]) / 2
  lat <- extent(tiles[i,])[3]  + (extent(tiles[i,])[4] - extent(tiles[i,])[3]) / 2

 tile <- getData('worldclim', var='bio', res=10, lon=lon, lat=lat)

 srtm_list[[i]] <- tile

#Create a list with all exported .bil files from the climate data folder
ras_list <- list.files(".../wc10",full.names=TRUE, pattern=".bil$")

#Load raster mosaic into R 
climatemosaic <- stack(ras_list)

#Extract mean values 
assign(paste0("FIN_means"), extract(climatemosaic, country, fun=mean, na.rm=TRUE, small=TRUE, layer=1, nl=19, sp=TRUE))

#Write data.frame that keeps the needed variables
dataframe <- as.data.frame(FIN_means)

It seems to work for all the countries except FINLAND. Therefore, my guess is that the problem comes from the data from Finland. I have been trying and it does work for administrative level 0, but not for level 1 or 2 (which looks weird to me). Actually, the error message that it returns is:

Error in extent(tiles[i, ]) : error in evaluating the argument 'x' in selecting a method for function 'extent': Error in tiles[i, ] : no es posible obtener un slot ("Polygons") de un objeto de tipo "NULL"

Does anybody know if this is because the data from Finland is just available at a country level (administrative level 0)?

  • Hi @Marina nice question but the code is difficult to reproduce, as you refer to local files on your computer. Can you specify what is the download link for the srtm/tiles.shp? Ideally add a line to download automatically? Also, usually no need to have the install.package() lines, the rm() one is quite dangerous, and linking to local libraries (dplyr) won't work for anyone else :-) – Matifou Feb 28 '17 at 18:44

I downloaded the GADM FIN data manually, plus your SRTM tiles from Github, and opened them in QGIS - they don't overlap, except for a few of Finland's coastal islands in the far south. Since level 0 is a multipolygon, you'd get a result from your code, but any level below that would produce at least one null result with gIntersect. Finland is probably just the first country your code has failed out on, I would think the same thing will happen with, say, Sweden.


R is great, but it pays to check this stuff in a real GIS :) Your choices now are to a) add a couple of rows to your tile dataset or b) write some code to handle a negative gIntersect result.

  • Hi @obrl_soil! I appreciate a lot your comment. As you said, I found also problems with other countries (Iceland). Can I ask you if you can give me any hint about where I can find these additional commands to make my code working well? Thank you very much in advance! – Marina Mar 7 '17 at 11:27

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.