forked from mikejohnson51/hydrofab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_to_distribution.R
136 lines (115 loc) · 4.77 KB
/
aggregate_to_distribution.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#' @title Aggregate Network to Uniform Size
#' @description This function aggregates a network to a desired size distribution while
#' enforcing minimum flowpath legnths and catchment areas. Additionally a set of explicit nexus
#' locations can be provided over which the network cannot be aggregated (see poi_to_outlet)
#' @param gpkg a path to a gpkg
#' @param divide If gpkg is NULL, then an sf data.frame, otherwise a the layer name. See details.
#' @param flowpath If gpkg is NULL, then an sf data.frame, otherwise a the layer name. See details.
#' @param outlets data.frame with mandatory "ID" column and optional "POI_ID" column. "ID" must be identifiers from
#' flowpath and divide data.frames and POI ID must be unique.
#' @param ideal_size_sqkm The ideal size of catchments (default = 10 sqkm)
#' @param min_length_km The minimum allowable length of flowpath features (default = 1 km)
#' @param min_area_sqkm The minimum allowable area of catchment features (default = 3 sqkm)
#' @param outfile of not NULL, where to write the output files
#' @param overwrite overwrite existing gf file. Default is FALSE
#' @param nexus_locations a data.frame with columns specifying the ID, and the nexus type.
#' @param log a filepath to write messages to or Boolean (TRUE = print to console; FALSE = no messages)
#' @param verbose print status updates. Default = TRUE
#' @return if outfile = TRUE, a file path, else a list object
#' @details If gpkg is not NULL, divide and flowpath can be left NULL as well. The code attempts to
#' infer the correct layers. The divides layer will be the one including the word "divide" or "catchment" and the
#' flowpath layer will be the one including 'flowpath' or 'flowline'. If no layers, or more then one layer are deemed possible
#' for each input, then the function will stop and ask for explicit names.
#' @export
#' @importFrom sf st_transform read_sf st_set_crs write_sf st_layers
#' @importFrom dplyr left_join filter
#' @importFrom nhdplusTools get_sorted calculate_total_drainage_area get_streamorder
#' @importFrom logger log_appender appender_file appender_console
aggregate_to_distribution = function(gpkg = NULL,
flowpath = NULL,
divide = NULL,
outlets = NULL,
ideal_size_sqkm = 10,
min_length_km = 1,
min_area_sqkm = 3,
outfile = NULL,
log = TRUE,
overwrite = FALSE,
cache = TRUE,
verbose = TRUE) {
if (cache & is.null(outfile)) {
stop("cache cannot be written if outfile is NULL")
}
if (cache) {
cache_file = outfile
} else {
cache_file = NULL
}
if (!is.logical(log)) {
log_appender(appender_file(log))
verbose = TRUE
} else {
log_appender(appender_console)
verbose = log
}
if (!is.null(outfile)) {
if (file.exists(outfile) & overwrite) {
unlink(outfile)
} else if (file.exists(outfile)) {
hyaggregate_log("WARN",
glue("{outfile} already exists and overwrite is FALSE"),
verbose)
return(outfile)
}
}
network_list = read_hydrofabric(gpkg,
catchments = divide,
flowpaths = flowpath,
crs = 5070)
network_list = add_network_type(network_list)
# Add outlets
if (!is.null(outlets)) {
network_list$flowpaths = left_join(network_list$flowpaths,
outlets,
by = 'id')
} else {
network_list$flowpaths$poi_id = NA
}
network_list <- prepare_network(network_list)
if (cache) {
tmp = list()
tmp$base_catchments = network_list$catchments
tmp$base_flowpaths = network_list$flowpaths
write_hydrofabric(tmp,
cache_file,
verbose,
enforce_dm = FALSE)
rm(tmp)
}
network_list = aggregate_along_mainstems(
network_list,
ideal_size_sqkm,
min_area_sqkm,
min_length_km,
verbose = verbose,
cache_file = cache_file
)
network_list = collapse_headwaters2(
network_list,
min_area_sqkm,
min_length_km,
verbose = verbose,
cache_file = cache_file
)
network_list = add_mapped_pois(network_list, refactored_gpkg = gpkg, verbose = verbose)
if (!is.null(outfile)) {
outfile = write_hydrofabric(
network_list,
outfile,
verbose = verbose,
enforce_dm = FALSE)
return(outfile)
} else {
tmp
}
}