I have a set of raster images (GeoTIFF, Landsat 1984-2018) which I cropped with my AOI using a shapefile.
Now I want to stack them and calculate the mean and standard deviation of every pixel of the stacked images (in space and time). How can I stack them and calculate the mean and standard deviation? For the outputfile I need one image for the mean and one for the standard deviation.
library(raster)
library(rgdal)
#load data
setwd("C:/Users/cathe/Documents/GEOTIFF_offsetcorrected")
dbase = "C:/Users/cathe/Documents/GEOTIFF_offsetcorrected"
#polygon with crop-extend
shape_data <- readOGR("C:/Users/cathe/OneDrive/Documents/ArcGIS/Projects/Cologne/WGS_1984_UTM_Zone_32N/Cologne.shp", stringsAsFactors=FALSE)
#load tif files
ALL_FILES <- list.files(path = dbase, pattern = "*\\.tif$|*.TIF$")
#output
outfiles = file.path("C:/Users/cathe/Documents/Output",
paste0(basename(tools::file_path_sans_ext(ALL_FILES)),
".tif"))
#crop
for (i in seq_along(ALL_FILES)) {
r = crop(stack(ALL_FILES[i]), shape_data)
writeRaster(r, filename=outfiles[i],
bylayer=FALSE,
format="GTiff",
datatype= "INT1U",
options="COMPRESS=ZIP",
overwrite=TRUE)
}
mean <- mean(r)
plot(mean)
writeRaster(x = mean, filename = "mean1.tif", driver = "GeoTiff", overwrite=TRUE)
I get a result, but I am not sure if it is correct?
mean(r1,r2,r3,r4)
?mean
is the mean of the 77th pixel of the input rasters? Create some rasters with known values in a folder as a test set.