diff --git a/DESCRIPTION b/DESCRIPTION index c8ecbf5..50d35ca 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -51,11 +51,10 @@ Imports: ggplot2, ggspatial, stats, - raster, sf, dplyr, - magrittr, httr, + terra, tidyhydat, whitebox, datasets, diff --git a/NAMESPACE b/NAMESPACE index 1a42493..eab5562 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -110,17 +110,8 @@ importFrom(lubridate,date) importFrom(lubridate,day) importFrom(lubridate,month) importFrom(lubridate,year) -importFrom(magrittr,"%>%") importFrom(plotrix,radial.grid) importFrom(plotrix,radial.plot) -importFrom(raster,crs) -importFrom(raster,getValues) -importFrom(raster,mask) -importFrom(raster,maxValue) -importFrom(raster,minValue) -importFrom(raster,raster) -importFrom(raster,rasterFromXYZ) -importFrom(raster,rasterToContour) importFrom(rnaturalearth,ne_download) importFrom(rnaturalearth,ne_load) importFrom(sf,as_Spatial) @@ -146,6 +137,15 @@ importFrom(stringr,fixed) importFrom(stringr,str_detect) importFrom(stringr,str_split_fixed) importFrom(stringr,str_to_lower) +importFrom(terra,as.contour) +importFrom(terra,crs) +importFrom(terra,hist) +importFrom(terra,mask) +importFrom(terra,minmax) +importFrom(terra,plot) +importFrom(terra,quantile) +importFrom(terra,rast) +importFrom(terra,values) importFrom(tidyhydat,hy_agency_list) importFrom(tidyhydat,hy_daily) importFrom(tidyhydat,hy_datum_list) diff --git a/R/ch_catchment_hyps.R b/R/ch_catchment_hyps.R index b21f73e..0e6b0e7 100644 --- a/R/ch_catchment_hyps.R +++ b/R/ch_catchment_hyps.R @@ -9,7 +9,7 @@ #' curve may also be created. #' #' @param catchment A \pkg{sf} object containing the catchment divide. -#' @param dem A \pkg{raster} object of the Digital Elevation Model. +#' @param dem A \pkg{terra} SpatRaster object of the Digital Elevation Model. #' @param z_levels Vector of elevation levels for the hypsometry. If specified, #' then no other elevation parameters are required. Default is \code{NULL}. #' @param n_levels If specified, sets number of elevation intervals. @@ -32,15 +32,13 @@ #' @param ... Other parameters for the graph #' #' @importFrom sf as_Spatial -#' @importFrom raster mask minValue maxValue +#' @importFrom terra mask minmax hist quantile plot #' @return Returns a data frame of elevations and catchment fractions below. #' @author Dan Moore #' @export #' #' @examples \donttest{ #' # Note: example not tested automatically as it is very slow to execute due to the downloading -#' library(raster) -#' library(magrittr) #' # change the following line to specify a directory to hold the data #' dir_name <- tempdir(check = FALSE) #' # create directory to store data sets @@ -59,11 +57,11 @@ #' cb <- ch_get_url_data(cb_url, cb_fn) #' #' # quick check plot - all catchments -#' raster::plot(dem_upc) +#' terra::plot(dem_upc) #' plot(cb, add = TRUE, col = NA) #' #' # subset 240 catchment -#' cb_240 <- cb %>% dplyr::filter(wsc_name == "240") +#' cb_240 <- cb |> dplyr::filter(wsc_name == "240") #' plot(cb_240, col = NA) #' #' ## test function @@ -101,27 +99,25 @@ ch_catchment_hyps <- function(catchment, dem, add_grid = FALSE, ...) { # need to add error traps for incorrect values for # catchment and dem - catchment_sp <- as_Spatial(catchment) - dem_masked <- raster::mask(dem, catchment_sp) + dem_masked <- terra::mask(dem, catchment) if (is.null(quantiles)) { if (is.null(z_levels)) { - if (is.null(zmin)) zmin <- raster::minValue(dem_masked) - if (is.null(zmax)) zmax <- raster::maxValue(dem_masked) + if (is.null(zmin)) zmin <- terra::minmax(dem_masked)[1] + if (is.null(zmax)) zmax <- terra::minmax(dem_masked)[2] z_levels <- seq(zmin, zmax, length.out = n_levels) } # there may be a more direct way, but this works - z_hist <- raster::hist(dem_masked, plot = FALSE, breaks = z_levels) + z_hist <- terra::hist(dem_masked, plot = FALSE, breaks = z_levels) nz <- sum(z_hist$counts) qz <- c(0, cumsum(z_hist$counts)/nz) out_df <- data.frame(z = z_levels, qz) } else { - zq <- raster::quantile(dem_masked, probs = quantiles) - out_df <- data.frame(z = zq, qz = quantiles) + zq <- terra::global(dem_masked, fun = quantile, probs = quantiles, na.rm = T) + out_df <- data.frame(z = as.numeric(t(zq)), qz = quantiles) } if (hypso_plot) { plot(out_df$qz, out_df$z, - col = col, xlab = xlab, ylab = ylab, type = type, ... - ) + col = col, xlab = xlab, ylab = ylab, type = type) if (add_grid) grid() } return(out_df) diff --git a/R/ch_checkcatchment.R b/R/ch_checkcatchment.R index 28edb31..a1d3c1d 100644 --- a/R/ch_checkcatchment.R +++ b/R/ch_checkcatchment.R @@ -8,7 +8,7 @@ #' Also generates a table summarizing the catchments, #' including the coordinates of the outlet point and the catchment area. #' -#' @param dem raster DEM that catchments were generated from. +#' @param dem terra SpatRaster DEM that catchments were generated from. #' @param catchment Catchment polygon (sf object). #' @param outlet Location of catchment outlet (sf object). #' @param outlet_label Character label for outlet. @@ -36,23 +36,22 @@ #' @importFrom ggspatial annotation_north_arrow north_arrow_fancy_orienteering annotation_scale #' @importFrom dplyr mutate #' @importFrom grid unit -#' @importFrom magrittr %>% #' @export #' @examples #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = ".tif") #' no_sink_raster_file <- tempfile("no_sinks", fileext = ".tif") #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, -#' method = "fill") +#' method = "fill") #' #' # get flow accumulations #' flow_acc_file <- tempfile("flow_acc", fileext = ".tif") @@ -63,7 +62,7 @@ #' pourpoints <- ch_volcano_pourpoints(pourpoint_file) #' snapped_pourpoint_file <- tempfile("snapped_pourpoints", fileext = ".shp") #' snapped_pourpoints <- ch_wbt_pourpoints(pourpoints, flow_acc_file, pourpoint_file, -#' snapped_pourpoint_file, snap_dist = 10) +#' snapped_pourpoint_file, snap_dist = 10) #' #' # get flow directions #' flow_dir_file <- tempfile("flow_dir", fileext = ".tif") @@ -71,7 +70,7 @@ #' fn_catchment_ras <- tempfile("catchment", fileext = ".tif") #' fn_catchment_vec <- tempfile("catchment", fileext = ".shp") #' catchments <- ch_wbt_catchment(snapped_pourpoint_file, flow_dir_file, -#' fn_catchment_ras, fn_catchment_vec) +#' fn_catchment_ras, fn_catchment_vec) #' #' # check results #' ch_checkcatchment(test_raster, catchments, snapped_pourpoints) @@ -142,7 +141,7 @@ ch_checkcatchment <- function(dem, catchment, outlet, outlet_label = NULL, area <- st_area(catchment) units <- rep(paste0(attr(area, "units")$numerator[1], "^2"), length(area)) value <- round(as.numeric(area)) - area_df <- outlet %>% + area_df <- outlet |> mutate(label = labels, area = value, units = units) return(TRUE) diff --git a/R/ch_checkchannels.R b/R/ch_checkchannels.R index 824426a..3ef48a6 100644 --- a/R/ch_checkchannels.R +++ b/R/ch_checkchannels.R @@ -6,7 +6,7 @@ #' @details #' Generates a simple map of the drainage network plotted over the contours to allow a visual assessment. #' -#' @param dem raster DEM that catchments were generated from +#' @param dem terra SpatRaster DEM that catchments were generated from #' @param channels channel polyline (or channels list from \code{ch_wbt_channels}) (sf object) #' @param main_label Main label for channel plot. #' @param channel_colour Colour for channel. Default is "blue". @@ -27,13 +27,13 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = c(".tif")) #' no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -48,14 +48,14 @@ #' channel_raster_file <- tempfile("channels", fileext = c(".tif")) #' channel_vector_file <- tempfile("channels", fileext = c(".shp")) #' channels <- ch_wbt_channels(flow_acc_file, flow_dir_file, channel_raster_file, -#' channel_vector_file, 1) +#' channel_vector_file, 1) #' #' # get pour points #' pourpoint_file <- tempfile("volcano_pourpoints", fileext = ".shp") #' pourpoints <- ch_volcano_pourpoints(pourpoint_file) #' snapped_pourpoint_file <- tempfile("snapped_pourpoints", fileext = ".shp") #' snapped_pourpoints <- ch_wbt_pourpoints(pourpoints, flow_acc_file, pourpoint_file, -#' snapped_pourpoint_file, snap_dist = 10) +#' snapped_pourpoint_file, snap_dist = 10) #' ch_checkchannels(test_raster, channels, snapped_pourpoints) #' } else { #' message("Examples not run as Whitebox executable not found") diff --git a/R/ch_contours.R b/R/ch_contours.R index d830e45..dea65e4 100644 --- a/R/ch_contours.R +++ b/R/ch_contours.R @@ -29,9 +29,8 @@ #' # plot contours map #' plot(contours) #' -#' @importFrom raster raster getValues rasterToContour crs +#' @importFrom terra rast values as.contour crs #' @importFrom sf st_as_sf st_crs -#' @importFrom magrittr %>% #' @export ch_contours <- function(dem, zmin = NULL, zmax = NULL, @@ -45,7 +44,7 @@ ch_contours <- function(dem, # determine contour levels if (is.null(z_levels)) { - z <- getValues(dem) + z <- values(dem) if (is.null(zmin)) zmin <- min(z, na.rm = TRUE) if (is.null(zmax)) zmax <- max(z, na.rm = TRUE) z_levels <- seq(zmin, zmax, length.out = n_levels) @@ -53,7 +52,7 @@ ch_contours <- function(dem, # if dem includes sea level, start contours at 0.1 m to mimic coastline if (z_levels[1] <= 0) {z_levels[1] <- 0.1} # generate contours as a sf object - contours_sf <- rasterToContour(dem, levels = z_levels) %>% + contours_sf <- as.contour(dem, levels = z_levels) |> st_as_sf() sf::st_crs(contours_sf) <- crs(dem) return(contours_sf) diff --git a/R/ch_get_url_data.R b/R/ch_get_url_data.R index f03c987..fb27cf1 100644 --- a/R/ch_get_url_data.R +++ b/R/ch_get_url_data.R @@ -11,7 +11,7 @@ #' #' @importFrom httr GET write_disk #' @importFrom sf st_read -#' @importFrom raster raster +#' @importFrom terra rast #' #' @return Returns a data frame (from a .csv file), a \code{raster} object (from a .tif file), #'or an \code{sf} object (from a GeoJSON file). @@ -22,7 +22,7 @@ #' # Tested using files in the Upper Penticton Creek #' # zenodo repository https://zenodo.org/record/4781469 #' library(ggplot2) -#' library(raster) +#' library(terra) #' #' # create directory to store data sets #' dir_name <- tempdir(check = FALSE) @@ -40,7 +40,7 @@ #' ra_fn <- file.path(dir_name, "gs_dem25.tif") #' ra_url <- "https://zenodo.org/record/4781469/files/gs_dem25.tif" #' ra_data <- ch_get_url_data(ra_url, ra_fn) -#' plot(ra_data) +#' terra::plot(ra_data) #' #' # test with GeoJSON #' gs_fn <- file.path(dir_name, "gs_soilmaps.GeoJSON") @@ -58,7 +58,8 @@ #' @export #' ch_get_url_data <- function(gd_url, gd_filename, quiet = FALSE) { - file_ext <- strsplit(x = gd_filename, split = "[.]")[[1]][2] + filesplit <- strsplit(x = gd_filename, split = "[.]")[[1]] + file_ext <- filesplit[length(filesplit)] # csv file - returns data frame if (file_ext == "csv") { @@ -89,7 +90,7 @@ ch_get_url_data <- function(gd_url, gd_filename, quiet = FALSE) { GET(gd_url, write_disk(gd_filename)) } - da <- raster::raster(gd_filename) + da <- terra::rast(gd_filename) return(da) } diff --git a/R/ch_map_plot_data.R b/R/ch_map_plot_data.R index ac76c20..725a267 100644 --- a/R/ch_map_plot_data.R +++ b/R/ch_map_plot_data.R @@ -248,10 +248,10 @@ ch_map_plot_data <- function(map_data, map_label4 <- map_label3[map_label3$llat <= maplat[2],] - map_labels <- map_label4 %>% - dplyr::mutate(Label = as.factor(Labels)) %>% - dplyr::mutate(lpos = as.factor(lpos)) %>% - sf::st_as_sf(coords = c("llong","llat")) %>% + map_labels <- map_label4 |> + dplyr::mutate(Label = as.factor(Labels)) |> + dplyr::mutate(lpos = as.factor(lpos)) |> + sf::st_as_sf(coords = c("llong","llat")) |> sf::st_set_crs(4326) @@ -293,9 +293,9 @@ ch_map_plot_data <- function(map_data, } # create a simple features object with crs = epsg 4326 (longlat with WGS84 geoid) - pt_sf <- data.frame(mlong, mlat, mcode) %>% - dplyr::mutate(mcode = as.factor(mcode)) %>% - sf::st_as_sf(coords = c("mlong", "mlat")) %>% + pt_sf <- data.frame(mlong, mlat, mcode) |> + dplyr::mutate(mcode = as.factor(mcode)) |> + sf::st_as_sf(coords = c("mlong", "mlat")) |> sf::st_set_crs(4326) @@ -340,10 +340,10 @@ ch_map_plot_data <- function(map_data, # create a simple features object with crs = epsg 4326 (longlat with WGS84 geoid) - pt_sf <- data.frame(mlong, mlat, trend, signif) %>% - dplyr::mutate(trend = as.factor(trend)) %>% - dplyr::mutate(signif = as.factor(signif)) %>% - sf::st_as_sf(coords = c("mlong", "mlat")) %>% + pt_sf <- data.frame(mlong, mlat, trend, signif) |> + dplyr::mutate(trend = as.factor(trend)) |> + dplyr::mutate(signif = as.factor(signif)) |> + sf::st_as_sf(coords = c("mlong", "mlat")) |> sf::st_set_crs(4326) # reproject @@ -385,9 +385,9 @@ ch_map_plot_data <- function(map_data, # create a simple features object with crs = epsg 4326 (longlat with WGS84 geoid) - pt_sf <- data.frame(mlong, mlat, var_a ) %>% - dplyr::mutate(var_a = as.factor(var_a)) %>% - sf::st_as_sf(coords = c("mlong", "mlat")) %>% + pt_sf <- data.frame(mlong, mlat, var_a ) |> + dplyr::mutate(var_a = as.factor(var_a)) |> + sf::st_as_sf(coords = c("mlong", "mlat")) |> sf::st_set_crs(4326) # reproject @@ -435,9 +435,9 @@ ch_map_plot_data <- function(map_data, # create a simple features object with crs = epsg 4326 (longlat with WGS84 geoid) - pt_sf <- data.frame(mlong, mlat, var_a ) %>% - dplyr::mutate(var_a = as.factor(var_a)) %>% - sf::st_as_sf(coords = c("mlong", "mlat")) %>% + pt_sf <- data.frame(mlong, mlat, var_a ) |> + dplyr::mutate(var_a = as.factor(var_a)) |> + sf::st_as_sf(coords = c("mlong", "mlat")) |> sf::st_set_crs(4326) # reproject @@ -483,13 +483,13 @@ ch_map_plot_data <- function(map_data, lcol <- unlist(x_labels[6]) llabels <- unlist(x_labels[7]) - xmap_labels <- data.frame(llong, llat, lpos, lcol, lfont, lcex, llabels) %>% - dplyr::mutate(llabels = as.factor(llabels)) %>% - dplyr::mutate(lpos = as.factor(lpos)) %>% - dplyr::mutate(lcol = as.factor(lcol)) %>% - dplyr::mutate(lcex = as.factor(lcex)) %>% - dplyr::mutate(lfont = as.factor(lfont)) %>% - sf::st_as_sf(coords = c("llong","llat")) %>% + xmap_labels <- data.frame(llong, llat, lpos, lcol, lfont, lcex, llabels) |> + dplyr::mutate(llabels = as.factor(llabels)) |> + dplyr::mutate(lpos = as.factor(lpos)) |> + dplyr::mutate(lcol = as.factor(lcol)) |> + dplyr::mutate(lcex = as.factor(lcex)) |> + dplyr::mutate(lfont = as.factor(lfont)) |> + sf::st_as_sf(coords = c("llong","llat")) |> sf::st_set_crs(4326) x_labelsa <- sf::st_transform(xmap_labels, map_proj) diff --git a/R/ch_volcano_pourpoints.R b/R/ch_volcano_pourpoints.R index c980f10..5462f80 100644 --- a/R/ch_volcano_pourpoints.R +++ b/R/ch_volcano_pourpoints.R @@ -10,7 +10,6 @@ #' @export #' @importFrom dplyr mutate #' @importFrom sf st_as_sf st_set_crs st_write -#' @importFrom magrittr %>% #' @author Dan Moore and Kevin Shook #' @seealso \code{\link{ch_volcano_raster}} \code{\link{ch_wbt_pourpoints}} #' @@ -25,9 +24,9 @@ ch_volcano_pourpoints <- function(pp_shp) { } outlet_sf <- data.frame(x = c(300570, 300644), - y = c(5916757, 5916557)) %>% - mutate(test_label = c("test_1", "test_2")) %>% - st_as_sf(coords = c("x", "y")) %>% + y = c(5916757, 5916557)) |> + mutate(test_label = c("test_1", "test_2")) |> + st_as_sf(coords = c("x", "y")) |> st_set_crs(32760) if (file.exists(pp_shp)) st_write(outlet_sf, pp_shp, delete_dsn = TRUE) diff --git a/R/ch_volcano_raster.R b/R/ch_volcano_raster.R index 59e50fd..549f69f 100644 --- a/R/ch_volcano_raster.R +++ b/R/ch_volcano_raster.R @@ -1,7 +1,7 @@ -#' Create Test Raster +#' Create Test SpatRaster #' #' @description -#' Creates a \pkg{raster} object of land surface elevations, as +#' Creates a \pkg{terra} object of land surface elevations, as #' used to test/demonstrate many functions requiring a digital elevation model #' (DEM). #' @@ -10,10 +10,9 @@ #' \code{volcano} matrix of elevations. #' #' @export -#' @return Returns a raster object of land surface elevations. +#' @return Returns a SpatRaster object of land surface elevations. #' @author Dan Moore and Kevin Shook -#' @importFrom raster rasterFromXYZ crs -#' @importFrom magrittr %>% +#' @importFrom terra rast crs #' @examples #' test_raster <- ch_volcano_raster() #' @@ -28,8 +27,8 @@ ch_volcano_raster <- function() { ymax <- ymin + (nr - 1)*dx x <- rep(seq(xmax, xmin, -dx), each = nr) y <- rep(seq(ymin, ymax, dx), times = nc) - vol_ras <- data.frame(x, y, z = as.numeric(vol_mat)) %>% - raster::rasterFromXYZ() - raster::crs(vol_ras) <- "+proj=utm +zone=60 +south +datum=WGS84 +units=m +no_defs" + vol_ras <- data.frame(x, y, z = as.numeric(vol_mat)) |> + terra::rast(type = 'xyz') + terra::crs(vol_ras) <- "+proj=utm +zone=60 +south +datum=WGS84 +units=m +no_defs" return(vol_ras) } diff --git a/R/ch_wbt_catchment.R b/R/ch_wbt_catchment.R index 1c08b8d..cd061c5 100644 --- a/R/ch_wbt_catchment.R +++ b/R/ch_wbt_catchment.R @@ -7,10 +7,9 @@ #' @param return_vector If \code{TRUE} (the default) a vector of the catchment will be returned. #' #' @author Dan Moore and Kevin Shook -#' @importFrom raster raster +#' @importFrom terra rast #' @importFrom whitebox wbt_watershed wbt_raster_to_vector_polygons #' @importFrom sf st_crs write_sf st_read -#' @importFrom magrittr %>% #' @return If \code{return_vector == TRUE} a vector of the catchment is returned. Otherwise #' nothing is returned. #' @export @@ -19,13 +18,13 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = ".tif") #' no_sink_raster_file <- tempfile("no_sinks", fileext = ".tif") #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -39,7 +38,7 @@ #' pourpoints <- ch_volcano_pourpoints(pourpoint_file) #' snapped_pourpoint_file <- tempfile("snapped_pourpoints", fileext = ".shp") #' snapped_pourpoints <- ch_wbt_pourpoints(pourpoints, flow_acc_file, pourpoint_file, -#' snapped_pourpoint_file, snap_dist = 10) +#' snapped_pourpoint_file, snap_dist = 10) #' #' # get flow directions #' flow_dir_file <- tempfile("flow_dir", fileext = ".tif") @@ -47,14 +46,16 @@ #' fn_catchment_ras <- tempfile("catchment", fileext = ".tif") #' fn_catchment_vec <- tempfile("catchment", fileext = ".shp") #' catchments <- ch_wbt_catchment(snapped_pourpoint_file, flow_dir_file, -#' fn_catchment_ras, fn_catchment_vec) +#' fn_catchment_ras, fn_catchment_vec) #' } else { #' message("Examples not run as Whitebox executable not found") #' } ch_wbt_catchment <- function(fn_pp_snap, fn_flowdir, fn_catchment_ras, fn_catchment_vec, return_vector = TRUE) { + ch_wbt_check_whitebox() + if (!file.exists(fn_pp_snap)) { stop("Error: file containing snapped pour points does not exist") } @@ -63,19 +64,28 @@ ch_wbt_catchment <- function(fn_pp_snap, fn_flowdir, fn_catchment_ras, } message("ch_wbt: Delineating catchment boundaries") + crs_pp <- sf::st_crs(st_read(fn_pp_snap))$epsg - crs_fd <- sf::st_crs(raster(fn_flowdir))$epsg + + crs_fd <- sf::st_crs(rast(fn_flowdir))$epsg + if (crs_pp != crs_fd) { stop("Error: pour points and flow direction grid have different crs") } + wbt_watershed(d8_pntr = fn_flowdir, pour_pts = fn_pp_snap, output = fn_catchment_ras) + wbt_raster_to_vector_polygons(fn_catchment_ras, fn_catchment_vec) - catchment_vec <- st_read(fn_catchment_vec) %>% st_as_sf() + + catchment_vec <- st_read(fn_catchment_vec) |> + st_as_sf() + if (is.na(st_crs(catchment_vec))) { sf::st_crs(catchment_vec) <- st_crs(raster(fn_catchment_ras)) write_sf(catchment_vec, fn_catchment_vec) } + if (return_vector) { return(st_read(fn_catchment_vec)) } else { diff --git a/R/ch_wbt_catchment_onestep.R b/R/ch_wbt_catchment_onestep.R index 35ebb92..3959972 100644 --- a/R/ch_wbt_catchment_onestep.R +++ b/R/ch_wbt_catchment_onestep.R @@ -24,10 +24,9 @@ #' @param ... Extra parameters for \code{ch_wbt_removesinks}. #' @author Dan Moore and Kevin Shook #' @seealso \code{\link{ch_wbt_filenames}} -#' @importFrom raster raster +#' @importFrom terra rast #' @importFrom whitebox wbt_extract_streams wbt_raster_streams_to_vector wbt_snap_pour_points wbt_watershed wbt_raster_to_vector_polygons #' @importFrom sf st_crs write_sf st_write -#' @importFrom magrittr %>% #' @return Returns an \pkg{sp} object of the delineated catchment. #' @export #' @@ -35,16 +34,17 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = c(".tif")) #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' wd <- tempdir() #' pourpoint_file <- tempfile("volcano_pourpoints", fileext = ".shp") #' pourpoints <- ch_volcano_pourpoints(pourpoint_file) #' catchment <- ch_wbt_catchment_onestep(wd = wd, in_dem = dem_raster_file, -#' pp_sf = pourpoints, sink_method = "fill", threshold = 1, snap_dist = 10) +#' pp_sf = pourpoints, sink_method = "fill", +#' threshold = 1, snap_dist = 10) #' } else { #' message("Examples not run as Whitebox executable not found") #' } @@ -82,23 +82,35 @@ ch_wbt_catchment_onestep <- function(wd, in_dem, pp_sf, fn_dem_fsc = file_names$dem_fsc, ...) if (inherits(dem_ns, "character")) return(NULL) + ch_wbt_flow_accumulation(fn_dem_ns = file_names$dem_ns, fn_flowacc = file_names$flowacc, return_raster = FALSE) + ch_wbt_flow_direction(fn_dem_ns = file_names$dem_ns, fn_flowdir = file_names$flowdir, return_raster = FALSE) + wbt_extract_streams(file_names$flowacc, file_names$channel_ras, threshold = threshold) + wbt_raster_streams_to_vector(file_names$channel_ras, file_names$flowdir, file_names$channel_vec) + sf::st_write(pp_sf, file_names$pp, quiet = TRUE, delete_layer = TRUE) + wbt_snap_pour_points(file_names$pp, file_names$flowacc, file_names$pp_snap, snap_dist) + wbt_watershed(file_names$flowdir, file_names$pp_snap, file_names$catchment_ras) + wbt_raster_to_vector_polygons(file_names$catchment_ras, file_names$catchment_vec) - catchment_vec <- st_read(file_names$catchment_vec) %>% st_as_sf() + + catchment_vec <- st_read(file_names$catchment_vec) |> + st_as_sf() + if (is.na(sf::st_crs(catchment_vec))) { sf::st_crs(catchment_vec) <- sf::st_crs(raster(file_names$catchment_ras)) sf::write_sf(catchment_vec, file_names$catchment_vec) } - channel_vec <- st_read(file_names$channel_vec) %>% st_as_sf() + channel_vec <- st_read(file_names$channel_vec) |> st_as_sf() + if (is.na(sf::st_crs(channel_vec))) { sf::st_crs(channel_vec) <- sf::st_crs(catchment_vec) sf::write_sf(channel_vec, file_names$catchment_vec) @@ -111,5 +123,6 @@ ch_wbt_catchment_onestep <- function(wd, in_dem, pp_sf, plot_na = plot_na, plot_scale = plot_scale, na_location = na_location, scale_location = scale_location) } + return(catchment_vec) } \ No newline at end of file diff --git a/R/ch_wbt_channels.R b/R/ch_wbt_channels.R index f3e71cd..01ccc3b 100644 --- a/R/ch_wbt_channels.R +++ b/R/ch_wbt_channels.R @@ -7,7 +7,7 @@ #' @param threshold Threshold for channel initiation. #' @param ... Other parameters for \pkg{whitebox} function \code{wbt_extract_streams} #' @author Dan Moore -#' @importFrom raster raster +#' @importFrom terra rast #' @importFrom whitebox wbt_extract_streams wbt_raster_streams_to_vector #' @importFrom sf st_crs write_sf #' @importFrom stats step @@ -18,13 +18,13 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = c(".tif")) #' no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -48,7 +48,9 @@ ch_wbt_channels <- function(fn_flowacc, fn_flowdir, fn_channel_ras, fn_channel_vec, threshold = NULL, ...) { + ch_wbt_check_whitebox() + if (!file.exists(fn_flowacc)) { stop("Error: input flow accumulation file does not exist") } @@ -56,17 +58,24 @@ ch_wbt_channels <- function(fn_flowacc, fn_flowdir, if (!file.exists(fn_flowdir)) { stop("Error: input flow direction file does not exist") } + if (is.null(threshold)) { step("Error: threshold for channel initiation not specified") } message("ch_wbt: Generating stream network") + wbt_extract_streams(fn_flowacc, fn_channel_ras, threshold = threshold, ...) + wbt_raster_streams_to_vector(fn_channel_ras, fn_flowdir, fn_channel_vec) + channel_vec <- st_read(fn_channel_vec) + if(is.na(st_crs(channel_vec))) { - sf::st_crs(channel_vec) <- st_crs(raster(fn_channel_ras)) + sf::st_crs(channel_vec) <- st_crs(rast(fn_channel_ras)) write_sf(channel_vec, fn_channel_vec) } + return(channel_vec) -} + + } diff --git a/R/ch_wbt_flow_accumulation.R b/R/ch_wbt_flow_accumulation.R index aa5c3e2..a3b70d4 100644 --- a/R/ch_wbt_flow_accumulation.R +++ b/R/ch_wbt_flow_accumulation.R @@ -3,12 +3,12 @@ #' @param fn_dem_ns File name of dem with sinks removed. #' @param fn_flowacc File name for flow accumulation grid to be created. #' @param return_raster If \code{TRUE} (the default), the flow accumulation -#' grid will be returned as a raster object, in addition to being written to +#' grid will be returned as a SpatRaster object, in addition to being written to #' \option{fn_flowacc}. If \code{FALSE}, the output file will still be created #' but a \code{NULL} value is returned. #' #' @author Dan Moore -#' @importFrom raster raster +#' @importFrom terra rast #' @importFrom whitebox wbt_d8_flow_accumulation #' @return If \code{return_raster = TRUE}, the flow accumulation #' grid will be returned as a raster object, otherwise \code{NULL} is returned. @@ -18,13 +18,13 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = c(".tif")) #' no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -47,7 +47,7 @@ ch_wbt_flow_accumulation <- function(fn_dem_ns, fn_flowacc, return_raster = TRUE wbt_d8_flow_accumulation(fn_dem_ns, fn_flowacc) if (return_raster) { - return(raster(fn_flowacc)) + return(rast(fn_flowacc)) } else { return(NULL) } diff --git a/R/ch_wbt_flow_direction.R b/R/ch_wbt_flow_direction.R index b729207..f080335 100644 --- a/R/ch_wbt_flow_direction.R +++ b/R/ch_wbt_flow_direction.R @@ -5,7 +5,7 @@ #' @param return_raster Should a raster object be returned? #' #' @author Dan Moore -#' @importFrom raster raster +#' @importFrom terra rast #' @importFrom whitebox wbt_d8_pointer #' @return If \code{return_raster = TRUE} (the default), the flow direction #' grid will be returned as a raster object, in addition to being written to @@ -17,13 +17,13 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = c(".tif")) #' no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -45,7 +45,7 @@ ch_wbt_flow_direction <- function(fn_dem_ns, fn_flowdir, return_raster = TRUE) { wbt_d8_pointer(fn_dem_ns, fn_flowdir) if (return_raster) { - return(raster(fn_flowdir)) + return(rast(fn_flowdir)) } else { return(NULL) } diff --git a/R/ch_wbt_pourpoints.R b/R/ch_wbt_pourpoints.R index 3b6ac1e..0aa8779 100644 --- a/R/ch_wbt_pourpoints.R +++ b/R/ch_wbt_pourpoints.R @@ -16,7 +16,7 @@ #' #' @author Dan Moore #' @seealso \code{\link{ch_volcano_pourpoints}} -#' @importFrom raster raster +#' @importFrom terra rast #' @importFrom whitebox wbt_snap_pour_points #' @importFrom sf st_crs st_write #' @return Returns a \pkg{sf} object of the specified pour points snapped to the @@ -26,13 +26,13 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = c(".tif")) #' no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file) #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -52,25 +52,34 @@ #' } ch_wbt_pourpoints <- function(pp_sf = NULL, fn_flowacc, fn_pp, fn_pp_snap, check_crs = TRUE, snap_dist = NULL, ...) { + ch_wbt_check_whitebox() + if (!file.exists(fn_flowacc)) { stop("Error: flow accumulation file does not exist") } + if (missing(pp_sf)) { stop("Error: value for pp_sf missing") } + if (is.null(snap_dist)) { stop("Error: value for snap_dist missing") } + if (check_crs) { pp_crs <- st_crs(pp_sf)$epsg - fa_crs <- st_crs(raster(fn_flowacc))$epsg + fa_crs <- st_crs(rast(fn_flowacc))$epsg if (pp_crs != fa_crs) { stop("Error: pour points and flow accumulation grid have different crs") } } + message("ch_wbt: Snapping pour points to stream network") + st_write(pp_sf, fn_pp, quiet = TRUE, delete_layer = TRUE) + wbt_snap_pour_points(fn_pp, fn_flowacc, fn_pp_snap, snap_dist, ...) + return(st_read(fn_pp_snap)) } diff --git a/R/ch_wbt_removesinks.R b/R/ch_wbt_removesinks.R index f90127c..16b8de7 100644 --- a/R/ch_wbt_removesinks.R +++ b/R/ch_wbt_removesinks.R @@ -12,7 +12,7 @@ #' @param ... Additional arguments to be passed to functions to remove sinks. #' #' @author Dan Moore -#' @importFrom raster raster +#' @importFrom terra rast #' @importFrom whitebox wbt_init wbt_fill_single_cell_pits wbt_breach_depressions_least_cost #' @importFrom whitebox wbt_fill_depressions_wang_and_liu #' @importFrom whitebox wbt_breach_depressions wbt_fill_depressions wbt_fill_depressions_planchon_and_darboux @@ -23,13 +23,13 @@ #' # Only proceed if Whitebox executable is installed #' library(whitebox) #' if (check_whitebox_binary()){ -#' library(raster) +#' library(terra) #' test_raster <- ch_volcano_raster() #' dem_raster_file <- tempfile(fileext = c(".tif")) #' no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) #' #' # write test raster to file -#' writeRaster(test_raster, dem_raster_file, format = "GTiff") +#' terra::writeRaster(test_raster, dem_raster_file, format = "GTiff") #' #' # remove sinks #' removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -40,27 +40,44 @@ ch_wbt_removesinks <- function(in_dem, out_dem, method = "breach_leastcost", dist = NULL, fn_dem_fsc = NULL, ...) { ch_wbt_check_whitebox() + + exe_location <- wbt_init() + if (!file.exists(in_dem)) { stop("Error: input dem file does not exist") } + if (method == "breach_leastcost") { if (is.null(dist)) { stop("Error: no value for dist, which is required for wbt_breach_depressions_least_cost") } - wbt_fill_single_cell_pits(in_dem, fn_dem_fsc) - wbt_breach_depressions_least_cost(fn_dem_fsc, out_dem, dist, ...) - } else if (method == "breach") { - wbt_fill_single_cell_pits(in_dem, fn_dem_fsc) - wbt_breach_depressions(fn_dem_fsc, out_dem, ...) - } else if (method == "fill") { - wbt_fill_depressions(in_dem, out_dem, ...) - } else if (method == "fill_pd") { - wbt_fill_depressions_planchon_and_darboux(in_dem, out_dem, ...) - } else if (method == "fill_wl") { - wbt_fill_depressions_wang_and_liu(in_dem, out_dem, ...) - } else { - stop("Error: incorrect method for sink removal specified") - } - return(raster(out_dem)) + + wbt_fill_single_cell_pits(in_dem, fn_dem_fsc) + wbt_breach_depressions_least_cost(fn_dem_fsc, out_dem, dist, ...) + + } else if (method == "breach") { + + wbt_fill_single_cell_pits(in_dem, fn_dem_fsc) + + wbt_breach_depressions(fn_dem_fsc, out_dem, ...) + + } else if (method == "fill") { + + wbt_fill_depressions(in_dem, out_dem, ...) + + } else if (method == "fill_pd") { + + wbt_fill_depressions_planchon_and_darboux(in_dem, out_dem, ...) + + } else if (method == "fill_wl") { + + wbt_fill_depressions_wang_and_liu(in_dem, out_dem, ...) + + } else { + + stop("Error: incorrect method for sink removal specified") + + } + return(rast(out_dem)) } \ No newline at end of file diff --git a/man/ch_catchment_hyps.Rd b/man/ch_catchment_hyps.Rd index 333744f..4b40c1b 100644 --- a/man/ch_catchment_hyps.Rd +++ b/man/ch_catchment_hyps.Rd @@ -25,7 +25,7 @@ ch_catchment_hyps( \arguments{ \item{catchment}{A \pkg{sf} object containing the catchment divide.} -\item{dem}{A \pkg{raster} object of the Digital Elevation Model.} +\item{dem}{A \pkg{terra} SpatRaster object of the Digital Elevation Model.} \item{z_levels}{Vector of elevation levels for the hypsometry. If specified, then no other elevation parameters are required. Default is \code{NULL}.} @@ -75,8 +75,6 @@ curve may also be created. \examples{ \donttest{ # Note: example not tested automatically as it is very slow to execute due to the downloading -library(raster) -library(magrittr) # change the following line to specify a directory to hold the data dir_name <- tempdir(check = FALSE) # create directory to store data sets @@ -95,11 +93,11 @@ cb_url <- "https://zenodo.org/record/4781469/files/gs_catchments.GeoJSON" cb <- ch_get_url_data(cb_url, cb_fn) # quick check plot - all catchments -raster::plot(dem_upc) +terra::plot(dem_upc) plot(cb, add = TRUE, col = NA) # subset 240 catchment -cb_240 <- cb \%>\% dplyr::filter(wsc_name == "240") +cb_240 <- cb |> dplyr::filter(wsc_name == "240") plot(cb_240, col = NA) ## test function diff --git a/man/ch_checkcatchment.Rd b/man/ch_checkcatchment.Rd index f10c2c5..72404e4 100644 --- a/man/ch_checkcatchment.Rd +++ b/man/ch_checkcatchment.Rd @@ -23,7 +23,7 @@ ch_checkcatchment( ) } \arguments{ -\item{dem}{raster DEM that catchments were generated from.} +\item{dem}{terra SpatRaster DEM that catchments were generated from.} \item{catchment}{Catchment polygon (sf object).} @@ -71,17 +71,17 @@ including the coordinates of the outlet point and the catchment area. # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = ".tif") no_sink_raster_file <- tempfile("no_sinks", fileext = ".tif") # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, - method = "fill") + method = "fill") # get flow accumulations flow_acc_file <- tempfile("flow_acc", fileext = ".tif") @@ -92,7 +92,7 @@ if (check_whitebox_binary()){ pourpoints <- ch_volcano_pourpoints(pourpoint_file) snapped_pourpoint_file <- tempfile("snapped_pourpoints", fileext = ".shp") snapped_pourpoints <- ch_wbt_pourpoints(pourpoints, flow_acc_file, pourpoint_file, - snapped_pourpoint_file, snap_dist = 10) + snapped_pourpoint_file, snap_dist = 10) # get flow directions flow_dir_file <- tempfile("flow_dir", fileext = ".tif") @@ -100,7 +100,7 @@ if (check_whitebox_binary()){ fn_catchment_ras <- tempfile("catchment", fileext = ".tif") fn_catchment_vec <- tempfile("catchment", fileext = ".shp") catchments <- ch_wbt_catchment(snapped_pourpoint_file, flow_dir_file, - fn_catchment_ras, fn_catchment_vec) + fn_catchment_ras, fn_catchment_vec) # check results ch_checkcatchment(test_raster, catchments, snapped_pourpoints) diff --git a/man/ch_checkchannels.Rd b/man/ch_checkchannels.Rd index 4fc1429..a5a5bb8 100644 --- a/man/ch_checkchannels.Rd +++ b/man/ch_checkchannels.Rd @@ -15,7 +15,7 @@ ch_checkchannels( ) } \arguments{ -\item{dem}{raster DEM that catchments were generated from} +\item{dem}{terra SpatRaster DEM that catchments were generated from} \item{channels}{channel polyline (or channels list from \code{ch_wbt_channels}) (sf object)} @@ -42,13 +42,13 @@ Generates a simple map of the drainage network plotted over the contours to allo # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = c(".tif")) no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -63,14 +63,14 @@ if (check_whitebox_binary()){ channel_raster_file <- tempfile("channels", fileext = c(".tif")) channel_vector_file <- tempfile("channels", fileext = c(".shp")) channels <- ch_wbt_channels(flow_acc_file, flow_dir_file, channel_raster_file, - channel_vector_file, 1) + channel_vector_file, 1) # get pour points pourpoint_file <- tempfile("volcano_pourpoints", fileext = ".shp") pourpoints <- ch_volcano_pourpoints(pourpoint_file) snapped_pourpoint_file <- tempfile("snapped_pourpoints", fileext = ".shp") snapped_pourpoints <- ch_wbt_pourpoints(pourpoints, flow_acc_file, pourpoint_file, - snapped_pourpoint_file, snap_dist = 10) + snapped_pourpoint_file, snap_dist = 10) ch_checkchannels(test_raster, channels, snapped_pourpoints) } else { message("Examples not run as Whitebox executable not found") diff --git a/man/ch_get_url_data.Rd b/man/ch_get_url_data.Rd index 2cb4600..9da3def 100644 --- a/man/ch_get_url_data.Rd +++ b/man/ch_get_url_data.Rd @@ -28,7 +28,7 @@ saves them locally, then accesses them locally after the first time the script i # Tested using files in the Upper Penticton Creek # zenodo repository https://zenodo.org/record/4781469 library(ggplot2) -library(raster) +library(terra) # create directory to store data sets dir_name <- tempdir(check = FALSE) @@ -46,7 +46,7 @@ head(sm_data) ra_fn <- file.path(dir_name, "gs_dem25.tif") ra_url <- "https://zenodo.org/record/4781469/files/gs_dem25.tif" ra_data <- ch_get_url_data(ra_url, ra_fn) -plot(ra_data) +terra::plot(ra_data) # test with GeoJSON gs_fn <- file.path(dir_name, "gs_soilmaps.GeoJSON") diff --git a/man/ch_volcano_raster.Rd b/man/ch_volcano_raster.Rd index 31be2ad..7f26b67 100644 --- a/man/ch_volcano_raster.Rd +++ b/man/ch_volcano_raster.Rd @@ -2,15 +2,15 @@ % Please edit documentation in R/ch_volcano_raster.R \name{ch_volcano_raster} \alias{ch_volcano_raster} -\title{Create Test Raster} +\title{Create Test SpatRaster} \usage{ ch_volcano_raster() } \value{ -Returns a raster object of land surface elevations. +Returns a SpatRaster object of land surface elevations. } \description{ -Creates a \pkg{raster} object of land surface elevations, as +Creates a \pkg{terra} object of land surface elevations, as used to test/demonstrate many functions requiring a digital elevation model (DEM). } diff --git a/man/ch_wbt_catchment.Rd b/man/ch_wbt_catchment.Rd index 1d8d5ac..92e38a5 100644 --- a/man/ch_wbt_catchment.Rd +++ b/man/ch_wbt_catchment.Rd @@ -34,13 +34,13 @@ Delineate catchment boundaries # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = ".tif") no_sink_raster_file <- tempfile("no_sinks", fileext = ".tif") # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") @@ -54,7 +54,7 @@ if (check_whitebox_binary()){ pourpoints <- ch_volcano_pourpoints(pourpoint_file) snapped_pourpoint_file <- tempfile("snapped_pourpoints", fileext = ".shp") snapped_pourpoints <- ch_wbt_pourpoints(pourpoints, flow_acc_file, pourpoint_file, - snapped_pourpoint_file, snap_dist = 10) + snapped_pourpoint_file, snap_dist = 10) # get flow directions flow_dir_file <- tempfile("flow_dir", fileext = ".tif") @@ -62,7 +62,7 @@ if (check_whitebox_binary()){ fn_catchment_ras <- tempfile("catchment", fileext = ".tif") fn_catchment_vec <- tempfile("catchment", fileext = ".shp") catchments <- ch_wbt_catchment(snapped_pourpoint_file, flow_dir_file, - fn_catchment_ras, fn_catchment_vec) + fn_catchment_ras, fn_catchment_vec) } else { message("Examples not run as Whitebox executable not found") } diff --git a/man/ch_wbt_catchment_onestep.Rd b/man/ch_wbt_catchment_onestep.Rd index f2f2392..ddc626d 100644 --- a/man/ch_wbt_catchment_onestep.Rd +++ b/man/ch_wbt_catchment_onestep.Rd @@ -72,16 +72,17 @@ are taken from the list created by the function \code{ch_wbt_filenames}. # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = c(".tif")) # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) wd <- tempdir() pourpoint_file <- tempfile("volcano_pourpoints", fileext = ".shp") pourpoints <- ch_volcano_pourpoints(pourpoint_file) catchment <- ch_wbt_catchment_onestep(wd = wd, in_dem = dem_raster_file, - pp_sf = pourpoints, sink_method = "fill", threshold = 1, snap_dist = 10) + pp_sf = pourpoints, sink_method = "fill", + threshold = 1, snap_dist = 10) } else { message("Examples not run as Whitebox executable not found") } diff --git a/man/ch_wbt_channels.Rd b/man/ch_wbt_channels.Rd index aed16d6..997508c 100644 --- a/man/ch_wbt_channels.Rd +++ b/man/ch_wbt_channels.Rd @@ -36,13 +36,13 @@ Generate stream network # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = c(".tif")) no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") diff --git a/man/ch_wbt_flow_accumulation.Rd b/man/ch_wbt_flow_accumulation.Rd index 257caaf..492ee18 100644 --- a/man/ch_wbt_flow_accumulation.Rd +++ b/man/ch_wbt_flow_accumulation.Rd @@ -12,7 +12,7 @@ ch_wbt_flow_accumulation(fn_dem_ns, fn_flowacc, return_raster = TRUE) \item{fn_flowacc}{File name for flow accumulation grid to be created.} \item{return_raster}{If \code{TRUE} (the default), the flow accumulation -grid will be returned as a raster object, in addition to being written to +grid will be returned as a SpatRaster object, in addition to being written to \option{fn_flowacc}. If \code{FALSE}, the output file will still be created but a \code{NULL} value is returned.} } @@ -27,13 +27,13 @@ Creates flow accumulation grid file # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = c(".tif")) no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") diff --git a/man/ch_wbt_flow_direction.Rd b/man/ch_wbt_flow_direction.Rd index f86efac..13a0ee1 100644 --- a/man/ch_wbt_flow_direction.Rd +++ b/man/ch_wbt_flow_direction.Rd @@ -26,13 +26,13 @@ Creates flow direction grid file # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = c(".tif")) no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") diff --git a/man/ch_wbt_pourpoints.Rd b/man/ch_wbt_pourpoints.Rd index 3cc5fac..c7f5043 100644 --- a/man/ch_wbt_pourpoints.Rd +++ b/man/ch_wbt_pourpoints.Rd @@ -45,13 +45,13 @@ channels. # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = c(".tif")) no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file) # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill") diff --git a/man/ch_wbt_removesinks.Rd b/man/ch_wbt_removesinks.Rd index ff1a544..5516b57 100644 --- a/man/ch_wbt_removesinks.Rd +++ b/man/ch_wbt_removesinks.Rd @@ -38,13 +38,13 @@ types supported are listed in \code{\link{Spatial_hydrology_functions}}. # Only proceed if Whitebox executable is installed library(whitebox) if (check_whitebox_binary()){ - library(raster) + library(terra) test_raster <- ch_volcano_raster() dem_raster_file <- tempfile(fileext = c(".tif")) no_sink_raster_file <- tempfile("no_sinks", fileext = c(".tif")) # write test raster to file - writeRaster(test_raster, dem_raster_file, format = "GTiff") + terra::writeRaster(test_raster, dem_raster_file, format = "GTiff") # remove sinks removed_sinks <- ch_wbt_removesinks(dem_raster_file, no_sink_raster_file, method = "fill")