## resample
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster")
ds <- new(GDALRaster, evt_file)
ds$res()
ds$bbox()
ds$close()
# table of the unique pixel values and their counts
tbl <- buildRAT(evt_file)
print(tbl)
sum(tbl$COUNT)
# resample at 90-m resolution
# EVT is thematic vegetation type so use a majority value
vrt_file <- rasterToVRT(evt_file,
resolution=c(90,90),
resampling="mode")
# .vrt is a small xml file pointing to the source raster
file.size(vrt_file)
tbl90m <- buildRAT(vrt_file)
print(tbl90m)
sum(tbl90m$COUNT)
ds <- new(GDALRaster, vrt_file)
ds$res()
ds$bbox()
ds$close()
vsi_unlink(vrt_file)
## clip
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster")
ds_evt <- new(GDALRaster, evt_file)
ds_evt$bbox()
# WKT string for a boundary within the EVT extent
bnd = "POLYGON ((324467.3 5104814.2, 323909.4 5104365.4, 323794.2
5103455.8, 324970.7 5102885.8, 326420.0 5103595.3, 326389.6 5104747.5,
325298.1 5104929.4, 325298.1 5104929.4, 324467.3 5104814.2))"
# src_align = TRUE
vrt_file <- rasterToVRT(evt_file,
subwindow = bbox_from_wkt(bnd),
src_align=TRUE)
ds_vrt <- new(GDALRaster, vrt_file)
# VRT is a virtual clip, pixel-aligned with the EVT raster
bbox_from_wkt(bnd)
ds_vrt$bbox()
ds_vrt$res()
ds_vrt$close()
vsi_unlink(vrt_file)
# src_align = FALSE
vrt_file <- rasterToVRT(evt_file,
subwindow = bbox_from_wkt(bnd),
src_align=FALSE)
ds_vrt_noalign <- new(GDALRaster, vrt_file)
# VRT upper left corner (xmin, ymax) is exactly bnd xmin, ymax
ds_vrt_noalign$bbox()
ds_vrt_noalign$res()
ds_vrt_noalign$close()
vsi_unlink(vrt_file)
ds_evt$close()
## subset and pixel align two rasters
# FARSITE landscape file for the Storm Lake area
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster")
ds_lcp <- new(GDALRaster, lcp_file)
# Landsat band 5 file covering the Storm Lake area
b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster")
ds_b5 <- new(GDALRaster, b5_file)
ds_lcp$bbox() # 323476.1 5101872.0 327766.1 5105082.0
ds_lcp$res() # 30 30
ds_b5$bbox() # 323400.9 5101815.8 327870.9 5105175.8
ds_b5$res() # 30 30
# src_align = FALSE because we need target alignment in this case:
vrt_file <- rasterToVRT(b5_file,
resolution = ds_lcp$res(),
subwindow = ds_lcp$bbox(),
src_align = FALSE)
ds_b5vrt <- new(GDALRaster, vrt_file)
ds_b5vrt$bbox() # 323476.1 5101872.0 327766.1 5105082.0
ds_b5vrt$res() # 30 30
# read the the Landsat file pixel-aligned with the LCP file
# summarize band 5 reflectance where FBFM = 165
# LCP band 4 contains FBFM (a classification of fuel beds):
ds_lcp$getMetadata(band=4, domain="")
# verify Landsat nodata (0):
ds_b5vrt$getNoDataValue(band=1)
# will be read as NA and omitted from stats
rs <- new(RunningStats, na_rm=TRUE)
ncols <- ds_lcp$getRasterXSize()
nrows <- ds_lcp$getRasterYSize()
for (row in 0:(nrows-1)) {
row_fbfm <- ds_lcp$read(band=4, xoff=0, yoff=row,
xsize=ncols, ysize=1,
out_xsize=ncols, out_ysize=1)
row_b5 <- ds_b5vrt$read(band=1, xoff=0, yoff=row,
xsize=ncols, ysize=1,
out_xsize=ncols, out_ysize=1)
rs$update(row_b5[row_fbfm == 165])
}
rs$get_count()
rs$get_mean()
rs$get_min()
rs$get_max()
rs$get_sum()
rs$get_var()
rs$get_sd()
ds_b5vrt$close()
vsi_unlink(vrt_file)
ds_lcp$close()
ds_b5$close()
Run the code above in your browser using DataLab