Save multi layer RasterBrick to harddisk - r

I have a multilayer RasterBrick representing a topographic map that I want to save to the harddisk as grd or tif format, so that others can work with later.
This is the RasterBrick:
class : RasterBrick
dimensions : 2400, 4200, 10080000, 3 (nrow, ncol, ncell, nlayers)
resolution : 100, 100 (x, y)
extent : 480000, 9e+05, 62000, 302000 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : in memory
names : layer.1, layer.2, layer.3
min values : 2.8725, 2.8725, 2.8725
max values : 254.5175, 254.5175, 254.5175
I tried to save it with this command:
outfile <- writeRaster(brick, filename='grid.tif', format="GTiff", overwrite=TRUE)
and this:
outfile <- writeRaster(m, filename='grid.grd', format="raster", overwrite=TRUE)
But the tif file is corrupt and the grd object only contains one layer and is not recognized as multi layer RasterBrick when I read it back in using raster().
The aim is to use the topographic map as background for thematic maps.

Try this:
outfile <- writeRaster(brick, filename='grid.tif', format="GTiff", overwrite=TRUE,options=c("INTERLEAVE=BAND","COMPRESS=LZW"))

Related

Save raster to USGS DEM Format in R

Similar to this question:
I would like to know how to do the reverse and save an .img raster image into a USGS DEM format.
Based on GDAL docs, it seems like it would be possible but when I run rgdal::getGDALDriverNames() in R I get the following:
name long_name create copy isRaster
139 USGSDEM USGS Optional ASCII DEM (and CDED) FALSE TRUE TRUE
which seems to imply that it won't create these files?
I was hoping to do something like:
library(raster)
# read
img <- raster("Raster_100ft_2022_10_18.img")
# convert to DEM
writeRaster(img, 'test.dem')
But raster doesn't seem to recognize that output format.
Is there some other method to save as USGS DEM files?
Thanks
For me it works with terra. If that's proper "USGSDEM" file, that's another question. From gdal reference it should save the file as well: https://gdal.org/drivers/raster/usgsdem.html
f <- system.file("ex/elev.tif", package="terra")
r <- terra::rast(f)
terra::writeRaster(r, filename = "test.dem", filetype = "USGSDEM", overwrite = TRUE)
raster::raster("test.dem")
#> class : RasterLayer
#> dimensions : 90, 95, 8550 (nrow, ncol, ncell)
#> resolution : 0.008333333, 0.008333333 (x, y)
#> extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax)
#> crs : +proj=longlat +datum=WGS84 +no_defs
#> source : test.dem
#> names : elevation
#> values : 141, 547 (min, max)
Created on 2022-10-20 with reprex v2.0.2

Extraction of data from multiple netcdf files at five coordinates files and writing them to five separate csv files

