Search code examples
rgisaggregateraster

open raster in R! and some statistical operation


I would like to open raster files (in ASCII or TIFF format), aggregate their cells and after this operation count correlation between values in this new raster file and another one. Unfortunately I do not know what is wrong in my commands - I get an error message:

x <- GDAL.open('~/Pulpit/dods/karol/TVDI 113_121/TVDI_kamp_evi_TRANSF.asc') 

CPL ERROR 4: `~/Pulpit/dods/karol/TVDI 113_121/TVDI_kamp_evi_TRANSF.asc' does not exist in the file system, and is not recognised as a supported dataset name.

Error in .local(.Object, ...) : `~/Pulpit/dods/karol/TVDI 113_121/TVDI_kamp_evi_TRANSF.asc' does not exist in the file system, and is not recognised as a supported dataset name.


Solution

  • If you are having trouble getting the filenames, you might do this:

    my_asc_files = dir("../somepath", pattern="*.asc", recursive=T, full.names=T)
    files_I_want = my_asc_files[c(1,12,32,33)]
    

    Then you can load your files like this

    library(raster)
    my_rasters = lapply(files_I_want, raster)
    

    Then you may do this:

    pairs(my_rasters) 
    

    and this:

    for(i in 1:length(my_rasters)) 
      for(j in i:length(my_rasters))   
        if(i != j) {
          df = na.omit(data.frame(values(my_rasters[[i]]), values(my_rasters[[j]])))
          cor(df[,1], df[,2])
        }
    

    Although you will run into problems if the rasters are so large that you cannot hold two in memory at the same time. Without a better question it will be hard to give you better advice.