Skip to content

Commit 352d44b

Browse files
Merge pull request #18 from FRDC-SHL/dev
# LITAP 0.6.0 - Import x/y coords or create them if they don't exist - flow_mapper() now requires grid or infers from x/y value of input files - form_mapper() and wepp_mapper() now use grid inferred from x/y value of flow_mapper() output files - flow_mapper() now has upslope_m (upslope cells * grid^2) - flow_mapper() calculates UCED - facet_mapper() calculates buffer edges - Simplify output with 'debug' argument (if false, removes intermediate files) - Simplify output columns by removing intermediate ones - Remove option to 'end' a run prematurely (required due to simplified output) - Remove dbf output option because it truncates column names - Fix flow_mapper() inconsistencies - Add extra data output "topographical_derivatives" in facet_mapper - Initial work on all_points data output
2 parents 50975fe + 048e33f commit 352d44b

251 files changed

Lines changed: 969721 additions & 4679 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/R-CMD-check.yaml

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,30 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
config:
17+
- {os: macOS-latest, r: 'release'}
1718
- {os: windows-latest, r: 'release'}
18-
- {os: macOS-latest, r: 'release'}
19-
- {os: ubuntu-latest, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
20-
- {os: ubuntu-latest, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
19+
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
20+
- {os: ubuntu-latest, r: 'release'}
21+
- {os: ubuntu-latest, r: 'oldrel-1'}
2122

2223
env:
23-
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
24-
RSPM: ${{ matrix.config.rspm }}
2524
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
25+
R_KEEP_PKG_SOURCE: yes
2626

2727
steps:
2828
- uses: actions/checkout@v2
2929

30-
- uses: r-lib/actions/setup-r@v1
30+
- uses: r-lib/actions/setup-r@v2
3131
with:
3232
r-version: ${{ matrix.config.r }}
33+
http-user-agent: ${{ matrix.config.http-user-agent }}
34+
use-public-rspm: true
3335

34-
- uses: r-lib/actions/setup-pandoc@v1
36+
- uses: r-lib/actions/setup-pandoc@v2
3537

36-
- name: Query dependencies
37-
run: |
38-
install.packages('remotes')
39-
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
40-
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
41-
shell: Rscript {0}
42-
43-
- name: Restore R package cache
44-
if: runner.os != 'Windows'
45-
uses: actions/cache@v2
38+
- uses: r-lib/actions/setup-r-dependencies@v2
4639
with:
47-
path: ${{ env.R_LIBS_USER }}
48-
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
49-
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
50-
51-
- name: Install system dependencies
52-
if: runner.os == 'Linux'
53-
run: |
54-
while read -r cmd
55-
do
56-
eval sudo $cmd
57-
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
58-
59-
- name: Install dependencies
60-
run: |
61-
remotes::install_deps(dependencies = TRUE)
62-
remotes::install_cran("rcmdcheck")
63-
shell: Rscript {0}
40+
extra-packages: any::rcmdcheck
41+
needs: check
6442

65-
- name: Check
66-
env:
67-
_R_CHECK_CRAN_INCOMING_REMOTE_: false
68-
run: |
69-
options(crayon.enabled = TRUE)
70-
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
71-
shell: Rscript {0}
72-
73-
- name: Upload check results
74-
if: failure()
75-
uses: actions/upload-artifact@main
76-
with:
77-
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
78-
path: check
43+
- uses: r-lib/actions/check-r-package@v2

DESCRIPTION

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: LITAP
22
Type: Package
33
Title: Landscape Integrated Terrain Analysis Package
4-
Version: 0.5.0
4+
Version: 0.6.0
55
Authors@R: c(
66
person("Steffi", "LaZerte", email = "sel@steffilazerte.ca", role = c("aut","cre")),
77
person("Sheng", "Li", email = "sheng.li@canada.ca", role = "aut"),
@@ -34,15 +34,17 @@ Imports:
3434
stringr (>= 1.2.0),
3535
tibble (>= 2.1.3),
3636
tidyselect (>= 1.1.0),
37-
tidyr (>= 1.0.0)
37+
tidyr (>= 1.0.0),
38+
writexl (>= 1.4.0)
3839
Suggests:
40+
gt (>= 0.3.1),
3941
foreign (>= 0.8.67),
4042
knitr,
4143
microbenchmark,
4244
readxl,
4345
rgdal,
4446
testthat (>= 3.0.0)
4547
VignetteBuilder: knitr
46-
RoxygenNote: 7.1.1
48+
RoxygenNote: 7.1.2
4749
Roxygen: list(markdown = TRUE)
4850
Config/testthat/edition: 3

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export(flow_mapper)
66
export(flow_plot)
77
export(form_mapper)
88
export(load_file)
9-
export(merge_flow_form)
109
export(slope_gc)
1110
export(wepp_mapper)
1211
importFrom(magrittr,"%>%")
12+
importFrom(magrittr,"%T>%")

NEWS.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# LITAP 0.6.0
2+
- Import x/y coords or create them if they don't exist
3+
- flow_mapper() now requires grid or infers from x/y value of input files
4+
- form_mapper() and wepp_mapper() now use grid inferred from x/y value of
5+
flow_mapper() output files
6+
- flow_mapper() now has upslope_m (upslope cells * grid^2)
7+
- flow_mapper() calculates UCED
8+
- facet_mapper() calculates buffer edges
9+
- Simplify output with 'debug' argument (if false, removes intermediate files)
10+
- Simplify output columns by removing intermediate ones
11+
- Remove option to 'end' a run prematurely (required due to simplified output)
12+
- Remove dbf output option because it truncates column names
13+
- Fix flow_mapper() inconsistencies
14+
- Add extra data output "topographical_derivatives" in facet_mapper
15+
- Initial work on all_points data output
16+
117
# LITAP 0.5.0
218
- Load files forces to numeric (fixes problems where some imports in character)
319
- Corrected `slope_gc()` directions (fixes #10)

R/LITAP_combine_files.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' Combine flow and form output dems
2+
#'
3+
#' `flow_mapper()` and `form_mapper()` each provide output information per cell
4+
#' of a dem file. This function takes the fill dem from `flow_mapper()` as well
5+
#' as the length and weti dem files from `form_mapper()` and merges them
6+
#' together into a complete dem file with all information. This file is saved
7+
#' to the project folder.
8+
#'
9+
#' @param folder Character. Folder with previous LITAP runs (i.e. where output
10+
#' of `flow_mapper()` etc. are)
11+
#' @param out_format Character. Output format (rds or csv) that merged file
12+
#' should be saved as (if different from the rest; by default uses the format
13+
#' of the other LITAP output files)
14+
15+
merge_all <- function(folder, out_format = NULL) {
16+
17+
# Get current out format
18+
ext <- get_format(folder, where = "flow")
19+
if(!is.null(out_format)) {
20+
check_out_format(out_format)
21+
ext <- out_format
22+
}
23+
24+
flow <- get_previous(folder, step = "fill", where = "flow") %>%
25+
dplyr::select(-"ridge")
26+
27+
flow_stats <- get_previous(folder, step = "fill", where = "flow", type = "stats")
28+
29+
inv <- get_previous(folder, step = "inverted", where = "flow") %>%
30+
dplyr::select("seqno", "ddir", "drec", "upslope", "upslope_m",
31+
"inv_initial_shed", "inv_local_shed", "edge_map") %>%
32+
dplyr::rename_with(.cols = -c("seqno", dplyr::contains("inv_")),
33+
~paste0("inv_", .))
34+
inv_stats <- get_previous(folder, step = "inverted", where = "flow", type = "stats")
35+
36+
length <- get_previous(folder, step = "length", where = "form")
37+
38+
weti <- get_previous(folder, step = "form", where = "form")
39+
40+
combo <- dplyr::left_join(flow, inv, by = "seqno") %>%
41+
dplyr::left_join(length,
42+
by = c("seqno", "x", "y", "row", "col", "elev")) %>%
43+
dplyr::left_join(weti,
44+
by = c("seqno", "x", "y", "row", "col",
45+
"elev", "drec", "upslope"))
46+
47+
name <- paste0("all_points.", ext)
48+
if(ext == "rds") readr::write_rds(combo, file.path(folder, name))
49+
if(ext == "csv") readr::write_csv(combo, file.path(folder, name), progress = FALSE)
50+
combo
51+
}
52+

R/LITAP_common_docs.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# args ------------------
2-
#' Common arguments for [flow_mapper()] and [form_mapper()]
2+
3+
#' Common arguments for [flow_mapper()], [form_mapper()], [facet_mapper()], a
4+
#' nd [wepp_mapper()]
35
#'
6+
#' @param grid Numeric. Grid size in m of the input DEM file
47
#' @param resume Character. From which stage should the run be resumed? (see
5-
#' Details below)
6-
#' @param end Character. If ending a run after a particular step, which step
7-
#' (see Details below)
88
#' @param clean Logical. Remove all output files from previous runs in this
99
#' folder?
1010
#' @param report Logical. Create html report of results?
1111
#' @param log Logical. Create log file recording progress?
1212
#' @param verbose Logical. Output extra progress messages.
1313
#' @param quiet Logical. Suppress all messages.
14+
#' @param debug Logical. If TRUE, output files contain intermediate columns
15+
#' useful for debugging (e.g., 'buffer', 'seqno_buffer', etc.) Default FALSE.
1416
#'
1517
#' @keywords internal
1618
#' @name args

R/LITAP_load.R

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#' @param clim Vector. Two numbers specifying the start and end of a subset of
2121
#' columns to extract
2222
#' @param edge Logical. Whether to add an edge (buffer) around the data.
23-
#' @param verbose Logical. Output extra progress messages.
23+
#'
24+
#' @inheritParams args
2425
#'
2526
#' @return Returns a data frame containing elevation data in a format suitable
2627
#' for analysis
@@ -65,7 +66,8 @@
6566
#'
6667
#' @export
6768
load_file <- function(file, nrow = NULL, ncol = NULL, missing_value = -9999,
68-
rlim = NULL, clim = NULL, edge = TRUE, verbose = TRUE) {
69+
rlim = NULL, clim = NULL, grid = NULL, edge = TRUE,
70+
verbose = TRUE) {
6971

7072
if(!file.exists(file)) stop("Cannot locate ", file,
7173
" relative to working directory, ", getwd(),
@@ -90,17 +92,17 @@ load_file <- function(file, nrow = NULL, ncol = NULL, missing_value = -9999,
9092
db <- dplyr::arrange(db, dplyr::desc(y), x)
9193
nrow <- length(unique(db$y))
9294
ncol <- length(unique(db$x))
93-
db <- dplyr::select(db, -"x", -"y")
9495
if(verbose) message(" Detected ", nrow, " rows and ", ncol, " columns")
9596
} else if(!is.null(nrow) && !is.null(ncol)) {
9697
if(verbose) message(" Using supplied ", nrow, " rows and ", ncol, " columns")
9798
} else {
98-
stop("dbf files with only one column require nrow and ncol arguments.",
99+
stop("dbf files with only one column require 'nrow' and 'ncol' arguments.",
99100
call. = FALSE)
100101
}
101102

102-
db_format(db, nrow, ncol, missing_value, verbose) %>%
103-
db_prep(clim, rlim, edge, verbose)
103+
db_format(db, nrow = nrow, ncol = ncol, grid = grid,
104+
missing_value = missing_value, verbose = verbose) %>%
105+
db_prep(clim = clim, rlim = rlim, edge = edge, verbose = verbose)
104106
}
105107

106108

@@ -185,21 +187,33 @@ load_raster <- function(file) {
185187
db
186188
}
187189

188-
db_format <- function(db, nrow, ncol, missing_value = -9999, verbose) {
190+
db_format <- function(db, nrow, ncol, grid, missing_value = -9999, verbose) {
189191
if(verbose) message(" Formating grid")
190192
# Check if valid rows/cols
191193
if(nrow * ncol != length(db$elev)){
192194
stop("Number of rows and columns does not match the total number of cells in the data base, Try again!")
193195
}
194196

195197
# Arrange as grid
196-
db %>%
198+
db <- db %>%
197199
dplyr::mutate(seqno = 1:length(elev),
198200
row = sort(rep(1:nrow, length(elev)/nrow)),
199201
col = rep(1:ncol, length(elev)/ncol),
200202
missing = elev == missing_value,
201203
elev = replace(elev, missing, NA_real_)) %>%
202204
dplyr::mutate(dplyr::across(-"missing", as.numeric))
205+
206+
if(any(!c("x", "y") %in% names(db))) {
207+
if(is.null(grid)) stop("No grid dimensions in data, require 'grid' argument",
208+
call. = FALSE)
209+
if(verbose) message(" No x/y in file, creating x/y from cols/rows/grid")
210+
211+
db <- dplyr::mutate(db,
212+
x = col * grid,
213+
y = rev(row) * grid)
214+
}
215+
216+
db
203217
}
204218

205219
db_prep <- function(db, clim, rlim, edge, verbose) {
@@ -356,3 +370,18 @@ format_rule <- function(rule, type, quiet) {
356370
seqno_to_buffer <- function(seqno, seqno_buffer) {
357371
seqno_buffer[seqno]
358372
}
373+
374+
seqno_as_buffer <- function(seqno, db) {
375+
dplyr::mutate(db, seqno2 = seqno) %>%
376+
add_buffer() %>%
377+
dplyr::filter(.data$seqno2 == !!seqno) %>%
378+
dplyr::pull(.data$seqno)
379+
}
380+
381+
seqno_from_buffer <- function(seqno_buffer, db) {
382+
dplyr::mutate(db, seqno2 = seqno) %>%
383+
add_buffer() %>%
384+
dplyr::filter(.data$seqno == !!seqno_buffer) %>%
385+
dplyr::pull(.data$seqno2)
386+
}
387+

R/utils_plot.R renamed to R/LITAP_plot.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#' @param shed Logical. Show watersheds?
1313
#' @param pits Logical. Show watershed pits (lowest point)
1414
#' @param shed_type Character. Which type of watershed, must be included as a
15-
#' column in the data frame. Can be one of 'initial', 'local', 'pond', or
16-
#' 'fill'.
15+
#' column in the data frame. Can be one of 'initial', 'local', 'fill',
16+
#' 'inv_initial', or 'inv_local'/'inverted'.
1717
#' @param upslope_threshold Numeric. If dir = TRUE, only show flow directions
1818
#' for cells with >= this many cells which drain to it.
1919
#' @param cells Vector. Which cells to show
@@ -25,7 +25,7 @@
2525
#'
2626
#' @export
2727
flow_plot <- function(db, type = "relief", dir = FALSE, seqno = FALSE, highlight = FALSE,
28-
shed = FALSE, shed_type = "shedno", pits = FALSE,
28+
shed = FALSE, shed_type = "local", pits = FALSE,
2929
upslope_threshold = NULL,
3030
cells = NULL, clim = NULL, rlim = NULL,
3131
stats = NULL, missing = NA) {
@@ -58,8 +58,10 @@ flow_plot <- function(db, type = "relief", dir = FALSE, seqno = FALSE, highlight
5858
if(shed == TRUE){
5959
if(shed_type == "initial" & "initial_shed" %in% names(db)) db$shedno <- db$initial_shed
6060
if(shed_type == "local" & "local_shed" %in% names(db)) db$shedno <- db$local_shed
61-
if(shed_type == "pond" & "pond_shed" %in% names(db)) db$shedno <- db$pond_shed
61+
#if(shed_type == "pond" & "pond_shed" %in% names(db)) db$shedno <- db$pond_shed
6262
if(shed_type == "fill" & "fill_shed" %in% names(db)) db$shedno <- db$fill_shed
63+
if(shed_type == "inv_initial" & "inv_initial_shed" %in% names(db)) db$shedno <- db$inv_initial_shed
64+
if(shed_type %in% c("inv_local", "inverted") & "inv_local_shed" %in% names(db)) db$shedno <- db$inv_local_shed
6365
}
6466

6567

@@ -98,12 +100,12 @@ flow_plot <- function(db, type = "relief", dir = FALSE, seqno = FALSE, highlight
98100

99101
g <- g +
100102
ggplot2::geom_raster(ggplot2::aes(fill = factor(shedno))) +
101-
ggplot2::scale_fill_discrete(name = "Watershed", guide = FALSE) +
103+
ggplot2::scale_fill_discrete(name = "Watershed", guide = "none") +
102104
ggplot2::geom_text(data = labs, ggplot2::aes(label = shedno))
103105

104106
} else if(type == "relief") {
105107
g <- g + ggplot2::geom_raster(ggplot2::aes(alpha = relief)) +
106-
ggplot2::scale_alpha_continuous(range = c(1, 0), guide = FALSE)
108+
ggplot2::scale_alpha_continuous(range = c(1, 0), guide = "none")
107109
} else if(type == "elevation") {
108110
g <- g + ggplot2::geom_raster(ggplot2::aes(alpha = elev)) +
109111
ggplot2::scale_alpha_continuous(range = c(0, 1))

0 commit comments

Comments
 (0)