0
votes

I've like to create a data frame with the values of multi-band raster (rasters sl1 and sl2 with 4 layers in each raster) in 5 random coordinates (ptS), but unfortunately, my output was 10 values and I expected 80 values in my final data frame.

In my code I make:

library(raster)

# Example data
r <- raster(ncol=10, nrow=10)

# 10 layers
s <- stack(lapply(1:8, function(i) setValues(r, runif(ncell(r)))))

#Create two GeoTIFF with 4 layers 
sl1<-s[[1:4]]
writeRaster(sl1,filename=paste("sl1",sep=""),
                  format="GTiff",datatype="FLT4S",overwrite=TRUE)
sl2<-s[[5:8]]                  
writeRaster(sl2,filename=paste("sl2",sep=""),
                  format="GTiff",datatype="FLT4S",overwrite=TRUE)

#Read rasters in batch
f <- list.files(getwd(), pattern = ".tif") 
ras <- lapply(f,raster)

# Simulation of 5 random points in the rasters
pt<-rbind(coordinates(ras[[1]]),coordinates(ras[[1]]))
randomRows = function(df,n){
   return(df[sample(nrow(df),n),])
}
ptS<-randomRows(pt,5) 

# Extract raster values in random coordinates and create a data frame of the results
RES<-NULL
for(i in 1:length(ras)){
value <- raster::extract(ras[[i]],ptS)
dummy<-1:length(DF)
RES<-rbind(RES,cbind(ptS,paste(substr(f[1],1,3)),value,dummy))
}

str(RES)

 chr [1:10, 1:5] "-126" "126" "-126" "-90" "-126" "-126" "126" "-126" "-90" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "x" "y" "" "value" ...

head(RES)

     x      y           value               dummy
[1,] "-126" "63"  "sl1" "0.52498596906662"  "1"  
[2,] "126"  "63"  "sl1" "0.702012658119202" "2"  
[3,] "-126" "63"  "sl1" "0.52498596906662"  "3"  
[4,] "-90"  "-63" "sl1" "0.522934257984161" "4"  
[5,] "-126" "-81" "sl1" "0.115565001964569" "5"  
[6,] "-126" "63"  "sl1" "0.537986099720001" "1"  

The problem is that the extract function select values in just one layer and I need the information in all layers. Please, any ideas?

1
When you read the GeoTIFFs back in, you should call ras <- lapply(f, stack) because your current call ras <- lapply(f, raster) is only reading the first layer. That should solve the problem.qdread
Thanks very much!! So simple and works!!Isabel

1 Answers

1
votes

Example data

library(raster)  
r <- raster(ncol=10, nrow=10)
s <- stack(lapply(1:8, function(i) setValues(r, runif(ncell(r)))))
f1 <- file.path(tempdir(), "sl1.tif")
f2 <- file.path(tempdir(), "sl2.tif")
writeRaster(s[[1:4]], f1, overwrite=TRUE)
writeRaster(s[[5:8]], f2, overwrite=TRUE)
# 5 random points in the rasters
set.seed(5)
pts <- sampleRandom(s[[1]], 5, xy=TRUE)[,1:2]

You used raster where it should have been brick

f <- c(f1, f2)
ras <- lapply(f, brick)

Extract

lapply(ras, function(r) extract(r, pts))

#[[1]]
#         sl1.1     sl1.2     sl1.3     sl1.4
#[1,] 0.9118225 0.3683489 0.9461193 0.5610859
#[2,] 0.7260444 0.2175285 0.1937144 0.1064423
#[3,] 0.2299451 0.4175407 0.1246593 0.6523710
#[4,] 0.9659587 0.5460171 0.4254940 0.2420936
#[5,] 0.6640411 0.3803043 0.3480674 0.9805962
#
#[[2]]
#         sl2.1       sl2.2     sl2.3     sl2.4
#[1,] 0.4129051 0.689537048 0.6562566 0.6587621
#[2,] 0.4662205 0.004840697 0.3207286 0.8606737
#[3,] 0.3988470 0.609101892 0.6140796 0.9177927
#[4,] 0.1542479 0.030588239 0.8145537 0.5056544
#[5,] 0.4548225 0.917027473 0.2570022 0.4182261