I have 365 .nc files located in a folder containing daily soil moisture information. I want to extract data at five-coordinate locations for the whole year and write them into five separate csv files. My code is attached below. However, I am getting this error after the line:
s <- stack(ff)
>Error in if (is.na(get("has_proj_def.dat", envir = .RGDAL_CACHE))) { : argument is of length zero In addition: Warning message: In .varName(nc, varname, warn = warn) : varname used is: sm If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor
No idea how to proceed further.
library(raster)
library(ncdf4)
ptf <- "D://SMOS_ECV_SM//SMOS_ECV_SM//ECV_SM_Data_1978_2010//1978"
ff <- list.files(path=ptf, pattern="[.]nc$", full.names=TRUE)
s <- stack(ff)
points <- rbind(c(0,1), c(100,120), c(80,5), c(85,4), c(82,4))
v <- extract(s, points)
for (i in 1:ncol(v)) {
write.csv(v[,i,drop=FALSE], paste0("file", i, ".csv"))
}
library(raster)
#Loading required package: sp
f <- list.files("try", full=T)
First try for a single file
r <- raster(f[1])
#Loading required namespace: ncdf4
#Warning message:
#In .varName(nc, varname, warn = warn) : varname used is: sm
#If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor
To get rid of the warning:
r <- raster(f[1], varname="sm")
Now for all files
s <- stack(f, varname="sm")
s
#class : RasterStack
#dimensions : 720, 1440, 1036800, 2 (nrow, ncol, ncell, nlayers)
#resolution : 0.25, 0.25 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#names : Soil.Moisture.1, Soil.Moisture.2
Extract values
points <- rbind(c(-96.7, 47), c(34.55, 54.85))
v <- extract(s, points)
v
# Soil.Moisture.1 Soil.Moisture.2
#[1,] 0.3254 0.3018
#[2,] 0.3386 0.3386

Error using writeRaster on a large RasterStack

I have a RasterStack in R called "preds2" that is 4.1 GB and was outputted from 4 RasterStacks and 2 RasterLayers (wveg, wfps_lag, wfps, ndvi, swt, lu):
cl <- makeCluster(4)
registerDoSNOW(cl)
preds<-foreach(j = 1:nlayers(ndvi))%dopar%{
library(raster)
library(SpaDES)
time <- stack(wveg,wfps_lag[[j]],wfps[[j]],ndvi[[j]],swt[[j]],lu)
names(time) <- c('wveg','wfps_lag','wfps','ndvi','swt','lu')
exp(raster::predict(time,m,const=fact_table,exclude=c("s(wlch)","s(wetid)")))
}
stopCluster(cl)
preds2<-stack(preds)
> preds2
class : RasterStack
dimensions : 6617, 11771, 77888707, 27 (nrow, ncol, ncell, nlayers)
resolution : 0.0002694946, 0.0002694946 (x, y)
extent : -95.69591, -92.52369, 41.71803, 43.50128 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
names : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10, layer.11, layer.12, layer.13, layer.14, layer.15, ...
min values : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
max values : 0.001754114, 0.001754114, 0.001754114, 0.001754114, 0.001730909, 0.001601078, 0.081421784, 3.510447853, 5.134697329, 7.547881571, 10.945457688, 13.332227330, 14.864517447, 16.708383138, 16.631466329, ...
I am trying to write this RasterStack to a .tif file but get an error:
> writeRaster(stack(preds3), filename = "C:\\Users\\RL\\Documents\\preds.tif", options="INTERLEAVE=BAND", overwrite=TRUE)
Error in file(fn, "rb") : cannot open the connection
In addition: Warning message:
In file(fn, "rb") :
cannot open file 'C:\Users\RL\AppData\Local\Temp\RtmpeegnBN\raster\r_tmp_2020-04-25_004109_2364_37231.gri': No such file or directory
Same error for using calc on the object "preds2" as well. I've created much smaller RasterStacks before with this code without any problem. Online blogs and documentation suggest this error may be due to it being such a large RasterStack (e.g. suggestion on storing "preds2" as a rasterTmpFile but I still get the same error when reading the temp file). Suggestions with code (as I'm new to R) would be appreciated. Thanks!
Figured it out -- the problem was that the RasterStack was too large to be stored in R's working memory and was insteaad saved to a temporary file on my computer that, for whatever reason, I couldn't find or access in R. So I just specified the temporary working directory.
options(rasterTmpDir = "C:\\Users\\RL\\Temp_R_folder")

Extracting all individual layers from a Raster Brick File

