'Problems with a for loop using writeRaster
I am trying to create a for loop to merge 4 raster (EVI MODIS tiles) at a time (because the 4 raster create one big map) in a list of 8 raster (my real list is much more bigger but I am doing a test), then crop the whole map and finally write (writeRaster) the result in the same folder. This is the code I am using but I dont know if I am doing the for loop correctly:
library(raster)
library(gdalUtils)
library(rgdal)
library("RStoolbox")
setwd("D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder")
files <- list.files(pattern=".tif$",full.names = FALSE)
vector <- c(files[1:4], files[5:8])
for (i in vector)
{
x <- files
x1 <- (raster(x[1])/10000) # we create a raster for each object in the vector "x" (files) to merge them
x2 <- (raster(x[2])/10000)
x3 <- (raster(x[3])/10000)
x4 <- (raster(x[4])/10000)
enlist <- list(x1,x2,x3,x4)
y <- do.call(merge, enlist)
cropped <- crop(y, size) # we crop the new full map (4 tiles) with the object "size" (polygon)
writeRaster(cropped, filename = paste0("EVI_crop_", x)) # we extract the new tif
I have to say that the first raster is created correctly but an error appears for the second:
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file. In addition: There were 35 warnings (use warnings() to see them)
warnings() Warning messages: 1: In if (filename == "") { ... : the condition has length > 1 and only the first element will be used 2: In if (!file.exists(dirname(filename))) { ... : the condition has
length > 1 and only the first element will be used 3: In if (nchar(ext) < 3) { ... : the condition has length > 1 and only the first element will be used 4: In if (ext == ".tif" | ext == ".tiff") {... : the condition has length > 1 and only the first element will be used 5: In if (filename == "") { ... : the condition has length > 1 and only the first element will be used 6: In if (file.exists(filename)) { ... : the condition has length > 1 and only the first element will be used 7: In if (nchar(filename) == 0) stop("empty file name") : the condition has length > 1 and only the first element will be used 8: In if (x == "" | x == ".") { ... :
the condition has length > 1 and only the first element will be used 9: In if (!start %in% c("htt", "ftp")) { ... : the condition has length > 1 and only the first element will be used 10: In if (fileext %in% c(".GRD", ".GRI")) { ... : the condition has length > 1 and only the first element will be used 11: In if (!file.exists(x)) { ... : the condition has length > 1 and only the first element will be used 12: In if ((fileext %in% c(".HE5", ".NC", ".NCF", ".NC4", ... : the condition has length > 1 and only the first element will be used 13: In if (fileext == ".GRD") { ... : the condition has length > 1 and only the first element will be used 14: In if (fileext %in% c(".BIN")) { ... : the condition has length > 1 and only the first element will be used 15: In if (fileext == ".DOC") { ... : the condition has length > 1 and only the first element will be used 16: In if (fileext %in% c(".SGRD", ".SDAT")) { ... : the condition has length > 1 and only the first element will be used 17: In if (nchar(filename) == 0) stop("empty file name") : the condition has length > 1 and only the first element will be used 18: In if (file.exists(file)) { ... : the condition has length > 1 and only the first element will be used 19: In normalizePath(path.expand(path), winslash, mustWork) :
path[2]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000065.h09v09.006.2015136022455.tif": The system cannot find the file specified 20: In normalizePath(path.expand(path), winslash, mustWork) :
path[3]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000065.h10v08.006.2015136022544.tif": The system cannot find the file specified 21: In normalizePath(path.expand(path), winslash, mustWork) :
path[4]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000065.h10v09.006.2015136022526.tif": The system cannot find the file specified 22: In normalizePath(path.expand(path), winslash, mustWork) :
path[5]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h09v08.006.2015136040620.tif": The system cannot find the file specified 23: In normalizePath(path.expand(path), winslash, mustWork) :
path[6]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h09v09.006.2015136040620.tif": The system cannot find the file specified 24: In normalizePath(path.expand(path), winslash, mustWork) :
path[7]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h10v08.006.2015136040049.tif": The system cannot find the file specified 25: In normalizePath(path.expand(path), winslash, mustWork) :
path[8]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h10v09.006.2015136040705.tif": The system cannot find the file specified 26: In if (nchar(filename) == 0) stop("empty file name") : the condition has length > 1 and only the first element will be used 27: In if (file.exists(file)) { ... : the condition has length > 1 and only the first element will be used 28: In normalizePath(path.expand(path), winslash, mustWork) :
path[2]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000065.h09v09.006.2015136022455.tif": The system cannot find the file specified 29: In normalizePath(path.expand(path), winslash, mustWork) :
path[3]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000065.h10v08.006.2015136022544.tif": The system cannot find the file specified 30: In normalizePath(path.expand(path), winslash, mustWork) :
path[4]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000065.h10v09.006.2015136022526.tif": The system cannot find the file specified 31: In normalizePath(path.expand(path), winslash, mustWork) :
path[5]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h09v08.006.2015136040620.tif": The system cannot find the file specified 32: In normalizePath(path.expand(path), winslash, mustWork) :
path[6]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h09v09.006.2015136040620.tif": The system cannot find the file specified 33: In normalizePath(path.expand(path), winslash, mustWork) :
path[7]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h10v08.006.2015136040049.tif": The system cannot find the file specified 34: In normalizePath(path.expand(path), winslash, mustWork) :
path[8]="D:/MODIS/Tesis/Ecuador_2000-2019/HDF-Terra/EVI_terra/New folder/New folder/EVI_crop_MOD13Q1.A2000081.h10v09.006.2015136040705.tif": The system cannot find the file specified 35: In if (!file.exists(x)) { ... : the condition has length > 1 and only the first element will be used
I am new in R but understand for loops had been the most difficult for me. Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|