0

I have 2 raster images of different spatial resolution and projection.

I want to resample multiple coarser images (0.25 degrees) to finer resolution(4 km) based on one finer resolution image which is of 4 km. Both are in different folders. I am not getting how to do that.

I had written the code in R but I am getting an error.

library(resample)
>setwd("D:/mtech project/data/DEC 16/RH/1-DEC-16/extracted/")
>inFiles1 <- list.files(pattern="*.tif")
> nFiles1 <-  length(inFiles1)
> setwd("D:/mtech project/data/extracted/")
> inFiles2 <- raster("3DIMG_01DEC2016_0000_L2C_FOG_FOG.tif")
> for (i in 1:nFiles1) {
+     r1<-raster(inFiles1[[i]])    
+     r2<-inFiles2    
+     rs<-resample(r1,r2,method="bilinear")
+     write.Raster(rs, paste0('D:/mtech project/data/DEC 16/RH/RESAMPLED/rs_',i,'.img'), overwrite=T)
+   }

Error in .local(.Object, ...) : 

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  :   Cannot create a RasterLayer object from this file. (file does not exist)

`

1
  • Does your file exist? Perhaps it needs to be "D://mtech/..." ? Nov 24, 2018 at 3:37

2 Answers 2

1

Building on Norwin's answer, you can store the path for the two separate files as separate variables and then paste them together to form full file names as you make the rasters: raster(paste0(pathname, "filename.tif"))

    >setwd("D:/mtech project/data/DEC 16/RH/1-DEC-16/extracted/")
    >path1 = getwd() #make the file path into a variable

    > inFiles1 <- list.files(pattern="*.tif")
    > nFiles1 <-  length(inFiles1)

    > setwd("D:/mtech project/data/extracted/")
    > path2 = getwd() # make the other file path into a variable

    > inFiles2 <- raster(paste0(path2, "3DIMG_01DEC2016_0000_L2C_FOG_FOG.tif")) #combine path and file name

    > for (i in 1:nFiles1) {
    +     r1<-raster(paste0(path1, inFiles1[[i]])) #combine path name and file name
    +     r2<-inFiles2    
    +     rs<-resample(r1,r2,method="bilinear")
    +     write.Raster(rs, paste0('D:/mtech project/data/DEC 16/RH/RESAMPLED/rs_',i,'.img'), overwrite=T)
    +   }
1

You changed your working directory, after creating the list of inputfiles inFiles1. As a result, the relative path to the files is not valid anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.