I have stacked 28 layers to a brick in R
brik
class : RasterBrick
dimensions : 720, 1440, 1036800, 28 (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
source : C:/Users/Ujjal Baruah/AppData/Local/Temp/Rtmp0GaiPO/raster/r_tmp_2020-01-03_030159_46788_10398.grd
names : Data.Fiel//tNO2Trop.1, Data.Fiel//tNO2Trop.2, Data.Fiel//tNO2Trop.3, Data.Fiel//tNO2Trop.4, Data.Fiel//tNO2Trop.5, Data.Fiel//tNO2Trop.6, Data.Fiel//tNO2Trop.7, Data.Fiel//tNO2Trop.8, Data.Fiel//tNO2Trop.9, Data.Fiel//NO2Trop.10, Data.Fiel//NO2Trop.11, Data.Fiel//NO2Trop.12, Data.Fiel//NO2Trop.13, Data.Fiel//NO2Trop.14, Data.Fiel//NO2Trop.15, ...
Now, i want to save this individual layers in Geotiff using
writeRaster(brik, file.path('/output/filepath/', names(brik)), bylayer=TRUE, format('GTiff'))
Unfortunately, i get just one file instead of multiple layers in geotiff.
Any solution would be appreciated.
Thanks
writeRaster seems to strip off the dot-number before creating a raster file. Hence it tries to write your layers all to Data.Fiel//tNO2Trop.tif.
> writeRaster(r, "./test.2", format="GTiff")
> dir(".")
[1] "test.tif"
(Note for some reason your code has format("GTiff") for format="GTiff". This works by the fluke that format is a function and returns the string "GTiff" and writeRaster is expecting the format string here)
I don't know why and I don't know if this is documented or a bug. You can work round by using dashes instead of dots:
> writeRaster(r, "./test-2", format="GTiff")
> dir(".")
[1] "test-2.tif" "test.tif"
and if dots are important to you then do a file.rename afterwards.
Edit: If you add the .tif to the file names then all is well:
> writeRaster(s, names(s), bylayer=TRUE, format="GTiff")
Error in .getGDALtransient(x, filename = filename, options = options, :
filename exists; use overwrite=TRUE
fails on the second layer because dot-number is stripped:
> dir()
[1] "layer.tif"
add .tif to the names:
> writeRaster(s, paste0(names(s),".tif"), bylayer=TRUE, format="GTiff")
shazam:
> dir()
[1] "layer.1.tif" "layer.2.tif" "layer.3.tif" "layer.tif"

Using greenbrown package with Landsat data

I have been trying to use the 'PhenologyRaster' function of the greenbrown package to model the growing season of my study area. However, everytime I run the function, I get empty outputs (e.g. the SOS.2016 layer will show as NA). My question is the following: am I having issues because I am running the function on one single year of data, or because Landsat time series are somewhat irregular (i.e. frequency of ~30 scenes per year)?
I am using the following piece of code to run the PhenologyRatser function:
PhenoTest = PhenologyRaster(landsat2016,start=c(2016,1,3),end=c(2016,12,20),freq=24,approach="Deriv",min.mean=-0.5,tsgf='TSGFspline',interpolate=TRUE)
The function is applied on a raster stack with the following characteristics:
class : RasterBrick
dimensions : 526, 591, 310866, 18 (nrow, ncol, ncell, nlayers)
resolution : 30, 30 (x, y)
extent : 604965, 622695, 4208175, 4223955 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=10 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : X2016.01.03, X2016.01.19, X2016.02.04, X2016.03.07, X2016.03.23, X2016.04.24, X2016.05.10, X2016.05.26, X2016.06.27, X2016.07.13, X2016.07.29, X2016.08.14, X2016.08.30, X2016.09.15, X2016.10.01, ...
min values : -0.1964, NA, -0.5382, NA, -0.4696, -0.2197, -0.2803, -0.4274, -0.4827, -0.2631, -0.5256, -0.4856, -0.5631, -0.3204, -0.5512, ...
max values : 0.1714, NA, 0.2425, NA, 0.2061, 0.5173, 0.4583, 0.2470, 0.3629, 0.5165, 0.2981, 0.2802, 1.6199, 0.5016, 0.3007, ...
I also had the same issue. What I did was that I created a dummy series for three years and then the data were successfully run.
b.1 <- brick(r.1, r.2, r.3, r.4, r.5, r.6, r.7, r.8, r.9, r.10, r.11, r.12)
b.2 <- stack(b.2, b.2, b.2)
pheno.test <- PhenologyRaster(b.2, start=c(2016,1), freq=12, approach="White",
tsgf="TSGFspline", interpolate=T)

Resources