1

I am trying to write a series of raster files into a new folder (after doing some image processing). However, when using my code, the files are but are not in the folder that I have created for that purpose.

I say they are but they are not because when I run my code and take a look inside the folder that I have created, a file pops-up, disappears, pops-up, disappears and this goes until I hit stop the R console.

Here is my code

Img.Raster <- list()
for (k in 1:length(list.pics)){
  Img.Raster[[k]] <- raster(list.pics[k])
  extent(Img.Raster[[k]])=c(coordinate.one.Image$CenterLeftx[[k]], coordinate.one.Image$CenterRightx[[k]],
                            coordinate.one.Image$CenterBottomy[[k]], coordinate.one.Image$CenterTopy[[k]])
  proj4string(Img.Raster[[k]]) <- "+proj=utm +zone=15 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
  writeRaster(Img.Raster[[k]],filename =paste0("/Users/Juan/Desktop/Images_Folder/"
                                                , "Img_",names(list.pics[[k]])), format="GTiff",overwrite=TRUE)
}

list.Image is a variable that contains 1745 images

Those anyone know what could be wrong?

Suggestion by @Jul

for (k in 1:length(list.pics)){
  Img.Raster[[k]] <- raster(list.pics[k])
  extent(Img.Raster[[k]])=c(coordinate.one.Image$CenterLeftx[[k]], coordinate.one.Image$CenterRightx[[k]],
                            coordinate.one.Image$CenterBottomy[[k]], coordinate.one.Image$CenterTopy[[k]])
  proj4string(Img.Raster[[k]]) <- "+proj=utm +zone=15 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
  writeRaster(Img.Raster[[k]],filename =paste0("/Users/Juan/Desktop/Images_Folder/"
                                                , "Img_",stringr::str_pad(string = k,width = 4,side = "left",pad=0),
                                               format="GTiff",overwrite=T)
}

1 Answer 1

1

Try changing the names(list.pics[[k]]) to names(list.pics[k]).

I think you want the names of each element in the list (presumably 1745 unique names in total), not the names within each raster element, which I'd guess are the same for every element. The appearing/disappearing behaviour is probably because every file is being given the same filename and you've set overwrite=TRUE. It will probably throw a 'file already exists' error without the overwrite argument.

EDIT after comment thread: Please ensure you are passing unique filenames to the writeRaster call. Swapping names(list.pics[k]) for stringr::str_pad(string = k,width = 4,side = "left",pad=0) should work.

Suggestion

for (k in 1:length(list.pics)){
  Img.Raster[[k]] <- raster(list.pics[k])
  extent(Img.Raster[[k]])=c(coordinate.one.Image$CenterLeftx[[k]], coordinate.one.Image$CenterRightx[[k]],
                            coordinate.one.Image$CenterBottomy[[k]], coordinate.one.Image$CenterTopy[[k]])
  proj4string(Img.Raster[[k]]) <- "+proj=utm +zone=15 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
  writeRaster(Img.Raster[[k]],filename =paste0("/Users/Juan/Desktop/Images_Folder/", "Img_",stringr::str_pad(string = k,width = 4,side = "left",pad=0)),format="GTiff",overwrite=TRUE)
}
11
  • Thank you for your response; unfortunately, the appearing and disappearing behavior is still happening after making the changes that you suggested.
    – Perro
    Nov 9, 2018 at 4:32
  • Are you sure you are piping in unique names? What happens if you set overwrite=F?
    – Jul
    Nov 9, 2018 at 4:36
  • By changing overwrite= F I get the following error Error in .getGDALtransient(x, filename = filename, options = options, : filename exists; use overwrite=TRUE
    – Perro
    Nov 9, 2018 at 5:03
  • Then the filename you are creating each time is the same, meaning all of the elements in list.pics have the exact same name. If you use stringr::str_pad(string = k,width = 4,side = "left",pad=0) in place of names(list.pics[k]), then it should work.
    – Jul
    Nov 9, 2018 at 5:55
  • I am not sure if I understand what you are suggesting me; however, I have added to the original post, what I believe you are telling me to do. By running that I still don't get all my files in the folder, then again I am not sure if that is what you are suggesting me.
    – Perro
    Nov 9, 2018 at 6:05

Your Answer

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

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