## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----setup-------------------------------------------------------------------- library(vaster) ## ----------------------------------------------------------------------------- dimension <- c(10, 5) extent <- c(0, 10, 0, 5) ## Cell boundaries are at integers x_corner(dimension, extent) y_corner(dimension, extent) ## ----------------------------------------------------------------------------- user_extent <- c(2.3, 7.8, 1.2, 4.6) ## Snap outward (default) - includes all cells touched align_extent(user_extent, dimension, extent, snap = "out") ## Snap inward - only fully contained cells align_extent(user_extent, dimension, extent, snap = "in") ## Snap to nearest boundary align_extent(user_extent, dimension, extent, snap = "near") ## ----------------------------------------------------------------------------- ## Snap to 0.5 unit resolution snap_extent(c(2.3, 7.8, 1.2, 4.6), res = 0.5) ## Snap to 1 unit resolution snap_extent(c(2.3, 7.8, 1.2, 4.6), res = 1) ## ----------------------------------------------------------------------------- ## Standard grid has origin at (0, 0) origin(c(10, 5), c(0, 10, 0, 5)) ## Offset grid origin(c(10, 5), c(0.5, 10.5, 0.25, 5.25)) ## ----------------------------------------------------------------------------- result <- vcrop(c(2.3, 7.8, 1.2, 4.6), dimension, extent) result$extent result$dimension ## ----------------------------------------------------------------------------- ## Extent that partially overlaps intersect_extent(c(-2, 5, -1, 3), dimension, extent) ## Extent fully within grid intersect_extent(c(2, 8, 1, 4), dimension, extent) ## ----------------------------------------------------------------------------- crop_extent <- c(2, 8, 1, 4) ## Dimension of the cropped region extent_dimension(crop_extent, dimension, extent) ## Verify: 6 columns (8-2) and 3 rows (4-1) ## ----------------------------------------------------------------------------- ## Define source grid (e.g., from a large raster file) src_dim <- c(3600, 1800) # 0.1 degree global grid src_ext <- c(-180, 180, -90, 90) ## User's region of interest roi <- c(140, 155, -45, -30) # Southeastern Australia ## Align to grid aligned_roi <- align_extent(roi, src_dim, src_ext, snap = "out") aligned_roi ## Get dimensions of cropped region crop_dim <- extent_dimension(aligned_roi, src_dim, src_ext) crop_dim ## Get cell indices to read cells <- cell_from_extent(src_dim, src_ext, aligned_roi) length(cells) ## Verify crop_dim[1] * crop_dim[2] ## ----------------------------------------------------------------------------- ## RasterIO parameters for reading the crop region rio <- rasterio0(src_dim, src_ext, aligned_roi) rio