[R-sig-Geo] Problem with extract {raster} function

Kalomenopoulos, Manos manos.kalomenopoulos at wur.nl
Mon May 20 20:25:50 CEST 2013


Hi everybody,
I am experiencing a problem with the extract{raster} function, specifically the 'weights' argument.

I have a raster and a polygon shapefile with 5 polygons (land cover classes).
I am using extract to obtain cellnumbers and the weights (percentage of cover of each class per raster cell). 
The sum of the five weights per cell should sum to 100% (except for those cells not completely inside my AOI). 
However, there is an considerable amount of cells were that is not the case.
I have been trying to track down the error, but can't understand it so far.

I localized the erroneous cells in one example site (10x10km) and clipped the original shapefile to a smaller area containing these cells.
The script below does the extraction once for the 10x10km sample and once for a smaller testsite containing 10 erroneous cells.
Surprisingly, doing the extraction with the smaller testsite ALL weights sum up to 100%, even though the polygon data is exactly the same.... 
Any ideas why and/or suggestions how to make it work properly for the bigger extent?

Here my script:


library(raster)
library(reshape)

# raster of AOI
 r <- raster('1site.tif')
# polygon shapefile of AOI with 5 different classes
  s1pol <- shapefile('1site.shp')

# polygon shapefile of test area with observed errors (clipped from s1pol)
  s2pol <- shapefile('test.shp')
# extract class names
  classes <- s1pol$class
########################################################################
# Extraction for complete AOI
  df1 <- extract(r,s1pol, df=TRUE, weights=TRUE, cellnumbers=TRUE)
    names(df1) <- c('ID','cell', 'r', 'weight1') # Extraction for test area
  
# Extraction for smaller site
  df2 <- extract(r,s2pol, df=TRUE, weights=TRUE, cellnumbers=TRUE)
    names(df2) <- c('ID','cell', 'r', 'weight2') ###########################################################################################
### Compairing

# Reshape df's keeping only ID, cell and weight
  df1new<-cast(df1[,c(1,2,4)],cell~ID)
    names(df1new) <- c('cell',classes)
  
  df2new<-cast(df2[,c(1,2,4)],cell~ID)
    names(df2new) <- c('cell',classes)
# Calculate class totals      
  df1new$totals <- mapply(df1new[,2], df1new[,3], df1new[,4],df1new[,5],df1new[,6], FUN=function(v,w,x,y,z) sum(c(v,w,x,y,z),na.rm=TRUE))
       
  df2new$totals <- mapply(df2new[,2], df2new[,3], df2new[,4],df2new[,5],df2new[,6], FUN=function(v,w,x,y,z) sum(c(v,w,x,y,z),na.rm=TRUE))

# Merge the two dataframes and select only rows with differnt totals
  df <- as.data.frame(merge(df1new,df2new,by='cell'))
    
    dfe <- subset(df, totals.x-totals.y !=0)
      dfe$error <- dfe$totals.x-dfe$totals.y

head(dfe)

Link to the files needed to run this script: 
https://www.dropbox.com/s/n283rwew1ypxghv/extractProblem.7z


Thanks in advance!
-------------------------------------------------------
Manos Kalomenopoulos
Lab. of Geo-Information Science and Remote Sensing, Center for Geo-Information - Wageningen University Department for Biogeochemical Integration, Max-Planck-Institute for Biogeochemistry, Jena



More information about the R-sig-Geo mailing list