I have several raster layers so I created a function that performs some analysis but it changes the names of the exported files. For example, for one of the rasters I have, I want the names of the files to be pop020.tif, pop040.tif etc, that, the 3 digit number to be right after the name of file.
Instead, the function moves the 3 digit number like pop.tif020. How can I place the 3 digit number to be right after the name of the file?
library(terra)
wd = "C:/Users/nikos/OneDrive/Desktop/psf_paris2/"
# read the rasters
pop = rast(paste0(wd, "pop.tif"))
doStuff <- function(file){
pic = rast(file)
for (i in seq(from = 0.2, to = 0.8, by = 0.2)) {
print(i)
gf <- focalMat(pic, i * 400, "Gauss")
r_gf <- focal(pic, w = gf, na.rm = TRUE)
r_gf = aggregate(r_gf, fact = 4, fun = "mean", cores = 8)
r_gf <- crop(r_gf, ext(v))
r_gf <- mask(r_gf, v)
stringedi = gsub("\\.", "", toString(format(i, nsmall = 2)))
writeRaster(r_gf,
paste0("C:/Users/nikos/OneDrive/Desktop/psf_paris2/", basename(file),
stringedi, ".tif"),
overwrite=TRUE)
}
}
list.files(wd, pattern = "tif$", full.names = TRUE) |>
purrr::walk(doStuff)
One raster as an example:
pop = raster(new("RasterLayer", file = new(".RasterFile", name = "C:\\Users\\Geography\\Desktop\\focal\\pop.tif",
datanotation = "FLT4S", byteorder = "little", nodatavalue = -Inf,
NAchanged = FALSE, nbands = 1L, bandorder = "BIL", offset = 0L,
toptobottom = TRUE, blockrows = c(rows = 5L), blockcols = c(cols = 358L),
driver = "gdal", open = FALSE), data = new(".SingleLayerData",
values = logical(0), offset = 0, gain = 1, inmemory = FALSE,
fromdisk = TRUE, isfactor = FALSE, attributes = list(), haveminmax = TRUE,
min = 0.43411433696747, max = 355.74725341797, band = 1L,
unit = "", names = "pop"), legend = new(".RasterLegend",
type = character(0), values = logical(0), color = logical(0),
names = logical(0), colortable = logical(0)), title = character(0),
extent = new("Extent", xmin = 165700, xmax = 201500, ymin = 5735500,
ymax = 5769600), rotated = FALSE, rotation = new(".Rotation",
geotrans = numeric(0), transfun = function ()
NULL), ncols = 358L, nrows = 341L, crs = new("CRS", projargs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"),
srs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs",
history = list(), z = list()))
To convert the raster
to spatraster
:
pop = rast(pop)