[R-sig-Geo] Converting large RasterStack to CSVs fast
Mohammad Abdel-Razek
kimofos at yahoo.com
Fri May 15 11:43:59 CEST 2015
Hi
I got a function to convert ndvi raster stack to CSVs. Each stack is divided into 100 subset, which is convert to csv. The code works for small raster stack, for large ones, I cannot load them into the memory, then it takes massive time to do the task.
is there a better way to do it?
The code is below:
require(gdal)
require(raster)
exportCSV <- function () {
tif <- list.files(pattern='NDVI.tif$')
wd <- getwd()
ModisTile<- substr(wd, nchar(wd)-5, nchar(wd))
nImages <- length(tif)
cat(paste("Stacking images ...", "\n"))
s <- stack(tif)
cat(paste("Loading values to RAM memory ...", "\n"))
#this step is skipped in case of large stacks, then it takes very long time
s <- readAll(s)
#create the subsets bounding coordinates
borders <- extent(s)
Xmin <- borders at xmin
Xmax <- borders at xmax
Ymin <- borders at ymin
Ymax <- borders at ymax
xIncreament <-(Xmax-Xmin)/10
yIncreament <-(Ymax-Ymin)/10
cat(paste("Subsetting and writing NDVI values ...", "\n"))
for (i in 1:10) {
for (j in 1:10) {
clip_xmin <- Xmin + xIncreament*(i-1)
clip_xmax <- Xmin + xIncreament*i
clip_ymin <- Ymin + yIncreament*(j-1)
clip_ymax <- Ymin + yIncreament*j
c_xmin <- format(round(clip_xmin,6), nsmall=6)
c_xmax <- format(round(clip_xmax,6), nsmall=6)
c_ymin <- format(round(clip_ymin,6), nsmall=6)
c_ymax <- format(round(clip_ymax,6), nsmall=6)
subset <- extent(c(clip_xmin, clip_xmax, clip_ymin, clip_ymax))
c <- crop(s, subset)
p <- as.data.frame(rasterToPoints(c))
csvName <- paste0(ModisTile, "_Xmin_",c_xmin, "_Xmax_",c_xmax, "_Ymin_",c_ymin, "_Ymax_",c_ymax,".csv")
cat(paste("Writing Subset... MOIDS Tile:", ModisTile,", X", i, "Y", j, "\n"))
write.table(p, csvName, row.names=F, sep=";", dec=".")
}
}
}
Best,
Mohammad PhD Candidate Institute of Crop Science and Resource Protection - Crop Science Research Group
Katzenburgweg 5 - 53115 Bonn - Germany
Tel.: +49 (0) 228 73 3258 Fax: +49 (0) 228 73 2870
abdelrazek at uni-bonn.de http://www.lap.uni-bonn.de
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list