I have created a script in R using the RSToolbox and raster packages in R to process multiband images from Landsat 7 and 8. The code works fine and it is giving me the outputs I need; however, due to the amount of data contained in these images, it takes a long time to run (~30-40 minutes per image and I have nearly 200 images to analyse).
To reduce the analysis time, I tried cropping the image to just the area I am interested in, which worked really well, but after a couple of calculations, the image becomes over 1GB in size and an error comes up saying that R is unable to process an image that size. I then noticed that when using the crop function, the image becomes about 10 times larger than the original one, even though it is cropped to about half its original spatial extent. I am not really sure why this is the case.
The image was cropped using the following code:
library(raster) cropMB = crop(MB,RioDoce)
where MB is the stacked layer of a Landsat 7 or 8 image, and RioDoce is a point vector representing the path of a river (uploaded to R as a shapefile).
Landsat images were stacked using the following code:
library(RSToolbox) meta = readMeta("filepath to Landsat image metadata") MB = stackMeta(meta)
Would anyone here be able to suggest a way to decrease the time of the analysis by decreasing the size of the original image? For reference, I am doing the following with each set of images:
- Create a raster stack containing all bands
- Transform green and NIR bands from DN to TOA
- Calculate NDWI using the previously mentioned bands
- Apply two masks to original raster stack so only areas with water bodies and no clouds remain
- Transformation of DN values to at-surface-reflectance of the masked raster stack
- Extract spectral band values to a point layer representing the path of a river
How can I reduce the analysis time?