# band 2 in a FARSITE landscape file has slope degrees
# convert slope degrees to slope percent in a new raster
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster")
ds_lcp <- new(GDALRaster, lcp_file)
ds_lcp$getMetadata(band=2, domain="")
slpp_file <- file.path(tempdir(), "storml_slpp.tif")
opt = c("COMPRESS=LZW")
rasterFromRaster(srcfile = lcp_file,
dstfile = slpp_file,
nbands = 1,
dtName = "Int16",
options = opt,
init = -32767)
ds_slp <- new(GDALRaster, slpp_file, read_only=FALSE)
# slpp_file is initialized to -32767 and nodata value set
ds_slp$getNoDataValue(band=1)
# extent and cell size are the same as lcp_file
ds_lcp$bbox()
ds_lcp$res()
ds_slp$bbox()
ds_slp$res()
# convert slope degrees in lcp_file band 2 to slope percent in slpp_file
# bring through LCP nodata -9999 to the output nodata value
ncols <- ds_slp$getRasterXSize()
nrows <- ds_slp$getRasterYSize()
for (row in 0:(nrows-1)) {
rowdata <- ds_lcp$read(band=2,
xoff=0, yoff=row,
xsize=ncols, ysize=1,
out_xsize=ncols, out_ysize=1)
rowslpp <- tan(rowdata*pi/180) * 100
rowslpp[rowdata==-9999] <- -32767
dim(rowslpp) <- c(1, ncols)
ds_slp$write(band=1, xoff=0, yoff=row, xsize=ncols, ysize=1, rowslpp)
}
# min, max, mean, sd
ds_slp$getStatistics(band=1, approx_ok=FALSE, force=TRUE)
ds_slp$close()
ds_lcp$close()
deleteDataset(slpp_file)
Run the code above in your browser using DataLab