diff --git a/.Rbuildignore b/.Rbuildignore index 47ea9e5..584bf1b 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -5,3 +5,7 @@ ^\.Rproj\.user$ ^LICENSE\.md$ ^data-raw$ +^_pkgdown\.yml$ +^docs$ +^pkgdown$ +^scripts$ diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 77ce88e..37c4025 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -6,7 +6,9 @@ on: pull_request: branches: [main, master] -name: R-CMD-check +name: R-CMD-check.yaml + +permissions: read-all jobs: R-CMD-check: @@ -30,7 +32,7 @@ jobs: R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 @@ -48,3 +50,4 @@ jobs: - uses: r-lib/actions/check-r-package@v2 with: upload-snapshots: true + build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index ed7650c..4bbce75 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -9,7 +9,9 @@ on: types: [published] workflow_dispatch: -name: pkgdown +name: pkgdown.yaml + +permissions: read-all jobs: pkgdown: @@ -22,7 +24,7 @@ jobs: permissions: contents: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 @@ -41,7 +43,7 @@ jobs: - name: Deploy to GitHub pages 🚀 if: github.event_name != 'pull_request' - uses: JamesIves/github-pages-deploy-action@v4.4.1 + uses: JamesIves/github-pages-deploy-action@v4.5.0 with: clean: false branch: gh-pages diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index c610979..8de0a0e 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -6,7 +6,9 @@ on: pull_request: branches: [main, master] -name: test-coverage +name: test-coverage.yaml + +permissions: read-all jobs: test-coverage: @@ -16,7 +18,7 @@ jobs: GOOGLE_MAPS_ELEVATION_API_KEY: ${{ secrets.GOOGLE_MAPS_ELEVATION_API_KEY }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 with: @@ -24,28 +26,37 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: any::covr + extra-packages: any::covr, any::xml2 needs: coverage - name: Test coverage run: | - covr::codecov( + cov <- covr::package_coverage( quiet = FALSE, clean = FALSE, install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") ) + covr::to_cobertura(cov) shell: Rscript {0} + - uses: codecov/codecov-action@v4 + with: + fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }} + file: ./cobertura.xml + plugin: noop + disable_search: true + token: ${{ secrets.CODECOV_TOKEN }} + - name: Show testthat output if: always() run: | ## -------------------------------------------------------------------- - find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true + find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true shell: bash - name: Upload test results if: failure() - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: coverage-test-failures path: ${{ runner.temp }}/package diff --git a/.gitignore b/.gitignore index 2793564..2676512 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ docs/ inst/docs/ +docs diff --git a/DESCRIPTION b/DESCRIPTION index 55cacb8..f17ad5f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,6 +10,7 @@ Authors@R: c( ) Description: What the package does (one paragraph). License: MIT + file LICENSE +URL: https://poissonconsulting.github.io/fwatlasbc/ Depends: R (>= 4.1) Imports: @@ -25,8 +26,8 @@ Imports: stats, stringr, tibble, - tidyselect, - tidyplus + tidyplus, + tidyselect Suggests: covr, googleway, diff --git a/R/add-blk-to-lon-lat.R b/R/add-blk-to-lon-lat.R index d8c5449..4d9d382 100644 --- a/R/add-blk-to-lon-lat.R +++ b/R/add-blk-to-lon-lat.R @@ -1,16 +1,22 @@ add_blk_to_lon_lat <- function(x, limit, tolerance, epsg, nocache) { check_dim(x, dim = nrow, values = 1L) # +chk - rm <- fwapgr::fwa_index_point(x = x$lon, y = x$lat, - limit = limit, tolerance = tolerance, - epsg = epsg, nocache = nocache) - if(!nrow(rm)) { + rm <- fwapgr::fwa_index_point( + x = x$lon, y = x$lat, + limit = limit, tolerance = tolerance, + epsg = epsg, nocache = nocache + ) + if (!nrow(rm)) { rm <- rm |> - dplyr::mutate(blk = integer(0), - rm = numeric(0), - distance_to_lon_lat = numeric(0)) |> - dplyr::select("blk", "rm", "distance_to_lon_lat", - "geometry") + dplyr::mutate( + blk = integer(0), + rm = numeric(0), + distance_to_lon_lat = numeric(0) + ) |> + dplyr::select( + "blk", "rm", "distance_to_lon_lat", + "geometry" + ) x <- x |> dplyr::bind_cols(rm) |> @@ -20,10 +26,12 @@ add_blk_to_lon_lat <- function(x, limit, tolerance, epsg, nocache) { } rm <- rm |> - dplyr::select(blk = "blue_line_key", - rm = "downstream_route_measure", - distance_to_lon_lat = "distance_to_stream", - "geometry") + dplyr::select( + blk = "blue_line_key", + rm = "downstream_route_measure", + distance_to_lon_lat = "distance_to_stream", + "geometry" + ) x |> dplyr::bind_cols(rm) |> @@ -51,8 +59,8 @@ add_blk_to_lon_lat <- function(x, limit, tolerance, epsg, nocache) { #' fwa_add_blk_to_lon_lat(data.frame(lon = -132.26, lat = 53.36)) #' } fwa_add_blk_to_lon_lat <- function(x, tolerance = 5000, limit = 1, - epsg = getOption("fwa.epsg", 3005), nocache = getOption("fwa.nocache", FALSE)) { - check_data(x, values = list(lon = c(-180,180,NA), lat = c(-90,90,NA))) + epsg = getOption("fwa.epsg", 3005), nocache = getOption("fwa.nocache", FALSE)) { + check_data(x, values = list(lon = c(-180, 180, NA), lat = c(-90, 90, NA))) check_dim(x, dim = nrow, values = TRUE) chk_not_subset(colnames(x), c("blk", "rm", "distance_to_lon_lat", "geometry")) chk_not_subset(colnames(x), "..fwa_id") @@ -61,7 +69,7 @@ fwa_add_blk_to_lon_lat <- function(x, tolerance = 5000, limit = 1, x |> dplyr::as_tibble() |> - dplyr::mutate(..fwa_id = 1:dplyr::n()) |> + dplyr::mutate(..fwa_id = seq_len(dplyr::n())) |> group_split_sf(.data$..fwa_id) |> lapply(add_blk_to_lon_lat, tolerance = tolerance, limit = limit, epsg = epsg, nocache = nocache) |> dplyr::bind_rows() |> diff --git a/R/add-blks-to-stream-name.R b/R/add-blks-to-stream-name.R index 46bd0c5..7aef081 100644 --- a/R/add-blks-to-stream-name.R +++ b/R/add-blks-to-stream-name.R @@ -24,7 +24,7 @@ fwa_add_blks_to_stream_name <- function(x, stream_name = fwatlasbc::fwa_stream_n dplyr::select("blk", "stream_name") |> dplyr::distinct() - if(!"sf" %in% class(x) && !"tbl" %in% class(x)) { + if (!"sf" %in% class(x) && !"tbl" %in% class(x)) { x <- x |> as_tibble() } diff --git a/R/add-collection-to-polygon.R b/R/add-collection-to-polygon.R index 19e4f52..ed55ca3 100644 --- a/R/add-collection-to-polygon.R +++ b/R/add-collection-to-polygon.R @@ -2,7 +2,7 @@ rename_collection <- function(collection) { chk_string(collection) # +chk wch <- which(fwatlasbc::fwa_collection_name$collection_name == collection) - if(!length(wch)) return(collection) + if (!length(wch)) return(collection) fwatlasbc::fwa_collection_name$collection[wch] } @@ -15,20 +15,21 @@ add_collection_to_polygon <- function(x, collection, filter, limit, offset, bbox <- bbox(polygon) coll <- fwapgr::fwa_query_collection(collection, - filter = filter, - limit = limit, - offset = offset, - bbox = bbox, - properties = properties, - transform = transform, - epsg = epsg, - nocache = nocache) + filter = filter, + limit = limit, + offset = offset, + bbox = bbox, + properties = properties, + transform = transform, + epsg = epsg, + nocache = nocache + ) polygon <- sf::st_transform(polygon, epsg) polygon <- sf::st_make_valid(polygon) - suppressMessages(coll <- coll[sf::st_intersects(coll, polygon, sparse = FALSE)[,1],]) + suppressMessages(coll <- coll[sf::st_intersects(coll, polygon, sparse = FALSE)[, 1], ]) coll <- sf::st_make_valid(coll) - if(x$..fwa_intersect) { + if (x$..fwa_intersect) { suppressWarnings(coll <- sf::st_intersection(coll, polygon, validate = TRUE)) coll <- sf::st_make_valid(coll) } @@ -58,7 +59,7 @@ add_collection_to_polygon <- function(x, collection, filter, limit, offset, #' individual features with the polygon as opposed to just including #' the features that intersect it. #' @return An sf object -#' @seealso \code{\link[fwapgr]{fwa_collection}}. +#' @seealso [fwapgr::fwa_collection()]. #' @export #' @examples #' \dontrun{ @@ -87,16 +88,20 @@ fwa_add_collection_to_polygon <- function( collection <- rename_collection(collection) x <- x |> - dplyr::mutate(..fwa_id = 1:dplyr::n(), - ..fwa_intersect = intersect) |> + dplyr::mutate( + ..fwa_id = seq_len(dplyr::n()), + ..fwa_intersect = intersect + ) |> group_split_sf(.data$..fwa_id) |> - lapply(add_collection_to_polygon, collection = collection, - filter = filter, limit = limit, - offset = offset, properties = properties, transform = transform, - epsg = epsg, nocache = nocache) |> + lapply(add_collection_to_polygon, + collection = collection, + filter = filter, limit = limit, + offset = offset, properties = properties, transform = transform, + epsg = epsg, nocache = nocache + ) |> dplyr::bind_rows() - if("blue_line_key" %in% colnames(x)) { + if ("blue_line_key" %in% colnames(x)) { x <- x |> mutate(blk = .data$blue_line_key) } diff --git a/R/add-collection-to-watershed.R b/R/add-collection-to-watershed.R index e7de430..1832774 100644 --- a/R/add-collection-to-watershed.R +++ b/R/add-collection-to-watershed.R @@ -14,13 +14,16 @@ fwa_add_collection_to_watershed <- function( transform = NULL, epsg = getOption("fwa.epsg", 3005), nocache = getOption("fwa.nocache", FALSE)) { + lifecycle::deprecate_soft( + "0.0.2", "fwa_add_collection_to_watershed()", + "fwa_add_collection_to_polygon()" + ) - lifecycle::deprecate_soft("0.0.2", "fwa_add_collection_to_watershed()", - "fwa_add_collection_to_polygon()") - - fwa_add_collection_to_polygon(x = x, collection = collection, - intersect = intersect, filter = filter, - limit = limit, offset = offset, - properties = properties, - transform = transform, epsg = epsg, nocache = nocache) + fwa_add_collection_to_polygon( + x = x, collection = collection, + intersect = intersect, filter = filter, + limit = limit, offset = offset, + properties = properties, + transform = transform, epsg = epsg, nocache = nocache + ) } diff --git a/R/add-cut-to-rms.R b/R/add-cut-to-rms.R index 957b02b..54df2f5 100644 --- a/R/add-cut-to-rms.R +++ b/R/add-cut-to-rms.R @@ -39,7 +39,7 @@ fwa_add_cut_to_rms <- function(x, y, cut = "cut") { x <- x |> dplyr::as_tibble() - if(!nrow(x)) { + if (!nrow(x)) { x[[cut]] <- y[[cut]][0] return(x) } @@ -49,16 +49,16 @@ fwa_add_cut_to_rms <- function(x, y, cut = "cut") { dplyr::filter(.data$blk %in% x$blk) x[[cut]] <- y[[cut]][0][1] # gets missing value - if(!nrow(y)) { + if (!nrow(y)) { return(x) } - for(i in seq_len(nrow(y))) { + for (i in seq_len(nrow(y))) { min_rm <- min(y$rm_start[i], y$rm_end[i]) max_rm <- max(y$rm_start[i], y$rm_end[i]) - x[[cut]][x$blk == y$blk[i] & x$rm >= min_rm & x$rm <= max_rm] <- y[[cut]][i] + x[[cut]][x$blk == y$blk[i] & x$rm >= min_rm & x$rm <= max_rm] <- y[[cut]][i] } - if(!identical(sf_column_name, character(0))) { + if (!identical(sf_column_name, character(0))) { x <- x |> sf::st_sf(sf_column_name = sf_column_name) } diff --git a/R/add-downstream-split-to-rms.R b/R/add-downstream-split-to-rms.R index 3998c2b..6fde97f 100644 --- a/R/add-downstream-split-to-rms.R +++ b/R/add-downstream-split-to-rms.R @@ -1,6 +1,6 @@ downstream_split_blk <- function(x, blk, rm) { x$..fwa_split[x$blk == blk & x$rm <= rm] <- TRUE - if(is.na(x$parent_blk[x$blk == blk][1])) return(x) + if (is.na(x$parent_blk[x$blk == blk][1])) return(x) downstream_split_blk(x, x$parent_blk[x$blk == blk][1], x$parent_rm[x$blk == blk][1]) } @@ -65,13 +65,13 @@ fwa_add_downstream_split_to_rms <- function(x, y) { chk_unique(y$name) chk_valid_name(y$name) - if(!nrow(y)) return(x) + if (!nrow(y)) return(x) splits <- lapply(y$name, get_downstream_split_to_rms, x = x, y = y) names(splits) <- y$name splits <- dplyr::as_tibble(splits) - if(!"tbl" %in% class(x)) { + if (!"tbl" %in% class(x)) { x <- dplyr::as_tibble(x) } diff --git a/R/add-gm-elevation-to-point.R b/R/add-gm-elevation-to-point.R index 4bade46..76a7ffd 100644 --- a/R/add-gm-elevation-to-point.R +++ b/R/add-gm-elevation-to-point.R @@ -7,7 +7,7 @@ add_gm_elevation_to_point <- function(x, digits, key) { dplyr::mutate(lon = round(.data$lon, digits), lat = round(.data$lat, digits)) elevation <- googleway::google_elevation(coords, key = key)$results$elevation - if(is.null(elevation)) { + if (is.null(elevation)) { err("Invalid Google Maps Elevation API key.") } x$elevation <- elevation @@ -30,8 +30,7 @@ add_gm_elevation_to_point <- function(x, digits, key) { #' } fwa_add_gm_elevation_to_point <- function(x, chunk_size = 300L, digits = 7, key = Sys.getenv("GOOGLE_MAPS_ELEVATION_API_KEY")) { - - if(!requireNamespace("googleway", quietly = TRUE)) { + if (!requireNamespace("googleway", quietly = TRUE)) { err("Package 'googleway' must be installed to get elevations from Google Maps.") } @@ -44,16 +43,16 @@ fwa_add_gm_elevation_to_point <- function(x, chunk_size = 300L, digits = 7, chk_range(digits, c(0, 15)) chk_string(key) - if(!nrow(x)) { + if (!nrow(x)) { x$elevation <- numeric(0) return(x) } - x$..fwa_chunk <- 1:nrow(x) %/% chunk_size + x$..fwa_chunk <- seq_len(nrow(x)) %/% chunk_size x |> group_split_sf(.data$..fwa_chunk) |> lapply(add_gm_elevation_to_point, digits = digits, key = key) |> - dplyr::bind_rows() |> - dplyr::select(!"..fwa_chunk") + dplyr::bind_rows() |> + dplyr::select(!"..fwa_chunk") } diff --git a/R/add-intersection-to-geometry.R b/R/add-intersection-to-geometry.R index 65b4b57..f4ad718 100644 --- a/R/add-intersection-to-geometry.R +++ b/R/add-intersection-to-geometry.R @@ -33,13 +33,13 @@ fwa_add_intersection_to_geometry <- function(x, y) { chk_unique(y$name) chk_valid_name(y$name) - if(!nrow(y)) return(x) + if (!nrow(y)) return(x) splits <- lapply(y$name, get_intersection_geometry, x = x, y = y) names(splits) <- y$name splits <- dplyr::as_tibble(splits) - if(!"tbl" %in% class(x)) { + if (!"tbl" %in% class(x)) { x <- dplyr::as_tibble(x) } diff --git a/R/add-new-blk-rm-to-blk-rm.R b/R/add-new-blk-rm-to-blk-rm.R index f86e4fd..4315671 100644 --- a/R/add-new-blk-rm-to-blk-rm.R +++ b/R/add-new-blk-rm-to-blk-rm.R @@ -29,7 +29,6 @@ fwa_add_new_blk_rm_to_blk_rm <- function( new_rm = "new_rm", new_blk_to = new_blk, new_rm_to = new_rm) { - chk_data(x) chk_data(y) @@ -42,7 +41,7 @@ fwa_add_new_blk_rm_to_blk_rm <- function( chk_null_or(new_blk_to, vld = vld_string) chk_string(new_rm_to) - if(vld_string(new_blk) && new_blk == blk2) { + if (vld_string(new_blk) && new_blk == blk2) { new_blk <- NULL new_blk_to <- NULL } @@ -71,7 +70,7 @@ fwa_add_new_blk_rm_to_blk_rm <- function( check_key(y, c(blk2, rm2)) - if(!is.null(new_blk)) { + if (!is.null(new_blk)) { chk_whole_numeric(y[[new_blk]]) chk_gt(y[[new_blk]]) } diff --git a/R/add-rms-to-blk.R b/R/add-rms-to-blk.R index 4a4c5b5..6e32ac7 100644 --- a/R/add-rms-to-blk.R +++ b/R/add-rms-to-blk.R @@ -5,24 +5,27 @@ add_rms_to_blk <- function(x, epsg, nocache) { start <- x$..fwa_start end <- x$..fwa_end - if(is.infinite(end)) end <- NULL + if (is.infinite(end)) end <- NULL rm <- fwa_locate_along_interval(x$blk, - interval_length = interval, - start_measure = start, - end_measure = end, - epsg = epsg, - nocache = nocache) |> - dplyr::mutate(rm = .data$index * interval + start, - rm = as.integer(.data$rm), - elevation = unname(sf::st_coordinates(.data$geometry)[,"Z"])) |> + interval_length = interval, + start_measure = start, + end_measure = end, + epsg = epsg, + nocache = nocache + ) |> + dplyr::mutate( + rm = .data$index * interval + start, + rm = as.integer(.data$rm), + elevation = unname(sf::st_coordinates(.data$geometry)[, "Z"]) + ) |> sf::st_zm(x) |> dplyr::select("rm", "elevation", "geometry") - if(!is.null(end)) { + if (!is.null(end)) { lim <- floor((end - start) / interval) - if(nrow(x) < lim) + if (nrow(x) < lim) chk::wrn("`end` was not reached for blk ", x$blk) } x |> @@ -51,7 +54,7 @@ add_rms_to_blk <- function(x, epsg, nocache) { #' } fwa_add_rms_to_blk <- function( x, interval = 1000, start = 0, end = Inf, - epsg = getOption("fwa.epsg", 3005), nocache = getOption("fwa.nocache", FALSE)){ + epsg = getOption("fwa.epsg", 3005), nocache = getOption("fwa.nocache", FALSE)) { check_data(x) check_dim(x, dim = nrow, values = TRUE) chk_whole_numeric(x$blk) @@ -59,8 +62,10 @@ fwa_add_rms_to_blk <- function( chk_gt(x$blk) chk_unique(x$blk) chk_not_subset(colnames(x), c("rm", "elevation", "geometry")) - chk_not_subset(colnames(x), c("..fwa_interval", "..fwa_start", - "..fwa_end", "..fwa_id")) + chk_not_subset(colnames(x), c( + "..fwa_interval", "..fwa_start", + "..fwa_end", "..fwa_id" + )) chk_whole_number(interval) chk_gt(interval) @@ -73,14 +78,18 @@ fwa_add_rms_to_blk <- function( x |> dplyr::as_tibble() |> - dplyr::mutate(..fwa_interval = interval, - ..fwa_start = start, - ..fwa_end = end, - ..fwa_id = 1:dplyr::n()) |> + dplyr::mutate( + ..fwa_interval = interval, + ..fwa_start = start, + ..fwa_end = end, + ..fwa_id = seq_len(dplyr::n()) + ) |> group_split_sf(.data$blk) |> lapply(add_rms_to_blk, epsg = epsg, nocache = nocache) |> dplyr::bind_rows() |> dplyr::arrange(.data$..fwa_id, .data$rm) |> - dplyr::select(!c("..fwa_interval", "..fwa_start", - "..fwa_end", "..fwa_id")) + dplyr::select(!c( + "..fwa_interval", "..fwa_start", + "..fwa_end", "..fwa_id" + )) } diff --git a/R/add-section-to-rms.R b/R/add-section-to-rms.R index 007b554..acef0d8 100644 --- a/R/add-section-to-rms.R +++ b/R/add-section-to-rms.R @@ -40,7 +40,7 @@ fwa_add_section_to_rms <- function(x, y, section = "section") { x <- x |> dplyr::as_tibble() - if(!nrow(x)) { + if (!nrow(x)) { x[[section]] <- y[[section]][0] return(x) } @@ -51,14 +51,14 @@ fwa_add_section_to_rms <- function(x, y, section = "section") { dplyr::filter(.data$blk %in% x$blk) x[[section]] <- y[[section]][0][1] # gets missing value - if(!nrow(y)) { + if (!nrow(y)) { return(x) } - for(i in seq_len(nrow(y))) { - x[[section]][x$blk == y$blk[i] & x$rm <= y$rm[i]] <- y[[section]][i] + for (i in seq_len(nrow(y))) { + x[[section]][x$blk == y$blk[i] & x$rm <= y$rm[i]] <- y[[section]][i] } - if(!identical(sf_column_name, character(0))) { + if (!identical(sf_column_name, character(0))) { x <- x |> sf::st_sf(sf_column_name = sf_column_name) } diff --git a/R/add-stream-name-to-blk.R b/R/add-stream-name-to-blk.R index 6016320..f9bf6f3 100644 --- a/R/add-stream-name-to-blk.R +++ b/R/add-stream-name-to-blk.R @@ -27,7 +27,7 @@ fwa_add_stream_names_to_blk <- function(x, stream_name = fwatlasbc::fwa_stream_n dplyr::select("blk", "stream_name") |> dplyr::distinct() - if(!"sf" %in% class(x) && !"tbl" %in% class(x)) { + if (!"sf" %in% class(x) && !"tbl" %in% class(x)) { x <- x |> as_tibble() } diff --git a/R/add-upstream-split-to-rms.R b/R/add-upstream-split-to-rms.R index 4102273..69fabdb 100644 --- a/R/add-upstream-split-to-rms.R +++ b/R/add-upstream-split-to-rms.R @@ -4,7 +4,7 @@ split_blk <- function(x, blk, rm = 0) { blks <- x$blk[!is.na(x$parent_blk) & x$parent_blk == blk & x$parent_rm >= rm] |> unique() - for(blk in blks) { + for (blk in blks) { x <- split_blk(x, blk) } x @@ -71,13 +71,13 @@ fwa_add_upstream_split_to_rms <- function(x, y) { chk_unique(y$name) chk_valid_name(y$name) - if(!nrow(y)) return(x) + if (!nrow(y)) return(x) splits <- lapply(y$name, get_split_to_rms, x = x, y = y) names(splits) <- y$name splits <- dplyr::as_tibble(splits) - if(!"tbl" %in% class(x)) { + if (!"tbl" %in% class(x)) { x <- dplyr::as_tibble(x) } diff --git a/R/add-watershed-to-blk.R b/R/add-watershed-to-blk.R index e19601f..100d005 100644 --- a/R/add-watershed-to-blk.R +++ b/R/add-watershed-to-blk.R @@ -1,20 +1,21 @@ adjust_watershed <- function(wshed, x, epsg, nocache) { - - if(x$..fwa_exclude && wshed$refine_method == "DROP") { + if (x$..fwa_exclude && wshed$refine_method == "DROP") { return(wshed) } - if(!x$..fwa_exclude && wshed$refine_method == "KEEP") { + if (!x$..fwa_exclude && wshed$refine_method == "KEEP") { return(wshed) } - fwshed <- fwa_watershed_hex(blue_line_key = x$blk, - downstream_route_measure = x$rm, - epsg = epsg, nocache = nocache) + fwshed <- fwa_watershed_hex( + blue_line_key = x$blk, + downstream_route_measure = x$rm, + epsg = epsg, nocache = nocache + ) fgeometry <- sf::st_union(fwshed$geometry) - if(x$..fwa_exclude) { + if (x$..fwa_exclude) { wshed$geometry <- sf::st_difference(wshed$geometry, fgeometry) } else { wshed$geometry <- sf::st_union(wshed$geometry, fgeometry) @@ -25,10 +26,12 @@ adjust_watershed <- function(wshed, x, epsg, nocache) { add_watershed_to_blk <- function(x, epsg, nocache) { check_dim(x, dim = nrow, values = 1L) # +chk - wshed <- fwa_watershed_at_measure(blue_line_key = x$blk, - downstream_route_measure = x$rm, - epsg = epsg, - nocache = nocache) + wshed <- fwa_watershed_at_measure( + blue_line_key = x$blk, + downstream_route_measure = x$rm, + epsg = epsg, + nocache = nocache + ) wshed <- adjust_watershed(wshed, x, epsg, nocache = nocache) @@ -51,7 +54,7 @@ add_watershed_to_blk <- function(x, epsg, nocache) { #' @param exclude A logical vector specifying whether to exclude the #' fundamental watershed in which the start falls. #' @return A sf object -#' @seealso \code{\link[fwapgr]{fwa_watershed_at_measure}}. +#' @seealso [fwapgr::fwa_watershed_at_measure()]. #' @export #' @examples #' \dontrun{ @@ -66,7 +69,7 @@ fwa_add_watershed_to_blk <- function(x, chk_whole_numeric(x$blk) chk_not_any_na(x$blk) chk_gt(x$blk) - if(!"rm" %in% names(x)) x$rm <- NA_real_ + if (!"rm" %in% names(x)) x$rm <- NA_real_ chk_numeric(x$rm) chk_gte(x$rm) x$rm[is.na(x$rm)] <- 0 @@ -80,8 +83,10 @@ fwa_add_watershed_to_blk <- function(x, x |> dplyr::as_tibble() |> - dplyr::mutate(..fwa_exclude = exclude, - ..fwa_id = 1:dplyr::n()) |> + dplyr::mutate( + ..fwa_exclude = exclude, + ..fwa_id = seq_len(dplyr::n()) + ) |> group_split_sf(.data$blk) |> lapply(add_watershed_to_blk, epsg = epsg, nocache = nocache) |> dplyr::bind_rows() |> diff --git a/R/convert-stream-names-to-blks.R b/R/convert-stream-names-to-blks.R index 57e8c66..019bc44 100644 --- a/R/convert-stream-names-to-blks.R +++ b/R/convert-stream-names-to-blks.R @@ -15,7 +15,7 @@ #' fwa_convert_stream_names_to_blks(c("a stream name", "a stream name2")) fwa_convert_stream_names_to_blks <- function(names) { chk_null_or(names, vld = vld_character_or_factor) - if(!length(names) || is.null(names)) return(integer()) + if (!length(names) || is.null(names)) return(integer()) names_chr <- as.character(names) names_na <- is.na(names_chr) names_int <- abs(digest::digest2int(names_chr)) diff --git a/R/convert-stream-network-to-rms.R b/R/convert-stream-network-to-rms.R index e51835a..51888c5 100644 --- a/R/convert-stream-network-to-rms.R +++ b/R/convert-stream-network-to-rms.R @@ -19,9 +19,11 @@ add_parent_blk_rm <- function(x) { dplyr::filter(!str_detect(.data$fwa_watershed_code, "^999")) |> dplyr::mutate(fwa_watershed_code = strip_trailing_0s(.data$fwa_watershed_code)) |> dplyr::group_by(.data$blue_line_key, .data$fwa_watershed_code) |> - dplyr::summarise(min_rm = min(.data$upstream_route_measure), - max_rm = max(.data$upstream_route_measure), - max_stream_order = max(.data$stream_order)) |> + dplyr::summarise( + min_rm = min(.data$upstream_route_measure), + max_rm = max(.data$upstream_route_measure), + max_stream_order = max(.data$stream_order) + ) |> dplyr::ungroup() |> dplyr::filter(.data$min_rm != 0) @@ -34,9 +36,11 @@ add_parent_blk_rm <- function(x) { child <- parent |> dplyr::mutate( parent_code = get_parent_code(.data$fwa_watershed_code), - parent_proportion = get_parent_proportion(.data$fwa_watershed_code)) |> + parent_proportion = get_parent_proportion(.data$fwa_watershed_code) + ) |> dplyr::select( - child_blk = "blue_line_key", "parent_code", "parent_proportion") + child_blk = "blue_line_key", "parent_code", "parent_proportion" + ) parent <- parent |> dplyr::filter(.data$max_stream_order > 1) @@ -58,7 +62,7 @@ convert_stream_segment_to_rms <- function(x, interval) { down_rm <- round_any(down, interval, ceiling) up_rm <- round_any(up, interval, floor) - if(down_rm > up_rm) { + if (down_rm > up_rm) { rm <- integer(0) sample <- numeric(0) } else { @@ -69,17 +73,17 @@ convert_stream_segment_to_rms <- function(x, interval) { y <- x |> dplyr::as_tibble() - if("geometry" %in% names(y)) + if ("geometry" %in% names(y)) y$geometry <- NULL - if("rm" %in% names(y)) + if ("rm" %in% names(y)) y$rm <- NULL x <- x |> sf::st_line_sample(sample = sample) |> sf::st_cast("POINT") - dplyr::tibble(geometry = x, rm = rm) |> + dplyr::tibble(geometry = x, rm = rm) |> sf::st_sf() |> dplyr::mutate(id = y$id) |> dplyr::left_join(y, by = "id") |> @@ -113,8 +117,10 @@ fwa_convert_stream_network_to_rms <- function(x, interval = 5, tolerance = 0.1) chk_number(tolerance) chk_gte(tolerance) - check_names(x, c("linear_feature_id", "blue_line_key", "downstream_route_measure", - "upstream_route_measure", "fwa_watershed_code")) + check_names(x, c( + "linear_feature_id", "blue_line_key", "downstream_route_measure", + "upstream_route_measure", "fwa_watershed_code" + )) chk_not_subset(colnames(x), "..fwa_id") x$id <- x$linear_feature_id @@ -141,13 +147,13 @@ fwa_convert_stream_network_to_rms <- function(x, interval = 5, tolerance = 0.1) diff <- diff - as.numeric(sf::st_length(x$geometry)) diff <- abs(diff) - if(any(diff > tolerance)) { + if (any(diff > tolerance)) { abort_chk("Difference between and upstream and down route measures and length of geometry exceeds tolerance in `x`") } x |> add_parent_blk_rm() |> - dplyr::mutate(..fwa_id = 1:dplyr::n()) |> + dplyr::mutate(..fwa_id = seq_len(dplyr::n())) |> group_split_sf(.data$..fwa_id) |> lapply(convert_stream_segment_to_rms, interval = interval) |> dplyr::bind_rows() |> diff --git a/R/convert-streams-to-rms.R b/R/convert-streams-to-rms.R index 0cf637b..2513fa8 100644 --- a/R/convert-streams-to-rms.R +++ b/R/convert-streams-to-rms.R @@ -1,24 +1,25 @@ get_parent_blk_rm <- function(x, y, gap) { - y <- y |> dplyr::anti_join(as_tibble(x), by = "blk") x <- x |> - dplyr::mutate(parent_blk = sf::st_nearest_feature(x, y), - parent_blk = y$blk[.data$parent_blk]) |> + dplyr::mutate( + parent_blk = sf::st_nearest_feature(x, y), + parent_blk = y$blk[.data$parent_blk] + ) |> dplyr::left_join(as_tibble(y), by = c("parent_blk" = "blk")) span <- sf::st_nearest_points(x$geometry, x$..fwa_linestring, pairwise = TRUE) distance <- sf::st_length(span) |> as.numeric() - if(distance > gap) { + if (distance > gap) { x$parent_blk <- NA_integer_ x$parent_rm <- NA_real_ } else { point <- sf::st_cast(span, "POINT") |> dplyr::nth(2) - if(distance > 0) { + if (distance > 0) { point <- sf::st_buffer(point, distance) } @@ -99,7 +100,7 @@ fwa_convert_streams_to_rms <- function(x, interval = 5, gap = 1, end = NULL, ele chk_gt(x$blk) chk_null_or(end, vld = vld_whole_number) - if(is.null(end)) { + if (is.null(end)) { end <- interval + 1 } chk_gt(end) diff --git a/R/find-stream-names.R b/R/find-stream-names.R index 7ceec36..e1ac915 100644 --- a/R/find-stream-names.R +++ b/R/find-stream-names.R @@ -10,7 +10,7 @@ #' @export #' @examples #' fwa_find_stream_names("sangan") -fwa_find_stream_names <- function(pattern = ".*", ignore_case = TRUE){ +fwa_find_stream_names <- function(pattern = ".*", ignore_case = TRUE) { chk_string(pattern) chk_flag(ignore_case) diff --git a/R/get-section-from-rms.R b/R/get-section-from-rms.R index e95ea1f..1e6232d 100644 --- a/R/get-section-from-rms.R +++ b/R/get-section-from-rms.R @@ -31,18 +31,20 @@ fwa_get_section_from_rms <- function(x, section = "section") { as_tibble() |> dplyr::filter(!is.na(.data[[section]])) |> dplyr::group_by(.data$blk, .data[[section]]) |> - dplyr::summarise(length = max(.data$rm) - min(.data$rm), - rm = max(.data$rm)) |> + dplyr::summarise( + length = max(.data$rm) - min(.data$rm), + rm = max(.data$rm) + ) |> dplyr::ungroup() |> dplyr::select(tidyselect::all_of(section), "blk", "rm", "length") |> dplyr::arrange(.data$blk, .data$rm) - if(!identical(sf_column_name, character(0))) { + if (!identical(sf_column_name, character(0))) { x <- x |> dplyr::select("blk", "rm") - y <- y |> - dplyr::inner_join(x, by = c("blk", "rm")) |> - sf::st_sf(sf_column_name = sf_column_name) + y <- y |> + dplyr::inner_join(x, by = c("blk", "rm")) |> + sf::st_sf(sf_column_name = sf_column_name) } y } diff --git a/R/join-stream-segments.R b/R/join-stream-segments.R index 8f41e59..16b4fad 100644 --- a/R/join-stream-segments.R +++ b/R/join-stream-segments.R @@ -49,12 +49,14 @@ start_end_elevation <- function(x, reverse) { start_points <- function(x, elevation, reverse) { x <- x |> - dplyr::mutate(start = sf::st_line_sample(x, sample = 0), - end = sf::st_line_sample(x, sample = 1), - start = sf::st_cast(.data$start, "POINT"), - end = sf::st_cast(.data$end, "POINT")) + dplyr::mutate( + start = sf::st_line_sample(x, sample = 0), + end = sf::st_line_sample(x, sample = 1), + start = sf::st_cast(.data$start, "POINT"), + end = sf::st_cast(.data$end, "POINT") + ) - if(elevation || length(reverse)) { + if (elevation || length(reverse)) { x <- x |> start_end_elevation(reverse) } diff --git a/R/mapview-rms-to-rms.R b/R/mapview-rms-to-rms.R index 71a8f05..b7c72f1 100644 --- a/R/mapview-rms-to-rms.R +++ b/R/mapview-rms-to-rms.R @@ -23,7 +23,9 @@ join_points <- function(x, y) { z <- x |> dplyr::inner_join(y, by = c("blk" = "blk", "new_rm" = "rm")) - z$geometry <- sf::st_sfc(mapply(function(a,b){sf::st_cast(sf::st_union(a,b),"LINESTRING")}, z$..fwa_geometry1, z$..fwa_geometry2, SIMPLIFY=FALSE), crs = crs) + z$geometry <- sf::st_sfc(mapply(function(a, b) { + sf::st_cast(sf::st_union(a, b), "LINESTRING") + }, z$..fwa_geometry1, z$..fwa_geometry2, SIMPLIFY = FALSE), crs = crs) z |> dplyr::select(-"..fwa_geometry1", -"..fwa_geometry2") |> @@ -40,8 +42,7 @@ join_points <- function(x, y) { #' @inheritParams fwa_mapview #' @export fwa_mapview_rms_to_rms <- function(x, y, zcol = "rm", npoint = 250) { - - if(!requireNamespace("mapview", quietly = TRUE)) { + if (!requireNamespace("mapview", quietly = TRUE)) { err("Package 'mapview' must be installed.") } @@ -58,7 +59,7 @@ fwa_mapview_rms_to_rms <- function(x, y, zcol = "rm", npoint = 250) { chk_whole_number(npoint) chk_gt(npoint) - if(!is.null(zcol)) { + if (!is.null(zcol)) { check_names(x, zcol) } diff --git a/R/mapview-rms.R b/R/mapview-rms.R index 07c2097..59fbf18 100644 --- a/R/mapview-rms.R +++ b/R/mapview-rms.R @@ -2,7 +2,7 @@ thin_points <- function(x, npoint) { x <- x |> dplyr::arrange(.data$blk, .data$rm) - if(nrow(x) <= npoint) { + if (nrow(x) <= npoint) { return(x) } @@ -10,10 +10,12 @@ thin_points <- function(x, npoint) { dplyr::group_by(.data$blk) |> dplyr::summarise(..fwa_n = n()) |> dplyr::ungroup() |> - dplyr::mutate(..fwa_p = .data$..fwa_n / sum(.data$..fwa_n), - ..fwa_np = .data$..fwa_p * npoint, - ..fwa_np = ceiling(.data$..fwa_np), - ..fwa_np = pmax(.data$..fwa_np, 2L)) |> + dplyr::mutate( + ..fwa_p = .data$..fwa_n / sum(.data$..fwa_n), + ..fwa_np = .data$..fwa_p * npoint, + ..fwa_np = ceiling(.data$..fwa_np), + ..fwa_np = pmax(.data$..fwa_np, 2L) + ) |> dplyr::inner_join(x, by = "blk", multiple = "all") |> dplyr::group_by(.data$blk) |> dplyr::mutate(..fwa_np = pmin(.data$..fwa_np, .data$..fwa_n)) |> @@ -31,8 +33,7 @@ thin_points <- function(x, npoint) { #' @export fwa_mapview_rms <- function(x, layer = NULL, zcol = "rm", legend = FALSE, npoint = 250, ...) { - - if(!requireNamespace("mapview", quietly = TRUE)) { + if (!requireNamespace("mapview", quietly = TRUE)) { err("Package 'mapview' must be installed.") } @@ -46,10 +47,10 @@ fwa_mapview_rms <- function(x, layer = NULL, zcol = "rm", legend = FALSE, npoint chk_whole_number(npoint) chk_gt(npoint) - if(!is.null(layer)) { + if (!is.null(layer)) { check_names(x, layer) } - if(!is.null(zcol)) { + if (!is.null(zcol)) { check_names(x, zcol) } diff --git a/R/mapview.R b/R/mapview.R index 10129d2..242c580 100644 --- a/R/mapview.R +++ b/R/mapview.R @@ -10,12 +10,11 @@ #' @param ... Additional arguments passed to `mapview::mapview()`. #' @export fwa_mapview <- function(x, layer = NULL, zcol = NULL, legend = FALSE, ...) { - - if(!requireNamespace("mapview", quietly = TRUE)) { + if (!requireNamespace("mapview", quietly = TRUE)) { err("Package 'mapview' must be installed.") } - if(!requireNamespace("hms", quietly = TRUE)) { + if (!requireNamespace("hms", quietly = TRUE)) { err("Package 'hms' must be installed.") } @@ -23,13 +22,13 @@ fwa_mapview <- function(x, layer = NULL, zcol = NULL, legend = FALSE, ...) { x <- x |> dplyr::mutate( - dplyr::across(tidyselect::vars_select_helpers$where(hms::is_hms), as.character)) + dplyr::across(tidyselect::vars_select_helpers$where(hms::is_hms), as.character) + ) - if(!is.null(layer)) { + if (!is.null(layer)) { x <- x |> split(x[[layer]], drop = TRUE) } mapview::mapview(x, zcol = zcol, legend = legend, ...) } - diff --git a/R/parent-blk-rms.R b/R/parent-blk-rms.R index 8d7d118..11daa1f 100644 --- a/R/parent-blk-rms.R +++ b/R/parent-blk-rms.R @@ -23,12 +23,12 @@ fwa_parent_blk_rms <- function(x, rms) { rms <- rms |> dplyr::distinct(.data$blk, .data$parent_blk) - if(anyDuplicated(rms$blk)) { + if (anyDuplicated(rms$blk)) { abort_chk("Each blk in `rms` must have one parent_blk value.") } - if(!length(x)) return(integer(0)) - if(!nrow(rms)) return(rep(NA_integer_, length(x))) + if (!length(x)) return(integer(0)) + if (!nrow(rms)) return(rep(NA_integer_, length(x))) x <- dplyr::tibble(blk = x) |> dplyr::left_join(rms, by = "blk") diff --git a/R/parent-stream-name-rms.R b/R/parent-stream-name-rms.R index 06f5814..1e8c078 100644 --- a/R/parent-stream-name-rms.R +++ b/R/parent-stream-name-rms.R @@ -36,12 +36,12 @@ fwa_parent_stream_name_rms <- function(x, rms, stream_name = fwatlasbc::fwa_stre sname <- stream_name |> dplyr::filter(.data$stream_name %in% x) - if(anyDuplicated(sname$stream_name)) { + if (anyDuplicated(sname$stream_name)) { abort_chk("Each `x` stream_name value must have one blk value in `stream_name`.") } - if(!length(x)) return(character(0)) - if(!nrow(rms)) return(rep(NA_character_, length(x))) + if (!length(x)) return(character(0)) + if (!nrow(rms)) return(rep(NA_character_, length(x))) x <- x |> dplyr::tibble(stream_name = x) |> diff --git a/R/prune-rms.R b/R/prune-rms.R index d310e80..a791806 100644 --- a/R/prune-rms.R +++ b/R/prune-rms.R @@ -16,7 +16,7 @@ fwa_prune_rms <- function(x, y) { y$name <- "..fwa_prune" n <- nrow(y) - for(i in seq_len(n)) { + for (i in seq_len(n)) { z <- y |> dplyr::slice(n) x <- x |> fwa_add_upstream_split_to_rms(z) |> diff --git a/R/snap-rm-to-point.R b/R/snap-rm-to-point.R index fa3b5ae..6038772 100644 --- a/R/snap-rm-to-point.R +++ b/R/snap-rm-to-point.R @@ -1,6 +1,6 @@ nearest_rm <- function(x, rm) { index <- sf::st_nearest_feature(x, rm) - rm <- rm[index,] + rm <- rm[index, ] x$..fwa_blk <- rm$blk x$rm <- rm$rm x$distance_to_rm <- sf::st_distance(x, rm, by_element = TRUE) @@ -9,7 +9,7 @@ nearest_rm <- function(x, rm) { } adjust_rm <- function(rm, x) { - if(ncol(rm) == 3) { + if (ncol(rm) == 3) { return(rm) } @@ -24,7 +24,7 @@ adjust_rm <- function(rm, x) { dplyr::select(tidyselect::vars_select_helpers$where(function(x) !is.na(x[1]))) |> colnames() - if(!length(cols)) { + if (!length(cols)) { return(rm) } rm |> @@ -32,12 +32,12 @@ adjust_rm <- function(rm, x) { } snap_rm_to_point <- function(x, rm) { - if(!is.na(x$blk[1])) { - rm <- rm[rm$blk == x$blk[1],] + if (!is.na(x$blk[1])) { + rm <- rm[rm$blk == x$blk[1], ] } rm <- adjust_rm(rm, x) - if(!nrow(rm)) { + if (!nrow(rm)) { x$rm <- NA_integer_ x$distance_to_rm <- NA_real_ return(x) @@ -60,14 +60,14 @@ snap_rm_to_point <- function(x, rm) { #' @export #' @examples #' rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) -#' x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] -#' rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] +#' x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] +#' rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] #' fwa_snap_rm_to_point(x, rm) fwa_snap_rm_to_point <- function(x, rm, ...) { chk::chk_s3_class(x, "sf") chk::chk_s3_class(rm, "sf") - if(!has_name(x, "blk")) x$blk <- NA_integer_ + if (!has_name(x, "blk")) x$blk <- NA_integer_ chk_whole_numeric(x$blk) chk_gt(x$blk) @@ -82,12 +82,12 @@ fwa_snap_rm_to_point <- function(x, rm, ...) { chk_not_any_na(rm$rm) chk_gte(rm$rm) - if(!nrow(x)) { + if (!nrow(x)) { x$rm <- integer(0) x$distance_to_rm <- numeric(0) return(x) } - if(!nrow(rm)) { + if (!nrow(rm)) { x$rm <- NA_integer_ x$distance_to_rm <- NA_real_ return(x) @@ -99,7 +99,7 @@ fwa_snap_rm_to_point <- function(x, rm, ...) { dplyr::select(rm, "blk", "rm", ...) x |> - dplyr::mutate(..fwa_id = 1:dplyr::n()) |> + dplyr::mutate(..fwa_id = seq_len(dplyr::n())) |> group_split_sf(.data$blk, ...) |> lapply(snap_rm_to_point, rm = rm) |> dplyr::bind_rows() |> @@ -107,7 +107,8 @@ fwa_snap_rm_to_point <- function(x, rm, ...) { dplyr::mutate( ..fwa_blk = as.integer(.data$..fwa_blk), blk = as.integer(.data$blk), - blk = dplyr::if_else(is.na(.data$..fwa_blk), .data$blk, .data$..fwa_blk)) |> + blk = dplyr::if_else(is.na(.data$..fwa_blk), .data$blk, .data$..fwa_blk) + ) |> dplyr::relocate("distance_to_rm", .after = "rm") |> dplyr::select(!c("..fwa_id", "..fwa_blk")) } diff --git a/R/snap-rm-to-rms.R b/R/snap-rm-to-rms.R index f8ad901..5c06a82 100644 --- a/R/snap-rm-to-rms.R +++ b/R/snap-rm-to-rms.R @@ -4,7 +4,7 @@ new_blk_to_blk <- function(x) { dplyr::rename(blk = "new_blk") } -relocate_blks_new_rm <- function (data) { +relocate_blks_new_rm <- function(data) { data |> dplyr::relocate("blk", "new_blk") |> dplyr::relocate("new_rm", .after = "rm") |> @@ -13,20 +13,20 @@ relocate_blks_new_rm <- function (data) { distance_to_rm <- function(x, rms) { fac <- factor(rms$rm, levels = rms$rm) - rm <- rms[as.integer(factor(x$rm, levels = levels(fac))),] + rm <- rms[as.integer(factor(x$rm, levels = levels(fac))), ] x$distance_to_rm <- sf::st_distance(x, rm, by_element = TRUE) x$distance_to_rm <- as.numeric(x$distance_to_rm) x } prev_cummax <- function(x) { - if(!length(x)) return(x) + if (!length(x)) return(x) c(x[1], cummax(x)[-length(x)]) } interpolate_block <- function(x, start, end) { - ax <- c(x$..fwa_x_rm[start-1], x$..fwa_x_rm[end+1]) - ay <- c(x$rm[start-1], x$rm[end+1]) + ax <- c(x$..fwa_x_rm[start - 1], x$..fwa_x_rm[end + 1]) + ay <- c(x$rm[start - 1], x$rm[end + 1]) indices <- start:end xout <- x$..fwa_x_rm[indices] @@ -50,35 +50,35 @@ reallocate_blocks <- function(x, rms) { df$start <- df$end - df$length + 1 - df <- df[!is.na(df$values) & df$length > 1,] + df <- df[!is.na(df$values) & df$length > 1, ] - if(!nrow(df)) return(x) + if (!nrow(df)) return(x) - if(df$start[1] == 1) { + if (df$start[1] == 1) { df$start[1] <- 2 df$length[1] <- df$length[1] - 1 } nrm <- length(x$rm) ndf <- nrow(df) - if(df$end[ndf] == nrm) { + if (df$end[ndf] == nrm) { df$end[ndf] <- nrm - 1 df$length[ndf] <- df$length[ndf] - 1 } - df <- df[df$length > 1,] + df <- df[df$length > 1, ] - if(!nrow(df)) return(x) + if (!nrow(df)) return(x) xrev <- x - for(i in nrow(df):1) { + for (i in rev(seq_len(nrow(df)))) { xrev <- interpolate_block(xrev, start = df$start[i], end = df$end[i]) } - for(i in 1:nrow(df)) { + for (i in seq_len(nrow(df))) { x <- interpolate_block(x, start = df$start[i], end = df$end[i]) indices <- df$start[i]:df$end[i] x$rm[indices] <- pmean(x$rm[indices], xrev$rm[indices]) - for(j in indices) { + for (j in indices) { x$rm[j] <- rms$rm[which.min(abs(x$rm[j] - rms$rm))] } } @@ -97,13 +97,13 @@ update_rms <- function(x, rms) { x$rm[provided] <- x$..fwa_provided_new_rm[provided] wch <- which(provided) - for(id in wch) { + for (id in wch) { x$rm[1:id] <- pmin(x$rm[1:id], x$rm[id]) } x <- to_prev_max(x) x <- reallocate_blocks(x, rms) x <- to_prev_max(x) - if(!vld_sorted(x$rm)) { + if (!vld_sorted(x$rm)) { stop("generated new_rm should be sorted end") } x @@ -158,8 +158,8 @@ snap_rm_to_rms <- function(x, rms) { #' @export #' @examples #' rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) -#' x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] -#' rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] +#' x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] +#' rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] #' fwa_snap_rm_to_rms(x, rm) fwa_snap_rm_to_rms <- function(x, rm, snap_mouths = FALSE) { chk::chk_s3_class(x, "sf") @@ -188,15 +188,15 @@ fwa_snap_rm_to_rms <- function(x, rm, snap_mouths = FALSE) { chk_gte(rm$rm) check_key(rm, c("blk", "rm")) - if(has_name(x, "new_rm")) { + if (has_name(x, "new_rm")) { chk_whole_numeric(x$new_rm) chk_gte(x$new_rm) - if(!vld_join(x[!is.na(x$new_rm),], rm, c(blk = "blk", new_rm = "rm"))) { + if (!vld_join(x[!is.na(x$new_rm), ], rm, c(blk = "blk", new_rm = "rm"))) { chk::abort_chk("All `x$new_rm` values must be in `rm$rm` by `blk`") } } - if(has_name(x, "new_blk")) { + if (has_name(x, "new_blk")) { chk_whole_numeric(x$new_blk) chk_not_any_na(x$new_blk) chk_gt(x$new_blk) @@ -207,24 +207,26 @@ fwa_snap_rm_to_rms <- function(x, rm, snap_mouths = FALSE) { blks <- x |> dplyr::distinct(.data$blk, .data$new_blk) - if(!vld_unique(blks$blk)) { + if (!vld_unique(blks$blk)) { abort_chk("Each blk in `x` must map to at most one new_blk.") } - if(!nrow(x)) { + if (!nrow(x)) { x <- x |> tidyplus::add_missing_column( new_rm = integer(0), - distance_to_new_rm = numeric(0)) |> + distance_to_new_rm = numeric(0) + ) |> relocate_blks_new_rm() return(x) } - if(!nrow(rm)) { + if (!nrow(rm)) { x <- x |> tidyplus::add_missing_column( new_rm = NA_integer_, - distance_to_new_rm = NA_real_) |> + distance_to_new_rm = NA_real_ + ) |> relocate_blks_new_rm() return(x) } @@ -239,7 +241,7 @@ fwa_snap_rm_to_rms <- function(x, rm, snap_mouths = FALSE) { x$new_rm <- as.integer(x$new_rm) - if(snap_mouths) { + if (snap_mouths) { mouths <- rm |> dplyr::as_tibble() |> dplyr::filter(.data$rm == 0) |> @@ -253,19 +255,25 @@ fwa_snap_rm_to_rms <- function(x, rm, snap_mouths = FALSE) { x |> dplyr::arrange("blk", "rm") |> - dplyr::mutate(..fwa_id = 1:dplyr::n()) |> - dplyr::rename(..fwa_provided_new_rm = "new_rm", - ..fwa_x_rm = "rm") |> + dplyr::mutate(..fwa_id = seq_len(dplyr::n())) |> + dplyr::rename( + ..fwa_provided_new_rm = "new_rm", + ..fwa_x_rm = "rm" + ) |> group_split_sf(.data$blk) |> lapply(snap_rm_to_rms, rm = rm) |> dplyr::bind_rows() |> - dplyr::rename(new_rm = "rm", - distance_to_new_rm = "distance_to_rm", - rm = "..fwa_x_rm") |> - dplyr::mutate(blk = as.integer(.data$blk), - new_blk = as.integer(.data$new_blk), - rm = as.integer(.data$rm), - new_rm = as.integer(.data$new_rm)) |> + dplyr::rename( + new_rm = "rm", + distance_to_new_rm = "distance_to_rm", + rm = "..fwa_x_rm" + ) |> + dplyr::mutate( + blk = as.integer(.data$blk), + new_blk = as.integer(.data$new_blk), + rm = as.integer(.data$rm), + new_rm = as.integer(.data$new_rm) + ) |> dplyr::select(!c("..fwa_id", "..fwa_blk", "..fwa_provided_new_rm")) |> relocate_blks_new_rm() |> dplyr::arrange(.data$blk, .data$rm) diff --git a/R/snap-rms-to-rms.R b/R/snap-rms-to-rms.R index 7bb77cb..5e539cd 100644 --- a/R/snap-rms-to-rms.R +++ b/R/snap-rms-to-rms.R @@ -1,23 +1,23 @@ merge_blocks <- function(df) { - if(nrow(df) == 1) return(df) + if (nrow(df) == 1) return(df) df$delete <- FALSE - for(i in 2:nrow(df)) { - if(df$values[i] == df$values[i-1]) { - df$start[i] <- df$start[i-1] - df$length[i] <- df$length[i] + df$length[i-1] - df$delete[i-1] <- TRUE + for (i in 2:nrow(df)) { + if (df$values[i] == df$values[i - 1]) { + df$start[i] <- df$start[i - 1] + df$length[i] <- df$length[i] + df$length[i - 1] + df$delete[i - 1] <- TRUE } } - df <- df[!df$delete,] + df <- df[!df$delete, ] df$delete <- NULL - df <- df[df$length > 1,] + df <- df[df$length > 1, ] df } select_closest <- function(rm, prev_new_rm) { chk_whole_number(prev_new_rm) rm$..fwa_keep[rm$new_rm < prev_new_rm] <- FALSE - if(!any(rm$..fwa_keep)) return(rm) + if (!any(rm$..fwa_keep)) return(rm) rm$..fwa_distance_to_rm <- sf::st_distance(rm, rm$..fwa_geometry, by_element = TRUE) rm$..fwa_distance_to_rm <- as.numeric(rm$..fwa_distance_to_rm) @@ -38,20 +38,20 @@ resolve_multijoins <- function(rm) { df$end <- cumsum(df$length) df$start <- df$end - df$length + 1 - df <- df[!is.na(df$values),] - if(!nrow(df)) return(rm) + df <- df[!is.na(df$values), ] + if (!nrow(df)) return(rm) df <- merge_blocks(df) - if(!nrow(df)) return(rm) + if (!nrow(df)) return(rm) prev_new_rm <- 0 rm$..fwa_keep <- TRUE - for(i in 1:nrow(df)) { + for (i in seq_len(nrow(df))) { indices <- df$start[i]:df$end[i] - rm[indices,] <- select_closest(rm[indices,], prev_new_rm) + rm[indices, ] <- select_closest(rm[indices, ], prev_new_rm) prev_new_rm <- rm$new_rm[indices][rm$..fwa_keep[indices]] } - rm <- rm[rm$..fwa_keep,] + rm <- rm[rm$..fwa_keep, ] rm$..fwa_keep <- NULL rm } @@ -76,8 +76,8 @@ resolve_multijoins <- function(rm) { #' @export #' @examples #' rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) -#' x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] -#' rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] +#' x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] +#' rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] #' fwa_snap_rms_to_rms(x, rm) fwa_snap_rms_to_rms <- function(x, rm, snap_mouths = FALSE) { chk::chk_s3_class(rm, "sf") diff --git a/R/swap-branches-rms.R b/R/swap-branches-rms.R index 5ecf573..f7ad4a8 100644 --- a/R/swap-branches-rms.R +++ b/R/swap-branches-rms.R @@ -5,17 +5,17 @@ is_rooted_blk <- function(x, blk) { parent_blk <- function(x, blk, defined = FALSE, trib = NA) { chk_whole_number(blk) # +chk - trib <- if(is.na(trib)) rep(TRUE, nrow(x)) else x$..fwa_trib == trib + trib <- if (is.na(trib)) rep(TRUE, nrow(x)) else x$..fwa_trib == trib x <- unique(x$parent_blk[x$blk == blk & trib]) - if(!length(x)) { + if (!length(x)) { abort_chk("`blk` ", blk, " is missing from `x`") } - if(length(x) > 1) { + if (length(x) > 1) { abort_chk("`parent_blk` is inconsistent for blk ", blk, " in `x`") } - if(defined & is.na(x)) { + if (defined && is.na(x)) { abort_chk("`parent_blk` is undefined for blk ", blk, " in `x`") } x @@ -24,17 +24,17 @@ parent_blk <- function(x, blk, defined = FALSE, trib = NA) { parent_rm <- function(x, blk, defined = FALSE, trib = NA) { chk_whole_number(blk) # +chk - trib <- if(is.na(trib)) rep(TRUE, nrow(x)) else x$..fwa_trib == trib + trib <- if (is.na(trib)) rep(TRUE, nrow(x)) else x$..fwa_trib == trib x <- unique(x$parent_rm[x$blk == blk & trib]) - if(!length(x)) { + if (!length(x)) { abort_chk("`blk` ", blk, " is missing from `x`") } - if(length(x) > 1) { + if (length(x) > 1) { abort_chk("`parent_rm` is inconsistent for blk ", blk, " in `x`") } - if(defined & is.na(x)) { + if (defined && is.na(x)) { abort_chk("`parent_rm` is undefined for blk ", blk, " in `x`") } x @@ -75,7 +75,7 @@ swap_main <- function(x, blk) { parent_blk <- parent_blk(x, blk, TRUE) parent_rm <- parent_rm(x, blk, TRUE) - root <- x[x$blk == blk & x$rm == 0,] + root <- x[x$blk == blk & x$rm == 0, ] check_dim(root, nrow, values = 1L) # +chk root$blk <- parent_blk root$rm <- parent_rm @@ -95,17 +95,17 @@ swap_main <- function(x, blk) { } adjust_points_blk <- function(x, blk) { - y <- x[x$blk == blk,] - if(nrow(y) < 2) return(x) + y <- x[x$blk == blk, ] + if (nrow(y) < 2) return(x) range <- range(y$rm) interval <- max(diff(sort(y$rm))) seq <- seq(0, range[2], by = interval) seq <- seq[seq >= range[1] & seq <= range[2]] - if(!length(seq)) return(x) + if (!length(seq)) return(x) - x <- x[x$blk != blk,] + x <- x[x$blk != blk, ] y <- y |> dplyr::arrange(rm) @@ -115,7 +115,7 @@ adjust_points_blk <- function(x, blk) { mutate(rm = y$rm) new_y <- y |> - dplyr::slice(1L:length(seq)) |> + dplyr::slice(seq_along(seq)) |> dplyr::mutate(rm = seq) X <- approx(coordinates$rm, coordinates$X, new_y$rm)$y @@ -134,7 +134,7 @@ adjust_points_blk <- function(x, blk) { } adjust_points <- function(x, blk, adjust_points) { - if(!adjust_points) return(x) + if (!adjust_points) return(x) parent_blk <- parent_blk(x, blk, TRUE) x |> adjust_points_blk(blk) |> @@ -144,7 +144,7 @@ adjust_points <- function(x, blk, adjust_points) { swap_branches <- function(x, blk, adjust_points) { chk_whole_number(blk) # +chk - if(!is_rooted_blk(x, blk)) { + if (!is_rooted_blk(x, blk)) { abort_chk("`blk` ", blk, " from `x` is unrooted (missing rm == 0)") } @@ -174,11 +174,11 @@ fwa_swap_branches_rms <- function(x, y, adjust_points = TRUE) { chk_data(x) chk_data(y) chk_flag(adjust_points) - if(!(vld_false(adjust_points) | vld_s3_class(x, "sf"))) { + if (!(vld_false(adjust_points) || vld_s3_class(x, "sf"))) { chkor_vld(vld_false(adjust_points), vld_s3_class(x, "sf")) } - if(vld_s3_class(x, "sf")) { + if (vld_s3_class(x, "sf")) { chk_s3_class(sf::st_geometry(x), "sfc_POINT") } @@ -207,15 +207,16 @@ fwa_swap_branches_rms <- function(x, y, adjust_points = TRUE) { check_key(x, c("blk", "rm")) - if(!nrow(y)) return(x) + if (!nrow(y)) return(x) missing <- setdiff(y$blk, x$blk) - if(length(missing)) { + if (length(missing)) { abort_chk("The following %n blk%s %r missing from `x`:", cc(missing, conj = "and"), - n = length(missing)) + n = length(missing) + ) } - for(blk in y$blk) { + for (blk in y$blk) { x <- swap_branches(x, blk, adjust_points) } x |> diff --git a/R/utils.R b/R/utils.R index c647089..b3b2ef1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,11 +1,11 @@ group_split_sf <- function(.tbl, ..., .keep = TRUE) { is_sf <- inherits(.tbl, "sf") - if(is_sf) { + if (is_sf) { sf_column_name <- attr(.tbl, "sf_column") } x <- dplyr::group_split(.tbl, ..., .keep = .keep) - if(is_sf) { + if (is_sf) { x <- x |> lapply(sf::st_sf, sf_column_name = sf_column_name) } @@ -14,7 +14,7 @@ group_split_sf <- function(.tbl, ..., .keep = TRUE) { # https://stackoverflow.com/questions/43627679/round-any-equivalent-for-dplyr round_any <- function(x, accuracy, f = round) { - f(x/ accuracy) * accuracy + f(x / accuracy) * accuracy } # https://stackoverflow.com/questions/12688717/round-up-from-5 @@ -46,7 +46,7 @@ has_name <- function(x, name) { name %in% names(x) } -is.sf <- function (x) { +is.sf <- function(x) { inherits(x, "sf") } @@ -59,7 +59,7 @@ sample_linestring <- function(x, interval, end) { sample <- seq(0, length, by = interval) end <- length - sample[length(sample)] >= end - if(end) { + if (end) { sample <- c(sample, length) } sample <- sample / length @@ -71,9 +71,11 @@ sample_linestring <- function(x, interval, end) { x <- x |> dplyr::slice(rep(1, length(points))) |> sf::st_set_geometry(points) |> - dplyr::mutate(rm = (dplyr::row_number() - 1) * interval, - rm = as.integer(.data$rm)) - if(end) { + dplyr::mutate( + rm = (dplyr::row_number() - 1) * interval, + rm = as.integer(.data$rm) + ) + if (end) { x$rm[nrow(x)] <- as.integer(length) } x @@ -81,7 +83,7 @@ sample_linestring <- function(x, interval, end) { sample_linestrings <- function(x, interval, end) { x <- x |> - dplyr::mutate(..fwa_id = 1:dplyr::n()) |> + dplyr::mutate(..fwa_id = seq_len(dplyr::n())) |> dplyr::group_split(.data$..fwa_id) |> purrr::map(sample_linestring, interval, end = end) |> dplyr::bind_rows() |> @@ -100,10 +102,10 @@ reverse_linestrings <- function(x) { } reverse_linestring <- function(x) { - sf::st_linestring(x[rev(seq_len(nrow(x))),]) + sf::st_linestring(x[rev(seq_len(nrow(x))), ]) } -sf_column_name <- function (x) { +sf_column_name <- function(x) { if (!is.sf(x) || is.null(attr(x, "sf_column"))) { return(character(0)) } diff --git a/README.Rmd b/README.Rmd index 561ff71..51be57e 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,7 +18,7 @@ knitr::opts_chunk$set( [![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) [![R-CMD-check](https://github.com/poissonconsulting/fwatlasbc/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/poissonconsulting/fwatlasbc/actions/workflows/R-CMD-check.yaml) -[![codecov](https://codecov.io/gh/poissonconsulting/fwatlasbc/branch/main/graph/badge.svg?token=x3TrvhuMbK)](https://codecov.io/gh/poissonconsulting/fwatlasbc) +[![Codecov test coverage](https://codecov.io/gh/poissonconsulting/fwatlasbc/graph/badge.svg)](https://app.codecov.io/gh/poissonconsulting/fwatlasbc) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) @@ -51,7 +51,7 @@ There may be multiple streams with the same name. ```{r} blks <- fwa_add_blks_to_stream_name(streams) blks -blks <- blks[blks$blk == 356534225,] +blks <- blks[blks$blk == 356534225, ] ``` Alternatively get the nearest blue line key and river meter (rm) from the longitude and latitude. @@ -78,8 +78,8 @@ network Convert the stream network into a table of blue line keys with regularly spaced river meters starting at 0. ```{r} -#rms <- fwa_convert_stream_network_to_rms(network, interval = 100) -#rms +# rms <- fwa_convert_stream_network_to_rms(network, interval = 100) +# rms ``` Plot the watershed, network and river meters. @@ -87,7 +87,7 @@ Plot the watershed, network and river meters. ggplot2::ggplot() + ggplot2::geom_sf(data = wshed) + ggplot2::geom_sf(data = network, color = "blue") + -# ggplot2::geom_sf(data = rms) + + # ggplot2::geom_sf(data = rms) + NULL ``` diff --git a/README.md b/README.md index fdf245a..09dda94 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ [![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) [![R-CMD-check](https://github.com/poissonconsulting/fwatlasbc/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/poissonconsulting/fwatlasbc/actions/workflows/R-CMD-check.yaml) -[![codecov](https://codecov.io/gh/poissonconsulting/fwatlasbc/branch/main/graph/badge.svg?token=x3TrvhuMbK)](https://codecov.io/gh/poissonconsulting/fwatlasbc) +[![Codecov test +coverage](https://codecov.io/gh/poissonconsulting/fwatlasbc/graph/badge.svg)](https://app.codecov.io/gh/poissonconsulting/fwatlasbc) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) @@ -61,7 +62,7 @@ blks #> 2 Steep Creek 356362258 #> 3 Steep Creek 356534225 #> 4 Steep Creek 356570155 -blks <- blks[blks$blk == 356534225,] +blks <- blks[blks$blk == 356534225, ] ``` Alternatively get the nearest blue line key and river meter (rm) from @@ -78,7 +79,7 @@ fwa_add_blk_to_lon_lat(data.frame(lon = -132.26, lat = 53.36)) #> Projected CRS: NAD83 / BC Albers #> # A tibble: 1 × 6 #> lon lat blk rm distance_to_lon_lat geometry -#> +#> #> 1 -132. 53.4 360824839 1118. 508. (585153.6 946162.9) ``` @@ -92,12 +93,12 @@ wshed #> Simple feature collection with 1 feature and 3 fields #> Geometry type: POLYGON #> Dimension: XY -#> Bounding box: xmin: 1656218 ymin: 725423.1 xmax: 1661726 ymax: 732146.2 +#> Bounding box: xmin: 1656219 ymin: 725423.4 xmax: 1661727 ymax: 732146.6 #> Projected CRS: NAD83 / BC Albers #> # A tibble: 1 × 4 #> stream_name blk rm geometry #> -#> 1 Steep Creek 356534225 0 ((1658107 728964.9, 1658107 728964.9, 1658179 728… +#> 1 Steep Creek 356534225 0 ((1658107 728965.2, 1658107 728965.2, 1658180 728… ``` Get stream network for the watershed polygon. Note that rm is from the @@ -107,39 +108,39 @@ equivalents for the stream segment in the network. ``` r network <- fwa_add_collection_to_polygon(wshed, "stream_network") network -#> Simple feature collection with 76 features and 31 fields +#> Simple feature collection with 76 features and 29 fields #> Geometry type: LINESTRING #> Dimension: XY -#> Bounding box: xmin: 1656200 ymin: 725423.1 xmax: 1661368 ymax: 731602.7 +#> Bounding box: xmin: 1656200 ymin: 725423.4 xmax: 1661368 ymax: 731603 #> Projected CRS: NAD83 / BC Albers -#> # A tibble: 76 × 32 -#> stream…¹ blk rm blue_…² blue_…³ downs…⁴ edge_…⁵ featu…⁶ featu…⁷ fwa_w…⁸ -#> -#> 1 Steep C… 3.57e8 0 3.57e8 1130 2931. 1250 WA2411… areal … 300-90… -#> 2 Steep C… 3.56e8 0 3.56e8 NA 1110. 1000 GA2485… linear… 300-90… -#> 3 Steep C… 3.57e8 0 3.57e8 NA 0 1000 GA2485… linear… 300-90… -#> 4 Steep C… 3.57e8 0 3.57e8 1142 2594. 1000 GA2485… linear… 300-90… -#> 5 Steep C… 3.57e8 0 3.57e8 1142 2251. 1000 GA2485… linear… 300-90… -#> 6 Steep C… 3.56e8 0 3.56e8 NA 0 1000 GA2485… linear… 300-90… -#> 7 Steep C… 3.56e8 0 3.56e8 NA 0 1000 GA2485… linear… 300-90… -#> 8 Steep C… 3.57e8 0 3.57e8 1131 51.8 1000 GA2485… linear… 300-90… -#> 9 Steep C… 3.57e8 0 3.57e8 1130 751. 1250 WA2411… areal … 300-90… -#> 10 Steep C… 3.56e8 0 3.56e8 NA 346. 1000 GA2485… linear… 300-90… -#> # … with 66 more rows, 22 more variables: gnis_id , gnis_name , -#> # gradient , left_right_tributary , length_metre , -#> # linear_feature_id , local_watershed_code , localcode_ltree , -#> # stream_magnitude , stream_order , stream_order_max , -#> # stream_order_parent , upstream_route_measure , -#> # waterbody_key , watershed_code_50k , watershed_group_code , -#> # watershed_group_code_50k , watershed_group_id , … +#> # A tibble: 76 × 30 +#> stream_name blk rm blue_line_key blue_line_key_50k +#> +#> 1 Steep Creek 356436505 0 356436505 NA +#> 2 Steep Creek 356526193 0 356526193 873 +#> 3 Steep Creek 356566041 0 356566041 1130 +#> 4 Steep Creek 356549188 0 356549188 1131 +#> 5 Steep Creek 356566041 0 356566041 1130 +#> 6 Steep Creek 356394005 0 356394005 NA +#> 7 Steep Creek 356534225 0 356534225 1142 +#> 8 Steep Creek 356549188 0 356549188 1131 +#> 9 Steep Creek 356526193 0 356526193 873 +#> 10 Steep Creek 356566041 0 356566041 1130 +#> # ℹ 66 more rows +#> # ℹ 25 more variables: downstream_route_measure , edge_type , +#> # feature_code , feature_source , fwa_watershed_code , +#> # gnis_id , gnis_name , gradient , left_right_tributary , +#> # length_metre , linear_feature_id , local_watershed_code , +#> # localcode_ltree , stream_magnitude , stream_order , +#> # upstream_route_measure , waterbody_key , … ``` Convert the stream network into a table of blue line keys with regularly spaced river meters starting at 0. ``` r -#rms <- fwa_convert_stream_network_to_rms(network, interval = 100) -#rms +# rms <- fwa_convert_stream_network_to_rms(network, interval = 100) +# rms ``` Plot the watershed, network and river meters. @@ -148,7 +149,7 @@ Plot the watershed, network and river meters. ggplot2::ggplot() + ggplot2::geom_sf(data = wshed) + ggplot2::geom_sf(data = network, color = "blue") + -# ggplot2::geom_sf(data = rms) + + # ggplot2::geom_sf(data = rms) + NULL ``` diff --git a/_pkgdown.yml b/_pkgdown.yml new file mode 100644 index 0000000..7823543 --- /dev/null +++ b/_pkgdown.yml @@ -0,0 +1,4 @@ +url: https://poissonconsulting.github.io/fwatlasbc/ +template: + bootstrap: 5 + diff --git a/data-raw/data-raw.R b/data-raw/data-raw.R index a00c846..e6c2e5a 100644 --- a/data-raw/data-raw.R +++ b/data-raw/data-raw.R @@ -31,7 +31,8 @@ fwa_stream_name <- fwa_stream_name |> mutate(blk = as.integer(blk)) chk::check_data(fwa_stream_name, - values = list(blk = 1L, stream_name = ""), - exclusive = TRUE, order = TRUE, key = "blk") + values = list(blk = 1L, stream_name = ""), + exclusive = TRUE, order = TRUE, key = "blk" +) usethis::use_data(fwa_stream_name, overwrite = TRUE) diff --git a/man/figures/README-unnamed-chunk-8-1.png b/man/figures/README-unnamed-chunk-8-1.png index f87268c..6a4aece 100644 Binary files a/man/figures/README-unnamed-chunk-8-1.png and b/man/figures/README-unnamed-chunk-8-1.png differ diff --git a/man/fwa_add_collection_to_polygon.Rd b/man/fwa_add_collection_to_polygon.Rd index 7825b3f..198899f 100644 --- a/man/fwa_add_collection_to_polygon.Rd +++ b/man/fwa_add_collection_to_polygon.Rd @@ -81,5 +81,5 @@ fwa_add_collection_to_polygon(watershed) } } \seealso{ -\code{\link[fwapgr]{fwa_collection}}. +\code{\link[fwapgr:fwa_query_collection]{fwapgr::fwa_collection()}}. } diff --git a/man/fwa_add_watershed_to_blk.Rd b/man/fwa_add_watershed_to_blk.Rd index efe9a3a..6d00c4b 100644 --- a/man/fwa_add_watershed_to_blk.Rd +++ b/man/fwa_add_watershed_to_blk.Rd @@ -39,5 +39,5 @@ fwa_add_watershed_to_blk(data.frame(blk = 356308001)) } } \seealso{ -\code{\link[fwapgr]{fwa_watershed_at_measure}}. +\code{\link[fwapgr:fwa_watershed_at_measure]{fwapgr::fwa_watershed_at_measure()}}. } diff --git a/man/fwa_snap_rm_to_point.Rd b/man/fwa_snap_rm_to_point.Rd index cfef535..3fb0864 100644 --- a/man/fwa_snap_rm_to_point.Rd +++ b/man/fwa_snap_rm_to_point.Rd @@ -24,7 +24,7 @@ together with the distance to the river meter (distance_to_rm) in m. } \examples{ rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) -x <- rm[rm$rm \%in\% c(0, 2000, 5000, 6000, 7000),] -rm <- rm[rm$rm \%in\% c(1000, 3000, 4000, 8000, 9000, 10000),] +x <- rm[rm$rm \%in\% c(0, 2000, 5000, 6000, 7000), ] +rm <- rm[rm$rm \%in\% c(1000, 3000, 4000, 8000, 9000, 10000), ] fwa_snap_rm_to_point(x, rm) } diff --git a/man/fwa_snap_rm_to_rms.Rd b/man/fwa_snap_rm_to_rms.Rd index 55bd2f5..c21345c 100644 --- a/man/fwa_snap_rm_to_rms.Rd +++ b/man/fwa_snap_rm_to_rms.Rd @@ -42,7 +42,7 @@ to be 0 where the rm is 0 or set \code{snap_mouths = TRUE} } \examples{ rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) -x <- rm[rm$rm \%in\% c(0, 2000, 5000, 6000, 7000),] -rm <- rm[rm$rm \%in\% c(1000, 3000, 4000, 8000, 9000, 10000),] +x <- rm[rm$rm \%in\% c(0, 2000, 5000, 6000, 7000), ] +rm <- rm[rm$rm \%in\% c(1000, 3000, 4000, 8000, 9000, 10000), ] fwa_snap_rm_to_rms(x, rm) } diff --git a/man/fwa_snap_rms_to_rms.Rd b/man/fwa_snap_rms_to_rms.Rd index a665979..ba5a7ec 100644 --- a/man/fwa_snap_rms_to_rms.Rd +++ b/man/fwa_snap_rms_to_rms.Rd @@ -30,8 +30,8 @@ that the links between x and rm are bidirectional as much as possible. } \examples{ rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) -x <- rm[rm$rm \%in\% c(0, 2000, 5000, 6000, 7000),] -rm <- rm[rm$rm \%in\% c(1000, 3000, 4000, 8000, 9000, 10000),] +x <- rm[rm$rm \%in\% c(0, 2000, 5000, 6000, 7000), ] +rm <- rm[rm$rm \%in\% c(1000, 3000, 4000, 8000, 9000, 10000), ] fwa_snap_rms_to_rms(x, rm) } \seealso{ diff --git a/man/fwatlasbc-package.Rd b/man/fwatlasbc-package.Rd index 8bb91e0..0a7cac4 100644 --- a/man/fwatlasbc-package.Rd +++ b/man/fwatlasbc-package.Rd @@ -9,6 +9,13 @@ \if{html}{\figure{logo.png}{options: style='float: right' alt='logo' width='120'}} What the package does (one paragraph). +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://poissonconsulting.github.io/fwatlasbc/} +} + } \author{ \strong{Maintainer}: Joe Thorley \email{joe@poissonconsulting.ca} (\href{https://orcid.org/0000-0002-7683-4592}{ORCID}) diff --git a/scripts/build.R b/scripts/build.R new file mode 100644 index 0000000..fbf0eab --- /dev/null +++ b/scripts/build.R @@ -0,0 +1,20 @@ +roxygen2md::roxygen2md() + +styler::style_pkg( + scope = "line_breaks", + filetype = c("R", "Rmd") +) + +lintr::lint_package(linters = linters_with_defaults( + line_length_linter = line_length_linter(1000), + object_name_linter = object_name_linter(regexes = ".*")) +) + +lintr::lint_package() + +devtools::test() +devtools::document() + +pkgdown::build_home() + +devtools::check() diff --git a/tests/testthat/test-add-blk-to-lon-lat.R b/tests/testthat/test-add-blk-to-lon-lat.R index 309800b..aaedc40 100644 --- a/tests/testthat/test-add-blk-to-lon-lat.R +++ b/tests/testthat/test-add-blk-to-lon-lat.R @@ -3,8 +3,7 @@ test_that("fwa_add_blk_to_lon_lat works", { x <- fwa_add_blk_to_lon_lat(data.frame(lon = -132.26, lat = 53.36)) expect_s3_class(x, "sf") - expect_identical(colnames(x), c("lon", "lat", "blk", "rm", "distance_to_lon_lat", "geometry" - )) + expect_identical(colnames(x), c("lon", "lat", "blk", "rm", "distance_to_lon_lat", "geometry")) expect_identical(x$lon, -132.26) expect_identical(x$lat, 53.36) expect_identical(x$blk, 360824839L) @@ -18,8 +17,7 @@ test_that("fwa_add_blk_to_lon_lat works multiple ", { x <- fwa_add_blk_to_lon_lat(data.frame(lon = c(-132.26, -132.25), lat = 53.36), limit = 3) expect_s3_class(x, "sf") - expect_identical(colnames(x), c("lon", "lat", "blk", "rm", "distance_to_lon_lat", "geometry" - )) + expect_identical(colnames(x), c("lon", "lat", "blk", "rm", "distance_to_lon_lat", "geometry")) expect_identical(nrow(x), 6L) expect_snapshot_data(x, "multiple") }) @@ -29,8 +27,7 @@ test_that("fwa_add_blk_to_lon_lat works none ", { x <- fwa_add_blk_to_lon_lat(data.frame(lon = -132.26, lat = 53.36), tolerance = 500) expect_s3_class(x, "sf") - expect_identical(colnames(x), c("lon", "lat", "blk", "rm", "distance_to_lon_lat", "geometry" - )) + expect_identical(colnames(x), c("lon", "lat", "blk", "rm", "distance_to_lon_lat", "geometry")) expect_identical(x$lon, numeric(0)) expect_identical(x$lat, numeric(0)) expect_identical(x$blk, integer(0)) diff --git a/tests/testthat/test-add-blks-to-stream-name.R b/tests/testthat/test-add-blks-to-stream-name.R index 91b7b3e..4001033 100644 --- a/tests/testthat/test-add-blks-to-stream-name.R +++ b/tests/testthat/test-add-blks-to-stream-name.R @@ -20,22 +20,26 @@ test_that("fwa_add_blks_to_stream_name works all missing values", { test_that("fwa_add_blks_to_stream_name works multiple matches example ", { blk <- fwa_add_blks_to_stream_name(data.frame(stream_name = "Steep Creek")) - expect_identical(blk, dplyr::tibble(stream_name = c("Steep Creek", "Steep Creek", "Steep Creek" - ), blk = c(356362258L, 356534225L, 356570155L))) + expect_identical(blk, dplyr::tibble(stream_name = c("Steep Creek", "Steep Creek", "Steep Creek"), blk = c(356362258L, 356534225L, 356570155L))) }) test_that("fwa_add_blks_to_stream_name preserves order", { blk <- fwa_add_blks_to_stream_name(data.frame(stream_name = c("Zymoetz River", "Aaltanhash River"))) - expect_identical(blk, dplyr::tibble(stream_name = c("Zymoetz River", "Aaltanhash River" - ), blk = c(360881231L, 360886335L))) + expect_identical(blk, dplyr::tibble(stream_name = c("Zymoetz River", "Aaltanhash River"), blk = c(360881231L, 360886335L))) }) test_that("fwa_add_blks_to_stream_name works mix multiple, missing and order ", { blk <- fwa_add_blks_to_stream_name(data.frame(stream_name = c("Zymoetz River", NA, "Steep Creek", "Sangan2"))) - expect_identical(blk, dplyr::tibble(stream_name = c("Zymoetz River", NA, "Steep Creek", - "Steep Creek", "Steep Creek", "Sangan2"), - blk = c(360881231L, NA, 356362258L, - 356534225L, 356570155L, NA))) + expect_identical(blk, dplyr::tibble( + stream_name = c( + "Zymoetz River", NA, "Steep Creek", + "Steep Creek", "Steep Creek", "Sangan2" + ), + blk = c( + 360881231L, NA, 356362258L, + 356534225L, 356570155L, NA + ) + )) }) test_that("fwa_add_blks_to_stream_name errors existing blk", { @@ -43,8 +47,9 @@ test_that("fwa_add_blks_to_stream_name errors existing blk", { }) test_that("fwa_add_blks_to_stream_name works custom", { - x <- fwa_add_blks_to_stream_name(data.frame(stream_name = "A Creek"), - data.frame(blk = 356235759, stream_name = "A Creek")) + x <- fwa_add_blks_to_stream_name( + data.frame(stream_name = "A Creek"), + data.frame(blk = 356235759, stream_name = "A Creek") + ) expect_identical(x, dplyr::tibble(stream_name = "A Creek", blk = 356235759)) }) - diff --git a/tests/testthat/test-add-collection-to-polygon.R b/tests/testthat/test-add-collection-to-polygon.R index 032eb2f..0ed4472 100644 --- a/tests/testthat/test-add-collection-to-polygon.R +++ b/tests/testthat/test-add-collection-to-polygon.R @@ -12,7 +12,8 @@ test_that("fwa_add_collection_to_polygon functions work", { expect_identical( colnames(x), - c("blk", "rm", "blue_line_key", "blue_line_key_50k", "downstream_route_measure", + c( + "blk", "rm", "blue_line_key", "blue_line_key_50k", "downstream_route_measure", "edge_type", "feature_code", "feature_source", "fwa_watershed_code", "gnis_id", "gnis_name", "gradient", "left_right_tributary", "length_metre", "linear_feature_id", "local_watershed_code", "localcode_ltree", @@ -20,7 +21,9 @@ test_that("fwa_add_collection_to_polygon functions work", { "upstream_route_measure", "waterbody_key", "watershed_code_50k", "watershed_group_code", "watershed_group_code_50k", "watershed_group_id", "watershed_key", - "watershed_key_50k", "wscode_ltree", "geometry")) + "watershed_key_50k", "wscode_ltree", "geometry" + ) + ) }) test_that("fwa_add_collection_to_polygon function intersects work", { @@ -34,7 +37,8 @@ test_that("fwa_add_collection_to_polygon function intersects work", { expect_gte(nrow(x), 189L) expect_identical( colnames(x), - c("blk", "rm", "blue_line_key", "blue_line_key_50k", "downstream_route_measure", + c( + "blk", "rm", "blue_line_key", "blue_line_key_50k", "downstream_route_measure", "edge_type", "feature_code", "feature_source", "fwa_watershed_code", "gnis_id", "gnis_name", "gradient", "left_right_tributary", "length_metre", "linear_feature_id", "local_watershed_code", "localcode_ltree", @@ -42,7 +46,9 @@ test_that("fwa_add_collection_to_polygon function intersects work", { "upstream_route_measure", "waterbody_key", "watershed_code_50k", "watershed_group_code", "watershed_group_code_50k", "watershed_group_id", "watershed_key", - "watershed_key_50k", "wscode_ltree", "geometry")) + "watershed_key_50k", "wscode_ltree", "geometry" + ) + ) expect_s3_class(x$geometry, "sfc_GEOMETRY") }) @@ -53,16 +59,19 @@ test_that("fwa_add_collection_to_polygon function intersects works with named st wshed <- fwa_add_watershed_to_blk(data.frame(blk = 356308001, ExCol = "ex")) wshed <- sf::st_transform(wshed, 4326) x <- fwa_add_collection_to_polygon(wshed, "whse_basemapping.fwa_named_streams", - limit = 1000, - epsg = 32610) + limit = 1000, + epsg = 32610 + ) expect_s3_class(x, "sf") expect_identical(sf::st_crs(x)$epsg, 32610L) expect_identical( colnames(x), - c("blk", "ExCol", "rm", "blue_line_key", + c( + "blk", "ExCol", "rm", "blue_line_key", "gnis_name", "named_streams_id", "stream_order", "watershed_group_code", "geometry" - )) + ) + ) # varies between operating systems expect_gte(nrow(x), 10L) diff --git a/tests/testthat/test-add-collection-to-watershed.R b/tests/testthat/test-add-collection-to-watershed.R index e4add73..96dc089 100644 --- a/tests/testthat/test-add-collection-to-watershed.R +++ b/tests/testthat/test-add-collection-to-watershed.R @@ -14,12 +14,15 @@ test_that("fwa_add_collection_to_watershed functions work", { expect_identical( colnames(x), - c("blk", "rm", "blue_line_key", "blue_line_key_50k", "downstream_route_measure", + c( + "blk", "rm", "blue_line_key", "blue_line_key_50k", "downstream_route_measure", "edge_type", "feature_code", "feature_source", "fwa_watershed_code", "gnis_id", "gnis_name", "gradient", "left_right_tributary", "length_metre", "linear_feature_id", "local_watershed_code", "localcode_ltree", "stream_magnitude", "stream_order", "upstream_route_measure", "waterbody_key", "watershed_code_50k", "watershed_group_code", "watershed_group_code_50k", "watershed_group_id", "watershed_key", - "watershed_key_50k", "wscode_ltree", "geometry")) + "watershed_key_50k", "wscode_ltree", "geometry" + ) + ) }) diff --git a/tests/testthat/test-add-cut-to-rms.R b/tests/testthat/test-add-cut-to-rms.R index 5c5a06a..3455fd1 100644 --- a/tests/testthat/test-add-cut-to-rms.R +++ b/tests/testthat/test-add-cut-to-rms.R @@ -1,8 +1,10 @@ test_that("fwa_add_cut_to_rms simple example", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) - y <- data.frame(blk = 1L, - rm_start = c(3000L, 7500L, 9000L), - rm_end = c(4000L, 8500L, 100000L), cut = c("3", "7", "10")) + y <- data.frame( + blk = 1L, + rm_start = c(3000L, 7500L, 9000L), + rm_end = c(4000L, 8500L, 100000L), cut = c("3", "7", "10") + ) x <- fwa_add_cut_to_rms(x, y) expect_s3_class(x, "tbl") @@ -14,9 +16,11 @@ test_that("fwa_add_cut_to_rms simple example", { test_that("fwa_add_cut_to_rms preserves order", { x <- data.frame(blk = 1L, rm = rev(seq(1000L, 10000L, by = 1000L))) - y <- data.frame(blk = 1L, - rm_start = c(3000L, 7500L, 9000L), - rm_end = c(4000L, 8500L, 100000L), cut = c("3", "7", "10")) + y <- data.frame( + blk = 1L, + rm_start = c(3000L, 7500L, 9000L), + rm_end = c(4000L, 8500L, 100000L), cut = c("3", "7", "10") + ) x <- fwa_add_cut_to_rms(x, y) expect_s3_class(x, "tbl") @@ -28,12 +32,14 @@ test_that("fwa_add_cut_to_rms preserves order", { test_that("fwa_add_cut_to_rms adds missing values if zero length", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) - y <- data.frame(blk = 1L, - rm_start = c(3000L, 7500L, 9000L), - rm_end = c(4000L, 8500L, 100000L), new = c("3", "7", "10")) + y <- data.frame( + blk = 1L, + rm_start = c(3000L, 7500L, 9000L), + rm_end = c(4000L, 8500L, 100000L), new = c("3", "7", "10") + ) y$new <- NA_real_ - x <- fwa_add_cut_to_rms(x, y[0,], cut = "new") + x <- fwa_add_cut_to_rms(x, y[0, ], cut = "new") expect_s3_class(x, "tbl") expect_identical(colnames(x), c("blk", "rm", "new")) expect_identical(x$blk, rep(1L, 10)) @@ -43,11 +49,13 @@ test_that("fwa_add_cut_to_rms adds missing values if zero length", { test_that("fwa_add_cut_to_rms adds zero length", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) - y <- data.frame(blk = 1L, - rm_start = c(3000L, 7500L, 9000L), - rm_end = c(4000L, 8500L, 100000L), new = c("3", "7", "10")) - x <- x[0,] - y <- y[0,] + y <- data.frame( + blk = 1L, + rm_start = c(3000L, 7500L, 9000L), + rm_end = c(4000L, 8500L, 100000L), new = c("3", "7", "10") + ) + x <- x[0, ] + y <- y[0, ] y$cut <- character(0) x <- fwa_add_cut_to_rms(x, y) expect_s3_class(x, "tbl") @@ -59,9 +67,11 @@ test_that("fwa_add_cut_to_rms adds zero length", { test_that("fwa_add_cut_to_rms handles missing section values", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) - y <- data.frame(blk = 1L, - rm_start = c(3000L, 7500L, 9000L), - rm_end = c(4000L, 8500L, 100000L), cut = c(NA, "7", "10")) + y <- data.frame( + blk = 1L, + rm_start = c(3000L, 7500L, 9000L), + rm_end = c(4000L, 8500L, 100000L), cut = c(NA, "7", "10") + ) x <- fwa_add_cut_to_rms(x, y) expect_s3_class(x, "tbl") @@ -73,10 +83,12 @@ test_that("fwa_add_cut_to_rms handles missing section values", { test_that("fwa_add_cut_to_rms doesn't grab extra column", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) - y <- data.frame(blk = 1L, - rm_start = c(3000L, 7500L, 9000L), - rm_end = c(4000L, 8500L, 100000L), cut = c("3", "7", "10"), - extra = 1:3) + y <- data.frame( + blk = 1L, + rm_start = c(3000L, 7500L, 9000L), + rm_end = c(4000L, 8500L, 100000L), cut = c("3", "7", "10"), + extra = 1:3 + ) x <- fwa_add_cut_to_rms(x, y) expect_s3_class(x, "tbl") @@ -88,26 +100,31 @@ test_that("fwa_add_cut_to_rms doesn't grab extra column", { test_that("fwa_add_cut_to_rms sf", { x <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pb", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pb", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") - y <- data.frame(blk = 1L, - rm_start = c(1L, 3L), - rm_end = c(2L, 10L), - cut = c(TRUE, FALSE)) + y <- data.frame( + blk = 1L, + rm_start = c(1L, 3L), + rm_end = c(2L, 10L), + cut = c(TRUE, FALSE) + ) - x <- fwa_add_cut_to_rms(x,y) + x <- fwa_add_cut_to_rms(x, y) expect_s3_class(x, "sf") - expect_identical(colnames(x), c("blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", - "cut")) + expect_identical(colnames(x), c( + "blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", + "cut" + )) expect_identical(nrow(x), 7L) expect_identical(x$cut, c(NA, TRUE, TRUE, FALSE, NA, NA, NA)) @@ -116,28 +133,33 @@ test_that("fwa_add_cut_to_rms sf", { test_that("fwa_add_cut_to_rms sf preserve geometry2", { x <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pb", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pb", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") |> dplyr::mutate(geometry2 = geometry) |> sf::st_set_geometry("geometry2") |> sf::st_set_crs(4326) - y <- data.frame(blk = 1L, - rm_start = c(1L, 3L), - rm_end = c(2L, 10L), - cut = c(TRUE, FALSE)) + y <- data.frame( + blk = 1L, + rm_start = c(1L, 3L), + rm_end = c(2L, 10L), + cut = c(TRUE, FALSE) + ) - x <- fwa_add_cut_to_rms(x,y) + x <- fwa_add_cut_to_rms(x, y) expect_s3_class(x, "sf") - expect_identical(colnames(x), c("blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", "geometry2", - "cut")) + expect_identical(colnames(x), c( + "blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", "geometry2", + "cut" + )) expect_identical(nrow(x), 7L) expect_identical(x$cut, c(NA, TRUE, TRUE, FALSE, NA, NA, NA)) expect_identical(sf::st_geometry(x), x$geometry2) diff --git a/tests/testthat/test-add-downstream-split-to-rms.R b/tests/testthat/test-add-downstream-split-to-rms.R index 4140ebc..8dd287e 100644 --- a/tests/testthat/test-add-downstream-split-to-rms.R +++ b/tests/testthat/test-add-downstream-split-to-rms.R @@ -1,6 +1,8 @@ test_that("fwa_add_downstream_split_to_rms simple example", { - x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L), - parent_blk = NA_integer_, parent_rm = NA_integer_) + x <- data.frame( + blk = 1L, rm = seq(1000L, 10000L, by = 1000L), + parent_blk = NA_integer_, parent_rm = NA_integer_ + ) y <- data.frame(blk = 1L, rm = 3000L, name = "new") x <- fwa_add_downstream_split_to_rms(x, y) @@ -14,14 +16,15 @@ test_that("fwa_add_downstream_split_to_rms simple example", { test_that("fwa_add_downstream_split_to_rms simple example all", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_add_downstream_split_to_rms(x, data.frame(blk = 1, rm = 0, name = "new2")) @@ -34,14 +37,15 @@ test_that("fwa_add_downstream_split_to_rms simple example all", { test_that("fwa_add_downstream_split_to_rms simple example partway up main", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_add_downstream_split_to_rms(x, data.frame(blk = 1, rm = 1, name = "new2")) @@ -54,14 +58,15 @@ test_that("fwa_add_downstream_split_to_rms simple example partway up main", { test_that("fwa_add_downstream_split_to_rms on trib", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_add_downstream_split_to_rms(x, data.frame(blk = 2, rm = 1, name = "new2")) @@ -74,16 +79,17 @@ test_that("fwa_add_downstream_split_to_rms on trib", { test_that("fwa_add_downstream_split_to_rms two splits", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) - y <- data.frame(blk = c(1,2), rm = c(1,1), name = c("new2", "new1")) + y <- data.frame(blk = c(1, 2), rm = c(1, 1), name = c("new2", "new1")) x <- fwa_add_downstream_split_to_rms(x, y) expect_s3_class(x, "tbl") diff --git a/tests/testthat/test-add-gm-elevation-to-point.R b/tests/testthat/test-add-gm-elevation-to-point.R index 90b218f..7dd97d3 100644 --- a/tests/testthat/test-add-gm-elevation-to-point.R +++ b/tests/testthat/test-add-gm-elevation-to-point.R @@ -19,12 +19,16 @@ test_that("fwa_add_gm_elevation_to_point works digits", { x <- fwa_add_gm_elevation_to_point(rm, digits = 3) expect_s3_class(x, "sf") expect_identical(colnames(x), c("blk", "rm", "elevation", "geometry")) - expect_equal(x$elevation, - c(5, 5, 5, 11.4579448699951, 5, 19.2013263702393, 10.6362476348877, - 6.682457447052, 6.72773838043213, 10.558274269104, 10.594612121582, - 8.23660659790039, 11.069766998291, 12.1544971466065, 11.0921401977539, - 11.8463001251221, 10.2729940414429, 80.8487396240234, 171.800216674805, - 210.517883300781)) + expect_equal( + x$elevation, + c( + 5, 5, 5, 11.4579448699951, 5, 19.2013263702393, 10.6362476348877, + 6.682457447052, 6.72773838043213, 10.558274269104, 10.594612121582, + 8.23660659790039, 11.069766998291, 12.1544971466065, 11.0921401977539, + 11.8463001251221, 10.2729940414429, 80.8487396240234, 171.800216674805, + 210.517883300781 + ) + ) }) test_that("fwa_add_gm_elevation_to_point works almost 1000 (slightly different linux!)", { diff --git a/tests/testthat/test-add-intersection-to-geometry.R b/tests/testthat/test-add-intersection-to-geometry.R index fdb11f7..7da8644 100644 --- a/tests/testthat/test-add-intersection-to-geometry.R +++ b/tests/testthat/test-add-intersection-to-geometry.R @@ -1,6 +1,6 @@ test_that("fwa_add_intersection_to_point works simple case", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - y <- sf::st_cast(rm[rm$rm == 15000,]) + y <- sf::st_cast(rm[rm$rm == 15000, ]) y$name <- "ne" x <- fwa_add_intersection_to_geometry(rm, y) diff --git a/tests/testthat/test-add-new-blk-rm-to-blk-rm.R b/tests/testthat/test-add-new-blk-rm-to-blk-rm.R index 75bdf13..a746c00 100644 --- a/tests/testthat/test-add-new-blk-rm-to-blk-rm.R +++ b/tests/testthat/test-add-new-blk-rm-to-blk-rm.R @@ -3,8 +3,8 @@ test_that("fwa_add_new_blk_to_blk_rm works", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x @@ -31,8 +31,8 @@ test_that("fwa_add_new_blk_to_blk_rm works tibble and missing values", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x @@ -59,16 +59,18 @@ test_that("fwa_add_new_blk_to_blk_rm works different names", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x rm <- x |> dplyr::slice(-2) |> - dplyr::rename(rm2 = rm, blk2 = blk, - old_rm = new_rm, old_blk = new_blk) + dplyr::rename( + rm2 = rm, blk2 = blk, + old_rm = new_rm, old_blk = new_blk + ) x <- x |> dplyr::as_tibble() |> @@ -76,9 +78,11 @@ test_that("fwa_add_new_blk_to_blk_rm works different names", { dplyr::rename(rm1 = rm, blk1 = blk) - x <- fwa_add_new_blk_rm_to_blk_rm(x, rm, rm = "rm1", blk = "blk1", - rm2 = "rm2", blk2 = "blk2", - new_rm = "old_rm", new_blk = "old_blk") + x <- fwa_add_new_blk_rm_to_blk_rm(x, rm, + rm = "rm1", blk = "blk1", + rm2 = "rm2", blk2 = "blk2", + new_rm = "old_rm", new_blk = "old_blk" + ) expect_s3_class(x, "tbl_df") expect_identical(colnames(x), c("blk1", "rm1", "old_blk", "old_rm", "elevation")) @@ -94,8 +98,8 @@ test_that("fwa_add_new_blk_to_blk_rm works same blk2", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x @@ -122,8 +126,8 @@ test_that("fwa_add_new_blk_to_blk_rm works new_blk = NULL", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x @@ -150,8 +154,8 @@ test_that("fwa_add_new_blk_to_blk_rm works rename", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x @@ -165,8 +169,10 @@ test_that("fwa_add_new_blk_to_blk_rm works rename", { dplyr::select(blk, rm, elevation) - x <- fwa_add_new_blk_rm_to_blk_rm(x, rm, new_blk_to = "zz_blk", - new_rm_to = "aa") + x <- fwa_add_new_blk_rm_to_blk_rm(x, rm, + new_blk_to = "zz_blk", + new_rm_to = "aa" + ) expect_s3_class(x, "tbl_df") expect_identical(colnames(x), c("blk", "rm", "zz_blk", "aa", "elevation")) diff --git a/tests/testthat/test-add-rms-to-blk.R b/tests/testthat/test-add-rms-to-blk.R index a7ccf32..7d5784b 100644 --- a/tests/testthat/test-add-rms-to-blk.R +++ b/tests/testthat/test-add-rms-to-blk.R @@ -2,7 +2,8 @@ test_that("fwa_add_rms_to_blk works simple case", { rlang::local_options(nocache = TRUE) x <- fwa_add_rms_to_blk(data.frame(blk = 356308001, extra_col = "extra"), - interval = 100, start = 10001) + interval = 100, start = 10001 + ) expect_s3_class(x, "sf") expect_identical(names(x), c("blk", "extra_col", "rm", "elevation", "geometry")) expect_identical(x$blk, rep(356308001, 93)) diff --git a/tests/testthat/test-add-section-to-rms.R b/tests/testthat/test-add-section-to-rms.R index e40337e..11345ad 100644 --- a/tests/testthat/test-add-section-to-rms.R +++ b/tests/testthat/test-add-section-to-rms.R @@ -26,7 +26,7 @@ test_that("fwa_add_section_to_rms adds missing values if zero length", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) y <- data.frame(blk = 1L, rm = c(3000L, 7500L, 9000L), new = 2000L) y$new <- NA_real_ - x <- fwa_add_section_to_rms(x, y[0,], section = "new") + x <- fwa_add_section_to_rms(x, y[0, ], section = "new") expect_s3_class(x, "tbl") expect_identical(colnames(x), c("blk", "rm", "new")) expect_identical(x$blk, rep(1L, 10)) @@ -38,8 +38,8 @@ test_that("fwa_add_section_to_rms adds zero length", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) y <- data.frame(blk = 1L, rm = c(3000L, 7500L, 9000L), new = 2000L) - x <- x[0,] - y <- y[0,] + x <- x[0, ] + y <- y[0, ] y$section <- character(0) x <- fwa_add_section_to_rms(x, y) expect_s3_class(x, "tbl") @@ -63,8 +63,10 @@ test_that("fwa_add_section_to_rms handles missing section values", { test_that("fwa_add_section_to_rms doesn't grab extra column", { x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L)) - y <- data.frame(blk = 1L, rm = c(3000L, 7500L, 9000L), section = c(3L, 7L, 10L), - extra = c("x", "y", "x")) + y <- data.frame( + blk = 1L, rm = c(3000L, 7500L, 9000L), section = c(3L, 7L, 10L), + extra = c("x", "y", "x") + ) x <- fwa_add_section_to_rms(x, y) expect_s3_class(x, "tbl") @@ -76,22 +78,25 @@ test_that("fwa_add_section_to_rms doesn't grab extra column", { test_that("fwa_add_section_to_rms sf", { x <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pb", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pb", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") y <- data.frame(blk = 1L, rm = c(1, 2, 4), section = c(3L, 7L, 10L)) - x <- fwa_add_section_to_rms(x,y) + x <- fwa_add_section_to_rms(x, y) expect_s3_class(x, "sf") - expect_identical(colnames(x), c("blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", - "section")) + expect_identical(colnames(x), c( + "blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", + "section" + )) expect_identical(nrow(x), 7L) expect_identical(x$section, c(3L, 3L, 7L, 10L, NA, NA, NA)) @@ -100,14 +105,15 @@ test_that("fwa_add_section_to_rms sf", { test_that("fwa_add_section_to_rms sf preserve geometry2", { x <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pb", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pb", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") |> dplyr::mutate(geometry2 = geometry) |> sf::st_set_geometry("geometry2") |> @@ -115,10 +121,12 @@ test_that("fwa_add_section_to_rms sf preserve geometry2", { y <- data.frame(blk = 1L, rm = c(1, 2, 4), section = c(3L, 7L, 10L)) - x <- fwa_add_section_to_rms(x,y) + x <- fwa_add_section_to_rms(x, y) expect_s3_class(x, "sf") - expect_identical(colnames(x), c("blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", "geometry2", - "section")) + expect_identical(colnames(x), c( + "blk", "rm", "parent_blk", "parent_rm", "popn", "geometry", "geometry2", + "section" + )) expect_identical(nrow(x), 7L) expect_identical(x$section, c(3L, 3L, 7L, 10L, NA, NA, NA)) expect_identical(sf::st_geometry(x), x$geometry2) diff --git a/tests/testthat/test-add-stream-names-to-blk.R b/tests/testthat/test-add-stream-names-to-blk.R index 4cc7d78..f7e52f8 100644 --- a/tests/testthat/test-add-stream-names-to-blk.R +++ b/tests/testthat/test-add-stream-names-to-blk.R @@ -24,30 +24,44 @@ test_that("fwa_add_stream_names_to_blk works missing values", { }) test_that("fwa_add_stream_names_to_blk works custom", { - x <- fwa_add_stream_names_to_blk(data.frame(blk = 356235759), - data.frame(blk = 356235759, stream_name = "A Creek")) - expect_identical(x, dplyr::tibble(blk = 356235759, - stream_name = "A Creek")) + x <- fwa_add_stream_names_to_blk( + data.frame(blk = 356235759), + data.frame(blk = 356235759, stream_name = "A Creek") + ) + expect_identical(x, dplyr::tibble( + blk = 356235759, + stream_name = "A Creek" + )) }) test_that("fwa_add_stream_names_to_blk removes duplicates custom", { - stream_name <- data.frame(blk = c(356235759, 356235759, 356235759), - stream_name = c("A Creek", "A Creek", "A Different Creek")) + stream_name <- data.frame( + blk = c(356235759, 356235759, 356235759), + stream_name = c("A Creek", "A Creek", "A Different Creek") + ) x <- fwa_add_stream_names_to_blk(data.frame(blk = 356235759), - stream_name = stream_name) + stream_name = stream_name + ) - expect_identical(x, dplyr::tibble(blk = c(356235759, 356235759), - stream_name = c("A Creek", "A Different Creek"))) + expect_identical(x, dplyr::tibble( + blk = c(356235759, 356235759), + stream_name = c("A Creek", "A Different Creek") + )) }) test_that("fwa_add_stream_names_to_blk deals with missing blk", { - stream_name <- data.frame(blk = 356235759, - stream_name = "A Creek") + stream_name <- data.frame( + blk = 356235759, + stream_name = "A Creek" + ) x <- fwa_add_stream_names_to_blk(data.frame(blk = 356235760), - stream_name = stream_name) + stream_name = stream_name + ) - expect_identical(x, dplyr::tibble(blk = 356235760, - stream_name = NA_character_)) + expect_identical(x, dplyr::tibble( + blk = 356235760, + stream_name = NA_character_ + )) }) diff --git a/tests/testthat/test-add-upstream-split-to-rms.R b/tests/testthat/test-add-upstream-split-to-rms.R index c397ca2..76d9e87 100644 --- a/tests/testthat/test-add-upstream-split-to-rms.R +++ b/tests/testthat/test-add-upstream-split-to-rms.R @@ -1,6 +1,8 @@ test_that("fwa_add_upstream_split_to_rms simple example", { - x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L), - parent_blk = NA_integer_, parent_rm = NA_integer_) + x <- data.frame( + blk = 1L, rm = seq(1000L, 10000L, by = 1000L), + parent_blk = NA_integer_, parent_rm = NA_integer_ + ) y <- data.frame(blk = 1L, rm = 3000L, name = "new") x <- fwa_add_upstream_split_to_rms(x, y) @@ -14,14 +16,15 @@ test_that("fwa_add_upstream_split_to_rms simple example", { test_that("fwa_add_upstream_split_to_rms simple example all", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_add_upstream_split_to_rms(x, data.frame(blk = 1, rm = 0, name = "new2")) @@ -34,14 +37,15 @@ test_that("fwa_add_upstream_split_to_rms simple example all", { test_that("fwa_add_upstream_split_to_rms simple example partway up main", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_add_upstream_split_to_rms(x, data.frame(blk = 1, rm = 1, name = "new2")) @@ -54,14 +58,15 @@ test_that("fwa_add_upstream_split_to_rms simple example partway up main", { test_that("fwa_add_upstream_split_to_rms on trib", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_add_upstream_split_to_rms(x, data.frame(blk = 2, rm = 1, name = "new2")) @@ -75,14 +80,15 @@ test_that("fwa_add_upstream_split_to_rms on trib", { test_that("fwa_add_upstream_split_to_rms off top", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_add_upstream_split_to_rms(x, data.frame(blk = 1, rm = 10, name = "new2")) @@ -95,16 +101,17 @@ test_that("fwa_add_upstream_split_to_rms off top", { test_that("fwa_add_upstream_split_to_rms two splits", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) - - y <- data.frame(blk = c(1,2), rm = c(1,1), name = c("new2", "new1")) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) + + y <- data.frame(blk = c(1, 2), rm = c(1, 1), name = c("new2", "new1")) x <- fwa_add_upstream_split_to_rms(x, y) expect_s3_class(x, "tbl") diff --git a/tests/testthat/test-add-watershed-to-blk.R b/tests/testthat/test-add-watershed-to-blk.R index cf1c1c7..32ccad7 100644 --- a/tests/testthat/test-add-watershed-to-blk.R +++ b/tests/testthat/test-add-watershed-to-blk.R @@ -27,7 +27,8 @@ test_that("fwa_add_watershed_to_blk exclude excludes", { rlang::local_options(nocache = TRUE) x <- fwa_add_watershed_to_blk(data.frame(blk = 360883036, rm = 10), - exclude = TRUE) + exclude = TRUE + ) expect_s3_class(x, "sf") expect_identical(colnames(x), c("blk", "rm", "geometry")) expect_identical(x$rm, 10) diff --git a/tests/testthat/test-convert-rms-to-streams.R b/tests/testthat/test-convert-rms-to-streams.R index 2da8bb2..9dc793e 100644 --- a/tests/testthat/test-convert-rms-to-streams.R +++ b/tests/testthat/test-convert-rms-to-streams.R @@ -8,7 +8,7 @@ test_that("convert_rms_to_streams works", { expect_identical(nrow(x), 39L) # drop those streams that single points.... not sure why necessary - x <- sf::st_cast(x[-c(3,6,11,16),], "MULTIPOINT") + x <- sf::st_cast(x[-c(3, 6, 11, 16), ], "MULTIPOINT") expect_warning(x <- sf::st_cast(x, "POINT")) expect_s3_class(x$geometry, "sfc_POINT") diff --git a/tests/testthat/test-convert-stream-names-to-blks.R b/tests/testthat/test-convert-stream-names-to-blks.R index 4692c07..46ceb02 100644 --- a/tests/testthat/test-convert-stream-names-to-blks.R +++ b/tests/testthat/test-convert-stream-names-to-blks.R @@ -1,15 +1,21 @@ test_that("fwa_convert_stream_names_to_blks works", { expect_identical(fwa_convert_stream_names_to_blks("a name"), 576524986L) expect_identical(fwa_convert_stream_names_to_blks("a name2"), 151860402L) - expect_identical(fwa_convert_stream_names_to_blks(c("a name", "a name2")), - c(576524986L, 151860402L)) - expect_identical(fwa_convert_stream_names_to_blks(c("a name3", "a name3")), - c(597060036L, 597060036L)) + expect_identical( + fwa_convert_stream_names_to_blks(c("a name", "a name2")), + c(576524986L, 151860402L) + ) + expect_identical( + fwa_convert_stream_names_to_blks(c("a name3", "a name3")), + c(597060036L, 597060036L) + ) }) test_that("fwa_convert_stream_names_to_blks errors", { - expect_error(fwa_convert_stream_names_to_blks(597060036L), - "^`names` must be character or factor or NULL\\.$") + expect_error( + fwa_convert_stream_names_to_blks(597060036L), + "^`names` must be character or factor or NULL\\.$" + ) }) test_that("fwa_convert_stream_names_to_blks factors", { @@ -24,6 +30,8 @@ test_that("fwa_convert_stream_names_to_blks null", { test_that("fwa_convert_stream_names_to_blks missing values", { expect_identical(fwa_convert_stream_names_to_blks(character()), integer()) expect_identical(fwa_convert_stream_names_to_blks(NA_character_), NA_integer_) - expect_identical(fwa_convert_stream_names_to_blks(c("a name2", NA_integer_)), - c(151860402L, NA_integer_)) + expect_identical( + fwa_convert_stream_names_to_blks(c("a name2", NA_integer_)), + c(151860402L, NA_integer_) + ) }) diff --git a/tests/testthat/test-convert-stream-network-to-rms.R b/tests/testthat/test-convert-stream-network-to-rms.R index cbb138d..412e1ab 100644 --- a/tests/testthat/test-convert-stream-network-to-rms.R +++ b/tests/testthat/test-convert-stream-network-to-rms.R @@ -15,7 +15,7 @@ test_that("fwa_convert_stream_network_to_rms", { watershed <- fwa_add_watershed_to_blk(data.frame(blk = 356308001, rm = 1000)) network <- fwa_add_collection_to_polygon(watershed) - channel <- network[network$linear_feature_id == 700730484,] + channel <- network[network$linear_feature_id == 700730484, ] channel$blue_line_key <- 156308001L channel$linear_feature_id <- 100730484L channel$stream_order <- 1L diff --git a/tests/testthat/test-data.R b/tests/testthat/test-data.R index 5974e14..d9e1823 100644 --- a/tests/testthat/test-data.R +++ b/tests/testthat/test-data.R @@ -1,13 +1,15 @@ test_that("fwa_collection_name", { chk::check_data(fwa_collection_name, - values = list(collection_name = "", collection = ""), - exclusive = TRUE, order = TRUE, key = "collection_name") + values = list(collection_name = "", collection = ""), + exclusive = TRUE, order = TRUE, key = "collection_name" + ) expect_snapshot_data(fwa_collection_name, "collection_name") }) test_that("fwa_stream_name", { chk::check_data(fwa_stream_name, - values = list(blk = 1L, stream_name = ""), - exclusive = TRUE, order = TRUE, key = "blk") + values = list(blk = 1L, stream_name = ""), + exclusive = TRUE, order = TRUE, key = "blk" + ) expect_snapshot_data(fwa_stream_name, "stream_name") }) diff --git a/tests/testthat/test-find-stream-names.R b/tests/testthat/test-find-stream-names.R index 94f43bb..5008f6e 100644 --- a/tests/testthat/test-find-stream-names.R +++ b/tests/testthat/test-find-stream-names.R @@ -1,24 +1,34 @@ test_that("fwa_find_stream_names works simple case", { - expect_identical(fwa_find_stream_names("sangan"), - dplyr::tibble(stream_name = "Sangan River")) + expect_identical( + fwa_find_stream_names("sangan"), + dplyr::tibble(stream_name = "Sangan River") + ) }) test_that("fwa_find_stream_names no rows with non-match", { - expect_identical(fwa_find_stream_names("sangan2", ignore_case = FALSE), - dplyr::tibble(stream_name = character(0))) + expect_identical( + fwa_find_stream_names("sangan2", ignore_case = FALSE), + dplyr::tibble(stream_name = character(0)) + ) }) test_that("fwa_find_stream_names ignore case works", { - expect_identical(fwa_find_stream_names("sangan", ignore_case = FALSE), - dplyr::tibble(stream_name = character(0))) + expect_identical( + fwa_find_stream_names("sangan", ignore_case = FALSE), + dplyr::tibble(stream_name = character(0)) + ) }) test_that("fwa_find_stream_names works if same", { - expect_identical(fwa_find_stream_names("3 Mile Creek"), - dplyr::tibble(stream_name = "3 Mile Creek")) + expect_identical( + fwa_find_stream_names("3 Mile Creek"), + dplyr::tibble(stream_name = "3 Mile Creek") + ) }) test_that("fwa_find_stream_names deals with multiple matches", { - expect_identical(fwa_find_stream_names("adams"), - dplyr::tibble(stream_name = c("Adams Creek", "Adams River", "Marsh Adams Creek"))) + expect_identical( + fwa_find_stream_names("adams"), + dplyr::tibble(stream_name = c("Adams Creek", "Adams River", "Marsh Adams Creek")) + ) }) diff --git a/tests/testthat/test-get-section-from-rms.R b/tests/testthat/test-get-section-from-rms.R index 978892b..207bd49 100644 --- a/tests/testthat/test-get-section-from-rms.R +++ b/tests/testthat/test-get-section-from-rms.R @@ -32,19 +32,20 @@ test_that("fwa_get_section_from_rms different name", { test_that("fwa_get_section_from_rms sf", { x <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pb", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pb", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") y <- data.frame(blk = 1L, rm = c(1, 2, 4), section = c(3L, 7L, 10L)) - x <- fwa_add_section_to_rms(x,y) + x <- fwa_add_section_to_rms(x, y) z <- fwa_get_section_from_rms(x) expect_s3_class(z, "tbl") diff --git a/tests/testthat/test-get-segment-from-rms.R b/tests/testthat/test-get-segment-from-rms.R index ccf6e42..8d00ff7 100644 --- a/tests/testthat/test-get-segment-from-rms.R +++ b/tests/testthat/test-get-segment-from-rms.R @@ -1,19 +1,19 @@ - test_that("fwa_get_segment_from_rms sf", { x <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p2", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pa", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p2", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pa", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") y <- data.frame(blk = 1L, rm = c(1, 2, 4), section = c(3L, 7L, 10L)) - x <- fwa_add_section_to_rms(x,y) + x <- fwa_add_section_to_rms(x, y) z <- fwa_get_segment_from_rms(x, "popn") expect_s3_class(z, "tbl") diff --git a/tests/testthat/test-parent-blk-rms.R b/tests/testthat/test-parent-blk-rms.R index 74b1da9..de39ba9 100644 --- a/tests/testthat/test-parent-blk-rms.R +++ b/tests/testthat/test-parent-blk-rms.R @@ -24,6 +24,6 @@ test_that("parent_blk_rms works called twice", { }) test_that("parent_blk_rms errors if more than one parent_blk", { - rms <- data.frame(blk = c(10, 10), parent_blk = c(2,3)) + rms <- data.frame(blk = c(10, 10), parent_blk = c(2, 3)) chk::expect_chk_error(fwa_parent_blk_rms(10, rms)) }) diff --git a/tests/testthat/test-parent-stream-name-rms.R b/tests/testthat/test-parent-stream-name-rms.R index a06b760..cb7a656 100644 --- a/tests/testthat/test-parent-stream-name-rms.R +++ b/tests/testthat/test-parent-stream-name-rms.R @@ -37,7 +37,7 @@ test_that("parent_stream_name works if unrooted", { test_that("parent_stream_name errors if multiple in child stream name", { rms <- data.frame(blk = 10, parent_blk = 2) stream_name <- data.frame(blk = c(10, 11, 2), stream_name = c("Sub Creek", "Sub Creek", "Super Creek")) - chk::expect_chk_error(fwa_parent_stream_name_rms("Sub Creek", rms, stream_name), NA_character_) + chk::expect_chk_error(fwa_parent_stream_name_rms("Sub Creek", rms, stream_name), NA_character_) }) test_that("parent_stream_name handles multiple in parent stream name", { @@ -45,4 +45,3 @@ test_that("parent_stream_name handles multiple in parent stream name", { stream_name <- data.frame(blk = c(10, 11, 2), stream_name = c("Sub Creek", "Super Creek", "Super Creek")) expect_identical(fwa_parent_stream_name_rms("Sub Creek", rms, stream_name), "Super Creek") }) - diff --git a/tests/testthat/test-prune-rms.R b/tests/testthat/test-prune-rms.R index 6b28ba6..8270d93 100644 --- a/tests/testthat/test-prune-rms.R +++ b/tests/testthat/test-prune-rms.R @@ -1,6 +1,8 @@ test_that("fwa_prune_rms simple example", { - x <- data.frame(blk = 1L, rm = seq(1000L, 10000L, by = 1000L), - parent_blk = NA_integer_, parent_rm = NA_integer_) + x <- data.frame( + blk = 1L, rm = seq(1000L, 10000L, by = 1000L), + parent_blk = NA_integer_, parent_rm = NA_integer_ + ) y <- data.frame(blk = 1L, rm = 3000L) x <- fwa_prune_rms(x, y) @@ -14,14 +16,15 @@ test_that("fwa_prune_rms simple example", { test_that("fwa_prune_rms simple example all", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_prune_rms(x, data.frame(blk = 1, rm = 0, name = "new2")) @@ -33,14 +36,15 @@ test_that("fwa_prune_rms simple example all", { test_that("fwa_prune_rms simple example partway up main", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_prune_rms(x, data.frame(blk = 1, rm = 1, name = "new2")) @@ -53,14 +57,15 @@ test_that("fwa_prune_rms simple example partway up main", { test_that("fwa_prune_rms on trib", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_prune_rms(x, data.frame(blk = 2, rm = 1, name = "new2")) @@ -74,14 +79,15 @@ test_that("fwa_prune_rms on trib", { test_that("fwa_prune_rms off top", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) x <- fwa_prune_rms(x, data.frame(blk = 1, rm = 10)) @@ -94,16 +100,17 @@ test_that("fwa_prune_rms off top", { test_that("fwa_prune_rms two splits", { x <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 3, 0, 2, 1.5, - 3, 1, 2, 1.5, - 4, 0, 1, 0, - 4, 1, 1, 0) - - y <- data.frame(blk = c(1,2), rm = c(1,1), name = c("new2", "new1")) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 3, 0, 2, 1.5, + 3, 1, 2, 1.5, + 4, 0, 1, 0, + 4, 1, 1, 0 + ) + + y <- data.frame(blk = c(1, 2), rm = c(1, 1), name = c("new2", "new1")) x <- fwa_prune_rms(x, y) expect_s3_class(x, "tbl") diff --git a/tests/testthat/test-readme.R b/tests/testthat/test-readme.R index fa1e639..be5ceac 100644 --- a/tests/testthat/test-readme.R +++ b/tests/testthat/test-readme.R @@ -3,7 +3,7 @@ test_that("multiplication works", { expect_identical(streams, dplyr::tibble(stream_name = c("Steep Canyon Creek", "Steep Creek"))) blks <- fwa_add_blks_to_stream_name(streams) expect_equal(blks, dplyr::tibble(stream_name = c("Steep Canyon Creek", "Steep Creek", "Steep Creek", "Steep Creek"), blk = c(360883036L, 356362258L, 356534225L, 356570155L))) - blks <- blks[blks$blk == 356534225,] + blks <- blks[blks$blk == 356534225, ] wshed <- fwa_add_watershed_to_blk(blks) expect_s3_class(wshed, "sf") @@ -21,10 +21,11 @@ test_that("multiplication works", { "fwa_watershed_code", "gnis_id", "gnis_name", "gradient", "left_right_tributary", "length_metre", "linear_feature_id", "local_watershed_code", "localcode_ltree", "stream_magnitude", "stream_order", - "upstream_route_measure", "waterbody_key", + "upstream_route_measure", "waterbody_key", "watershed_code_50k", "watershed_group_code", "watershed_group_code_50k", "watershed_group_id", "watershed_key", "watershed_key_50k", "wscode_ltree", - "geometry")) + "geometry" + )) skip("Columns 'blk' and 'rm' in `rms` constructed from `x` must be a unique key.") rms <- fwa_convert_stream_network_to_rms(network, interval = 100) }) diff --git a/tests/testthat/test-snap-rm-to-point.R b/tests/testthat/test-snap-rm-to-point.R index 67f765c..3efbd9e 100644 --- a/tests/testthat/test-snap-rm-to-point.R +++ b/tests/testthat/test-snap-rm-to-point.R @@ -1,23 +1,25 @@ test_that("fwa_snap_rm_to_point works", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x <- fwa_snap_rm_to_point(x, rm) expect_s3_class(x, "sf") expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry")) expect_equal(x$blk, rep(356308001, 5)) expect_equal(x$rm, c(1000, 3000, 4000, 4000, 8000)) - expect_equal(x$distance_to_rm, c(873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, - 514.952511361304)) + expect_equal(x$distance_to_rm, c( + 873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, + 514.952511361304 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point handles different projection", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] rm <- sf::st_transform(rm, 4326L) x <- fwa_snap_rm_to_point(x, rm) expect_s3_class(x, "sf") @@ -33,8 +35,8 @@ test_that("fwa_snap_rm_to_point handles different projection", { test_that("fwa_snap_rm_to_point handles different projection x", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x <- sf::st_transform(x, 4326L) x <- fwa_snap_rm_to_point(x, rm) expect_s3_class(x, "sf") @@ -50,24 +52,26 @@ test_that("fwa_snap_rm_to_point handles different projection x", { test_that("fwa_snap_rm_to_point handles missing blk", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x$blk <- NULL x <- fwa_snap_rm_to_point(x, rm) expect_s3_class(x, "sf") expect_identical(colnames(x), c("rm", "distance_to_rm", "elevation", "geometry", "blk")) expect_equal(x$blk, rep(356308001, 5)) expect_equal(x$rm, c(1000, 3000, 4000, 4000, 8000)) - expect_equal(x$distance_to_rm, c(873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, - 514.952511361304)) + expect_equal(x$distance_to_rm, c( + 873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, + 514.952511361304 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point works one row", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm == 5000,] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm == 5000, ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x <- fwa_snap_rm_to_point(x, rm) expect_s3_class(x, "sf") expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry")) @@ -80,8 +84,8 @@ test_that("fwa_snap_rm_to_point works one row", { test_that("fwa_snap_rm_to_point works", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 9000, 10000), ] rm$blk[rm$rm == 3000] <- 2L x$blk[1:2] <- NA_integer_ x <- fwa_snap_rm_to_point(x, rm) @@ -89,16 +93,18 @@ test_that("fwa_snap_rm_to_point works", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry")) expect_equal(x$blk, c(356308001, 2, rep(356308001, 3))) expect_equal(x$rm, c(1000, 3000, 4000, 4000, 9000)) - expect_equal(x$distance_to_rm, c(873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, - 645.410842257104)) + expect_equal(x$distance_to_rm, c( + 873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, + 645.410842257104 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point works active geometry geometry", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 9000, 10000), ] rm$blk[rm$rm == 3000] <- 2L x$blk[1:2] <- NA_integer_ x$geometry2 <- rm$geometry[1:5] @@ -107,16 +113,18 @@ test_that("fwa_snap_rm_to_point works active geometry geometry", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry", "geometry2")) expect_equal(x$blk, c(356308001, 2, rep(356308001, 3))) expect_equal(x$rm, c(1000, 3000, 4000, 4000, 9000)) - expect_equal(x$distance_to_rm, c(873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, - 645.410842257104)) + expect_equal(x$distance_to_rm, c( + 873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, + 645.410842257104 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point doesn't replace non-matching blk with NA", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000),] + x <- rm[rm$rm %in% c(0, 2000, 5000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000), ] x$blk[1] <- 1L x <- fwa_snap_rm_to_point(x, rm) expect_s3_class(x, "sf") @@ -130,8 +138,8 @@ test_that("fwa_snap_rm_to_point doesn't replace non-matching blk with NA", { test_that("fwa_snap_rm_to_point works active geometry geometry2", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 9000, 10000), ] rm$blk[rm$rm == 3000] <- 2L x$blk[1:2] <- NA_integer_ x$geometry2 <- rm$geometry[1:5] @@ -151,8 +159,8 @@ test_that("fwa_snap_rm_to_point works active geometry geometry2", { test_that("fwa_snap_rm_to_point works segment", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x$section <- c(2L, 1L, 2L, 2L, 3L) rm$section <- c(1L, 1L, 2L, 3L, 3L, 3L) @@ -161,16 +169,18 @@ test_that("fwa_snap_rm_to_point works segment", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry", "section")) expect_equal(x$blk, rep(356308001, 5)) expect_equal(x$rm, c(4000, 3000, 4000, 4000, 8000)) - expect_equal(x$distance_to_rm, c(2954.89069472597, 535.63765010454, 754.230245890789, 610.731097004499, - 514.952511361304)) + expect_equal(x$distance_to_rm, c( + 2954.89069472597, 535.63765010454, 754.230245890789, 610.731097004499, + 514.952511361304 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point works segment no match", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x$section <- c(2L, 1L, 2L, 2L, 4L) rm$section <- c(1L, 1L, 2L, 3L, 3L, 3L) @@ -179,16 +189,18 @@ test_that("fwa_snap_rm_to_point works segment no match", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry", "section")) expect_equal(x$blk, rep(356308001, 5)) expect_equal(x$rm, c(4000, 3000, 4000, 4000, NA_integer_)) - expect_equal(x$distance_to_rm, c(2954.89069472597, 535.63765010454, 754.230245890789, 610.731097004499, - NA_real_)) + expect_equal(x$distance_to_rm, c( + 2954.89069472597, 535.63765010454, 754.230245890789, 610.731097004499, + NA_real_ + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point works segment missing value x", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x$section <- c(NA_integer_, 1L, 2L, 2L, 3L) rm$section <- c(1L, 1L, 2L, 3L, 3L, 3L) @@ -197,16 +209,18 @@ test_that("fwa_snap_rm_to_point works segment missing value x", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry", "section")) expect_equal(x$blk, c(rep(356308001, 5))) expect_equal(x$rm, c(1000, 3000, 4000, 4000, 8000)) - expect_equal(x$distance_to_rm, c(873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, - 514.952511361304)) + expect_equal(x$distance_to_rm, c( + 873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, + 514.952511361304 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point works segment missing value rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x$section <- c(1L, 1L, 2L, 2L, 3L) rm$section <- c(NA_integer_, 1L, 2L, 3L, 3L, 3L) @@ -215,16 +229,18 @@ test_that("fwa_snap_rm_to_point works segment missing value rm", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry", "section")) expect_equal(x$blk, c(rep(356308001, 5))) expect_equal(x$rm, c(3000, 3000, 4000, 4000, 8000)) - expect_equal(x$distance_to_rm, c(2331.96951067872, 535.63765010454, 754.230245890789, 610.731097004499, - 514.952511361304)) + expect_equal(x$distance_to_rm, c( + 2331.96951067872, 535.63765010454, 754.230245890789, 610.731097004499, + 514.952511361304 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point works segment missing value x and rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x$section <- c(NA_integer_, 1L, 2L, 2L, 3L) rm$section <- c(NA_integer_, 1L, 2L, 3L, 3L, 3L) @@ -233,20 +249,22 @@ test_that("fwa_snap_rm_to_point works segment missing value x and rm", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry", "section")) expect_equal(x$blk, c(rep(356308001, 5))) expect_equal(x$rm, c(1000, 3000, 4000, 4000, 8000)) - expect_equal(x$distance_to_rm, c(873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, - 514.952511361304)) + expect_equal(x$distance_to_rm, c( + 873.50885850392, 535.63765010454, 754.230245890789, 610.731097004499, + 514.952511361304 + )) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_point works segment missing value x and rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 9000, 10000), ] x$section <- c(1L, 1L, 3L, 2L, 2L) rm$section <- c(2L, 2L, 2L, 2L, 2L, 2L) x$blk[1:3] <- NA_integer_ - rm2 <- rm[1:2,] + rm2 <- rm[1:2, ] rm2$blk <- 2L rm2$section <- c(1L, 1L) rm <- dplyr::bind_rows(rm, rm2) @@ -256,7 +274,9 @@ test_that("fwa_snap_rm_to_point works segment missing value x and rm", { expect_identical(colnames(x), c("blk", "rm", "distance_to_rm", "elevation", "geometry", "section")) expect_equal(x$blk, c(2, 2, NA, 356308001, 356308001)) expect_equal(x$rm, c(1000L, 3000L, NA, 4000L, 8000L)) - expect_equal(x$distance_to_rm, c(873.50885850392, 535.63765010454, NA, 610.731097004499, - 514.952511361304)) + expect_equal(x$distance_to_rm, c( + 873.50885850392, 535.63765010454, NA, 610.731097004499, + 514.952511361304 + )) expect_s3_class(x$geometry, "sfc_POINT") }) diff --git a/tests/testthat/test-snap-rm-to-rms.R b/tests/testthat/test-snap-rm-to-rms.R index 155772b..c876ed8 100644 --- a/tests/testthat/test-snap-rm-to-rms.R +++ b/tests/testthat/test-snap-rm-to-rms.R @@ -3,8 +3,8 @@ test_that("fwa_snap_rm_to_rms works", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rm_to_rms(x, rm) expect_s3_class(x, "sf") expect_identical(colnames(x), c("blk", "new_blk", "rm", "new_rm", "distance_to_new_rm", "elevation", "geometry")) @@ -12,7 +12,7 @@ test_that("fwa_snap_rm_to_rms works", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000)) - expect_equal(x$distance_to_new_rm, rep(0,5)) + expect_equal(x$distance_to_new_rm, rep(0, 5)) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -21,8 +21,8 @@ test_that("fwa_snap_rm_to_rms works new_blk", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x$new_blk <- x$blk x <- fwa_snap_rm_to_rms(x, rm) expect_s3_class(x, "sf") @@ -31,7 +31,7 @@ test_that("fwa_snap_rm_to_rms works new_blk", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000)) - expect_equal(x$distance_to_new_rm, rep(0,5)) + expect_equal(x$distance_to_new_rm, rep(0, 5)) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -40,8 +40,8 @@ test_that("fwa_snap_rm_to_rms works new_blk different", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] rm$blk <- rm$blk + 1L x$new_blk <- x$blk + 1L x <- fwa_snap_rm_to_rms(x, rm) @@ -51,7 +51,7 @@ test_that("fwa_snap_rm_to_rms works new_blk different", { expect_identical(x$new_blk, x$blk + 1L) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000)) - expect_equal(x$distance_to_new_rm, rep(0,5)) + expect_equal(x$distance_to_new_rm, rep(0, 5)) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -60,8 +60,8 @@ test_that("fwa_snap_rm_to_rms works multiple blks", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x2 <- x x2$blk <- 1 @@ -78,7 +78,7 @@ test_that("fwa_snap_rm_to_rms works multiple blks", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000, 0, 2000, 5000, 6000, 7000)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000, 0, 2000, 5000, 6000, 7000)) - expect_equal(x$distance_to_new_rm, rep(0,10)) + expect_equal(x$distance_to_new_rm, rep(0, 10)) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -87,8 +87,8 @@ test_that("fwa_snap_rm_to_rms works multiple blks new_rms", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x2 <- x x2$blk <- 1 @@ -98,7 +98,7 @@ test_that("fwa_snap_rm_to_rms works multiple blks new_rms", { x <- dplyr::bind_rows(x, x2) rm <- dplyr::bind_rows(rm, rm2) - x$new_rm <- c(NA, 2000, rep(NA,3), 0, rep(NA,4)) + x$new_rm <- c(NA, 2000, rep(NA, 3), 0, rep(NA, 4)) x <- fwa_snap_rm_to_rms(x, rm) expect_s3_class(x, "sf") @@ -107,7 +107,7 @@ test_that("fwa_snap_rm_to_rms works multiple blks new_rms", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000, 0, 2000, 5000, 6000, 7000)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000, 0, 2000, 5000, 6000, 7000)) - expect_equal(x$distance_to_new_rm, rep(0,10)) + expect_equal(x$distance_to_new_rm, rep(0, 10)) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -116,8 +116,8 @@ test_that("fwa_snap_rm_to_rms works multiple blks to same new_blk", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x2 <- x x2$blk <- 1 @@ -135,20 +135,23 @@ test_that("fwa_snap_rm_to_rms works multiple blks to same new_blk", { expect_identical(colnames(x), c("blk", "new_blk", "rm", "new_rm", "distance_to_new_rm", "elevation", "geometry")) expect_equal(x$blk, c(rep(1, 5), rep(356308001, 5))) expect_equal(x$new_blk, rep(356308001, 10)) - expect_equal(x$rm, - c(0L, 2000L, 5000L, 6000L, 7000L, 0L, 2000L, 5000L, 6000L, 7000L - )) - expect_equal(x$new_rm, - c(1000, 3000, 6000, 7000, 8000, 1000, 3000, 6000, 7000, 8000)) - expect_equal(x$distance_to_new_rm, rep(0,10)) + expect_equal( + x$rm, + c(0L, 2000L, 5000L, 6000L, 7000L, 0L, 2000L, 5000L, 6000L, 7000L) + ) + expect_equal( + x$new_rm, + c(1000, 3000, 6000, 7000, 8000, 1000, 3000, 6000, 7000, 8000) + ) + expect_equal(x$distance_to_new_rm, rep(0, 10)) expect_s3_class(x$geometry, "sfc_POINT") }) test_that("fwa_snap_rm_to_rms doesn't snap mouth by default", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001), nocache = FALSE) - x <- rm[rm$rm %in% c(0, 3000, 6000),] - rm <- rm[rm$rm %in% c(3000, 6000, 9000),] + x <- rm[rm$rm %in% c(0, 3000, 6000), ] + rm <- rm[rm$rm %in% c(3000, 6000, 9000), ] rm$rm <- c(3000, 6000, 0) x <- fwa_snap_rm_to_rms(x, rm) @@ -166,8 +169,8 @@ test_that("fwa_snap_rm_to_rms doesn't snap mouth by default", { test_that("fwa_snap_rm_to_rms does snap mouth using new_rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001), nocache = FALSE) - x <- rm[rm$rm %in% c(0, 3000, 6000),] - rm <- rm[rm$rm %in% c(3000, 6000, 9000),] + x <- rm[rm$rm %in% c(0, 3000, 6000), ] + rm <- rm[rm$rm %in% c(3000, 6000, 9000), ] rm$rm <- c(3000, 6000, 0) x$new_rm <- c(0, NA, NA) @@ -187,8 +190,8 @@ test_that("fwa_snap_rm_to_rms does snap mouth using new_rm", { test_that("fwa_snap_rm_to_rms no x", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001), nocache = FALSE) - x <- rm[FALSE,] - rm <- rm[rm$rm %in% c(3000, 6000, 9000),] + x <- rm[FALSE, ] + rm <- rm[rm$rm %in% c(3000, 6000, 9000), ] rm$rm <- c(3000, 6000, 0) x <- fwa_snap_rm_to_rms(x, rm) @@ -206,8 +209,8 @@ test_that("fwa_snap_rm_to_rms no x", { test_that("fwa_snap_rm_to_rms no rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001), nocache = FALSE) - x <- rm[rm$rm %in% c(0, 3000, 6000),] - rm <- rm[FALSE,] + x <- rm[rm$rm %in% c(0, 3000, 6000), ] + rm <- rm[FALSE, ] x <- fwa_snap_rm_to_rms(x, rm) expect_s3_class(x, "sf") @@ -223,8 +226,8 @@ test_that("fwa_snap_rm_to_rms no rm", { test_that("fwa_snap_rm_to_rms no x or rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001), nocache = FALSE) - x <- rm[FALSE,] - rm <- rm[FALSE,] + x <- rm[FALSE, ] + rm <- rm[FALSE, ] x <- fwa_snap_rm_to_rms(x, rm) @@ -241,20 +244,22 @@ test_that("fwa_snap_rm_to_rms no x or rm", { test_that("fwa_snap_rm_to_rms new_rm errors if new_rm not in rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001), nocache = FALSE) - x <- rm[rm$rm %in% c(0, 3000, 6000),] - rm <- rm[rm$rm %in% c(3000, 6000, 9000),] + x <- rm[rm$rm %in% c(0, 3000, 6000), ] + rm <- rm[rm$rm %in% c(3000, 6000, 9000), ] rm$rm <- c(3000, 6000, 0) x$new_rm <- c(3000, 6000, 9000) - expect_error(fwa_snap_rm_to_rms(x, rm), - "^All `x\\$new_rm` values must be in `rm\\$rm` by `blk`\\.$") + expect_error( + fwa_snap_rm_to_rms(x, rm), + "^All `x\\$new_rm` values must be in `rm\\$rm` by `blk`\\.$" + ) }) test_that("fwa_snap_rm_to_rms new_rm errors if not sorted", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001), nocache = FALSE) - x <- rm[rm$rm %in% c(0, 3000, 6000),] - rm <- rm[rm$rm %in% c(3000, 6000, 9000),] + x <- rm[rm$rm %in% c(0, 3000, 6000), ] + rm <- rm[rm$rm %in% c(3000, 6000, 9000), ] rm$rm <- c(3000, 6000, 0) x$new_rm <- c(3000, 6000, 0) @@ -266,8 +271,8 @@ test_that("fwa_snap_rm_to_rms not exceeds subsequent new_rm", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 7000, 5000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 7000, 5000), ] x$new_rm <- c(NA, 4000, NA, NA, NA) x <- fwa_snap_rm_to_rms(x, rm) @@ -286,8 +291,8 @@ test_that("fwa_snap_rm_to_rms snap new_rm to mouth", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 7000, 5000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 7000, 5000), ] rm$rm <- c(1000, 3000, 4000, 8000, 7000, 0) x$new_rm <- c(NA, 4000, NA, NA, NA) @@ -307,8 +312,8 @@ test_that("fwa_snap_rm_to_rms snap new_rm to mouth already set", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 7000, 5000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000, 7000, 5000), ] rm$rm <- c(1000, 3000, 4000, 8000, 7000, 0) x$new_rm <- c(1000, 4000, NA, NA, NA) @@ -328,8 +333,8 @@ test_that("fwa_snap_rm_to_rms only allow increasing order", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(1000, 3000, 4000, 8000), ] rm$rm <- c(1000, 8000, 4000, 3000) x <- fwa_snap_rm_to_rms(x, rm) @@ -339,8 +344,10 @@ test_that("fwa_snap_rm_to_rms only allow increasing order", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000)) expect_equal(x$new_rm, c(1000, 3000, 8000, 8000, 8000)) - expect_equal(x$distance_to_new_rm, c(873.50885850392, 2167.96002745135, 1455.35594246782, 1333.22900356052, - 1377.43784259615)) + expect_equal(x$distance_to_new_rm, c( + 873.50885850392, 2167.96002745135, 1455.35594246782, 1333.22900356052, + 1377.43784259615 + )) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -349,8 +356,8 @@ test_that("fwa_snap_rm_to_rms respects new_rm and only allow increasing order", rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 8000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 8000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 8000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 8000), ] rm$rm <- c(0, 2000, 7000, 6000, 8000) x$new_rm <- c(NA, NA, 6000, NA, NA, NA) @@ -361,8 +368,7 @@ test_that("fwa_snap_rm_to_rms respects new_rm and only allow increasing order", expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000, 8000)) expect_equal(x$new_rm, c(0, 2000, 6000, 7000, 7000, 8000)) - expect_equal(x$distance_to_new_rm, c(0, 0, 772.26037010328, 772.26037010328, 1161.55661705109, 0 - )) + expect_equal(x$distance_to_new_rm, c(0, 0, 772.26037010328, 772.26037010328, 1161.55661705109, 0)) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -371,8 +377,8 @@ test_that("fwa_snap_rm_to_rms interpolates block", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 1000, 2000, 3000, 4000, 8000),] - rm <- rm[rm$rm %in% c(0, 4000, 5000, 6000, 7000, 8000),] + x <- rm[rm$rm %in% c(0, 1000, 2000, 3000, 4000, 8000), ] + rm <- rm[rm$rm %in% c(0, 4000, 5000, 6000, 7000, 8000), ] rm$rm <- c(0, 4000, 1000, 2000, 3000, 8000) x <- fwa_snap_rm_to_rms(x, rm) @@ -391,9 +397,9 @@ test_that("fwa_snap_rm_to_rms interpolates blocks", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[c(1:5, 1:5, 1:5),] - x$rm <- c(c(1,4,7,10,13), c(2,5,8,11,14), c(3,6,9,12,15)) - x <- x[order(x$rm),] + x <- rm[c(1:5, 1:5, 1:5), ] + x$rm <- c(c(1, 4, 7, 10, 13), c(2, 5, 8, 11, 14), c(3, 6, 9, 12, 15)) + x <- x[order(x$rm), ] x$new_rm <- c(1, rep(NA, 14)) rm <- x @@ -404,8 +410,10 @@ test_that("fwa_snap_rm_to_rms interpolates blocks", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, 1:15) expect_equal(x$new_rm, c(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 13, 14, 15)) - expect_equal(x$distance_to_new_rm, c(0, 0, 0, 0, 0, 0, 0, 0, 535.637650106249, 0, 0, 775.632623248084, - 0, 0, 0)) + expect_equal(x$distance_to_new_rm, c( + 0, 0, 0, 0, 0, 0, 0, 0, 535.637650106249, 0, 0, 775.632623248084, + 0, 0, 0 + )) expect_s3_class(x$geometry, "sfc_POINT") }) @@ -414,8 +422,8 @@ test_that("fwa_snap_rm_to_rms multiple blks to 1 blk", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] rm2 <- rm rm$blk <- 1 @@ -432,8 +440,7 @@ test_that("fwa_snap_rm_to_rms multiple blks to 1 blk", { expect_identical(colnames(x), c("blk", "new_blk", "rm", "new_rm", "distance_to_new_rm", "elevation", "geometry")) expect_equal(x$blk, c(rep(2, 5), rep(356308001, 5))) expect_equal(x$new_blk, rep(356308001, 10)) - expect_equal(x$rm, c(0L, 2000L, 5000L, 6000L, 7000L, 0L, 2000L, 5000L, 6000L, 7000L - )) + expect_equal(x$rm, c(0L, 2000L, 5000L, 6000L, 7000L, 0L, 2000L, 5000L, 6000L, 7000L)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000, 0, 2000, 5000, 6000, 7000)) expect_equal(x$distance_to_new_rm, c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) expect_s3_class(x$geometry, "sfc_POINT") diff --git a/tests/testthat/test-snap-rms-to-rms.R b/tests/testthat/test-snap-rms-to-rms.R index ad7b84f..bfa6592 100644 --- a/tests/testthat/test-snap-rms-to-rms.R +++ b/tests/testthat/test-snap-rms-to-rms.R @@ -3,8 +3,8 @@ test_that("fwa_snap_rms_to_rms works", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x @@ -14,7 +14,7 @@ test_that("fwa_snap_rms_to_rms works", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 2000, 5000, 6000, 7000)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000)) - expect_equal(x$distance_to_new_rm, rep(0,5)) + expect_equal(x$distance_to_new_rm, rep(0, 5)) expect_s3_class(x$geometry, "sfc_POINT") expect_s3_class(rm, "sf") @@ -32,9 +32,9 @@ test_that("fwa_snap_rms_to_rms not snaps mouths to mouths", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(2000, 5000, 6000, 7000),] + x <- rm[rm$rm %in% c(2000, 5000, 6000, 7000), ] x$rm <- c(0, 5000, 6000, 7000) - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm) rm <- x$rm x <- x$x @@ -44,7 +44,7 @@ test_that("fwa_snap_rms_to_rms not snaps mouths to mouths", { expect_identical(x$new_blk, x$blk) expect_equal(x$rm, c(0, 5000, 6000, 7000)) expect_equal(x$new_rm, c(2000, 5000, 6000, 7000)) - expect_equal(x$distance_to_new_rm, rep(0,4)) + expect_equal(x$distance_to_new_rm, rep(0, 4)) expect_s3_class(x$geometry, "sfc_POINT") expect_s3_class(rm, "sf") @@ -62,9 +62,9 @@ test_that("fwa_snap_rms_to_rms snaps mouths to mouths", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(2000, 5000, 6000, 7000),] + x <- rm[rm$rm %in% c(2000, 5000, 6000, 7000), ] x$rm <- c(0, 5000, 6000, 7000) - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] x <- fwa_snap_rms_to_rms(x, rm, snap_mouths = TRUE) rm <- x$rm x <- x$x @@ -92,8 +92,8 @@ test_that("fwa_snap_rms_to_rms can deal with mising blks", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] rm$blk[1:2] <- 1 x <- fwa_snap_rms_to_rms(x, rm) @@ -110,7 +110,7 @@ test_that("fwa_snap_rms_to_rms can deal with mising blks", { expect_s3_class(rm, "sf") expect_identical(colnames(rm), c("blk", "new_blk", "rm", "new_rm", "distance_to_new_rm", "elevation", "geometry")) - expect_equal(rm$blk, c(1,1,rep(356308001, 4))) + expect_equal(rm$blk, c(1, 1, rep(356308001, 4))) expect_identical(x$new_blk, x$blk) expect_equal(rm$rm, c(0, 2000, 5000, 6000, 7000, 10000)) expect_equal(rm$new_rm, c(NA, NA, 2000, 6000L, 7000L, 7000L)) @@ -123,8 +123,8 @@ test_that("fwa_snap_rms_to_rms multiple blks to 1 blk", { rm <- fwa_add_rms_to_blk(data.frame(blk = 356308001)) - x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000),] - rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000),] + x <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000), ] + rm <- rm[rm$rm %in% c(0, 2000, 5000, 6000, 7000, 10000), ] rm2 <- rm rm$blk <- 1 @@ -143,8 +143,7 @@ test_that("fwa_snap_rms_to_rms multiple blks to 1 blk", { expect_identical(colnames(x), c("blk", "new_blk", "rm", "new_rm", "distance_to_new_rm", "elevation", "geometry")) expect_equal(x$blk, c(rep(2, 5), rep(356308001, 5))) expect_identical(x$new_blk, rep(356308001L, 10)) - expect_equal(x$rm, c(0L, 2000L, 5000L, 6000L, 7000L, 0L, 2000L, 5000L, 6000L, 7000L - )) + expect_equal(x$rm, c(0L, 2000L, 5000L, 6000L, 7000L, 0L, 2000L, 5000L, 6000L, 7000L)) expect_equal(x$new_rm, c(0, 2000, 5000, 6000, 7000, 0, 2000, 5000, 6000, 7000)) expect_equal(x$distance_to_new_rm, c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) expect_s3_class(x$geometry, "sfc_POINT") @@ -154,11 +153,11 @@ test_that("fwa_snap_rms_to_rms multiple blks to 1 blk", { expect_equal(rm$blk, c(rep(1, 6), rep(356308001, 6))) expect_identical(rm$new_blk, rm$blk) - expect_equal(rm$rm, c(1000, 3000, 6000, 7000, 8000, 11000, 0, 2000, 5000, 6000, 7000, - 10000)) - expect_equal(rm$new_rm, c(NA, NA, NA, NA, NA, NA, 0L, 2000L, 5000L, 6000L, 7000L, 7000L + expect_equal(rm$rm, c( + 1000, 3000, 6000, 7000, 8000, 11000, 0, 2000, 5000, 6000, 7000, + 10000 )) + expect_equal(rm$new_rm, c(NA, NA, NA, NA, NA, NA, 0L, 2000L, 5000L, 6000L, 7000L, 7000L)) expect_equal(rm$distance_to_new_rm, c(NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 1496.94367005726)) expect_s3_class(rm$geometry, "sfc_POINT") }) - diff --git a/tests/testthat/test-swap-branches-rms.R b/tests/testthat/test-swap-branches-rms.R index 702e5b1..5d495bd 100644 --- a/tests/testthat/test-swap-branches-rms.R +++ b/tests/testthat/test-swap-branches-rms.R @@ -1,13 +1,13 @@ test_that("fwa_swap_branches_rms works", { - rm <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 1, 2, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 2, 2, 1, 1) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 1, 2, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 2, 2, 1, 1 + ) x <- fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE) expect_identical(colnames(x), c("blk", "rm", "parent_blk", "parent_rm")) @@ -17,55 +17,61 @@ test_that("fwa_swap_branches_rms works", { }) test_that("fwa_swap_branches_rms useful error message no points and adjust", { - rm <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1) - - chk::expect_chk_error(fwa_swap_branches_rms(rm, data.frame(blk = 2)), - "`x` must inherit from S3 class 'sf'\\.$") + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1 + ) + + chk::expect_chk_error( + fwa_swap_branches_rms(rm, data.frame(blk = 2)), + "`x` must inherit from S3 class 'sf'\\.$" + ) }) test_that("fwa_swap_branches_rms useful error unrooted trib", { - rm <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 1, 1, 1) - - chk::expect_chk_error(fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE), - "^`blk` 2 from `x` is unrooted \\(missing rm == 0\\)\\.$") + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 1, 1, 1 + ) + + chk::expect_chk_error( + fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE), + "^`blk` 2 from `x` is unrooted \\(missing rm == 0\\)\\.$" + ) }) test_that("fwa_swap_branches_rms useful error duplicates trib", { - rm <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 2, 0, 1, 1) - - chk::expect_chk_error(fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE), - "^Columns 'blk' and 'rm' in `x` must be a unique key\\.$") + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 2, 0, 1, 1 + ) + + chk::expect_chk_error( + fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE), + "^Columns 'blk' and 'rm' in `x` must be a unique key\\.$" + ) }) test_that("fwa_swap_branches_rms works with a trib child", { - rm <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 1, 2, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 2, 2, 1, 1, - 3, 0, 2, 1, - 3, 1, 2, 1) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 1, 2, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 2, 2, 1, 1, + 3, 0, 2, 1, + 3, 1, 2, 1 + ) x <- fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE) expect_identical(colnames(x), c("blk", "rm", "parent_blk", "parent_rm")) @@ -75,17 +81,17 @@ test_that("fwa_swap_branches_rms works with a trib child", { }) test_that("fwa_swap_branches_rms works with a main child", { - rm <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 1, 2, NA_integer_, NA_integer_, - 2, 0, 1, 1, - 2, 1, 1, 1, - 2, 2, 1, 1, - 3, 0, 1, 2, - 3, 1, 1, 2) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 1, 2, NA_integer_, NA_integer_, + 2, 0, 1, 1, + 2, 1, 1, 1, + 2, 2, 1, 1, + 3, 0, 1, 2, + 3, 1, 1, 2 + ) x <- fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE) expect_identical(colnames(x), c("blk", "rm", "parent_blk", "parent_rm")) @@ -95,16 +101,16 @@ test_that("fwa_swap_branches_rms works with a main child", { }) test_that("fwa_swap_branches_rms works with a trib child and part parent_rm", { - rm <- dplyr::tribble( ~blk, ~rm, ~parent_blk, ~parent_rm, - 1, 0, NA_integer_, NA_integer_, - 1, 1, NA_integer_, NA_integer_, - 1, 2, NA_integer_, NA_integer_, - 1, 3, NA_integer_, NA_integer_, - 2, 0, 1, 0.5, - 2, 1, 1, 0.5, - 2, 2, 1, 0.5) + 1, 0, NA_integer_, NA_integer_, + 1, 1, NA_integer_, NA_integer_, + 1, 2, NA_integer_, NA_integer_, + 1, 3, NA_integer_, NA_integer_, + 2, 0, 1, 0.5, + 2, 1, 1, 0.5, + 2, 2, 1, 0.5 + ) x <- fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE) @@ -115,16 +121,16 @@ test_that("fwa_swap_branches_rms works with a trib child and part parent_rm", { }) test_that("fwa_swap_branches_rms works with a trib child and part parent_rm and adjust points", { - rm <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, - 1, 0, NA_integer_, NA_integer_, 0, 0, - 1, 1, NA_integer_, NA_integer_, 1, 0, - 1, 2, NA_integer_, NA_integer_, 2, 0, - 1, 3, NA_integer_, NA_integer_, 3, 0, - 2, 0, 1, 0.5, 0.5, 0, - 2, 1, 1, 0.5, 0.5, 1, - 2, 2, 1, 0.5, 0.5, 2) |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, + 1, 0, NA_integer_, NA_integer_, 0, 0, + 1, 1, NA_integer_, NA_integer_, 1, 0, + 1, 2, NA_integer_, NA_integer_, 2, 0, + 1, 3, NA_integer_, NA_integer_, 3, 0, + 2, 0, 1, 0.5, 0.5, 0, + 2, 1, 1, 0.5, 0.5, 1, + 2, 2, 1, 0.5, 0.5, 2 + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") x <- fwa_swap_branches_rms(rm, data.frame(blk = 2)) @@ -136,16 +142,16 @@ test_that("fwa_swap_branches_rms works with a trib child and part parent_rm and }) test_that("fwa_swap_branches_rms preserves pops not adjust points", { - rm <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pb", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pb", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") x <- fwa_swap_branches_rms(rm, data.frame(blk = 2), adjust_points = FALSE) @@ -158,16 +164,16 @@ test_that("fwa_swap_branches_rms preserves pops not adjust points", { }) test_that("fwa_swap_branches_rms preserves pops", { - rm <- dplyr::tribble( - ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, - 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", - 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", - 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", - 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", - 2, 0, 1, 0.5, 0.5, 0, "pa", - 2, 1, 1, 0.5, 0.5, 1, "pb", - 2, 2, 1, 0.5, 0.5, 2, "pc") |> + ~blk, ~rm, ~parent_blk, ~parent_rm, ~x, ~y, ~popn, + 1, 0, NA_integer_, NA_integer_, 0, 0, "p1", + 1, 1, NA_integer_, NA_integer_, 1, 0, "p2", + 1, 2, NA_integer_, NA_integer_, 2, 0, "p3", + 1, 3, NA_integer_, NA_integer_, 3, 0, "p4", + 2, 0, 1, 0.5, 0.5, 0, "pa", + 2, 1, 1, 0.5, 0.5, 1, "pb", + 2, 2, 1, 0.5, 0.5, 2, "pc" + ) |> sf::st_as_sf(coords = c("x", "y"), dim = "XY") x <- fwa_swap_branches_rms(rm, data.frame(blk = 2))