├── .Rbuildignore ├── .gitignore ├── CONTRIBUTING.md ├── CRAN-RELEASE ├── DESCRIPTION ├── LICENSE ├── LICENSE_full ├── NAMESPACE ├── NEWS.md ├── R ├── calc_attributes_edges.R ├── calc_attributes_sites_approx.R ├── calc_attributes_sites_exact.R ├── calc_binary.R ├── calc_edges.R ├── calc_prediction_sites.R ├── calc_sites.R ├── check_compl_confluences.R ├── check_ssn.R ├── correct_compl_confluences.R ├── delete_lakes.R ├── derive_streams.R ├── export_ssn.R ├── globals.R ├── import_data.R ├── merge_sites_measurements.R ├── openSTARS.R ├── restrict_network.R └── setup_grass_environment.R ├── README.Rmd ├── README.md ├── README_files ├── compl_junction1.PNG ├── compl_junction2.PNG ├── figure-html │ ├── plot_compl_confl-1.png │ ├── plot_compl_junctions-1.png │ ├── plot_data1-1.png │ ├── plot_data1.1-1.png │ ├── plot_data1.2-1.png │ ├── plot_data2-1.png │ ├── plot_data3-1.png │ ├── plot_data4-1.png │ ├── ssn_test-1.png │ └── ssn_test-2.png └── figure-markdown_github │ ├── plot_compl_confl-1.png │ ├── plot_data1.1-1.png │ ├── plot_data1.2-1.png │ ├── plot_data2-1.png │ ├── plot_data3-1.png │ ├── plot_data4-1.png │ ├── ssn_test-1.png │ └── ssn_test-2.png ├── cran-comments.md ├── inst └── extdata │ └── nc │ ├── elev_ned_30m.tif │ ├── elev_ned_30m.tif.aux.xml │ ├── geology.dbf │ ├── geology.prj │ ├── geology.shp │ ├── geology.shx │ ├── lakes.cpg │ ├── lakes.dbf │ ├── lakes.prj │ ├── lakes.qpj │ ├── lakes.shp │ ├── lakes.shx │ ├── landuse_r.tif │ ├── obs_data.csv │ ├── pointsources.dbf │ ├── pointsources.prj │ ├── pointsources.qpj │ ├── pointsources.shp │ ├── pointsources.shx │ ├── sites_nc.dbf │ ├── sites_nc.prj │ ├── sites_nc.shp │ ├── sites_nc.shx │ ├── streams.dbf │ ├── streams.prj │ ├── streams.shp │ └── streams.shx ├── man ├── assign_binIDs.Rd ├── calcCatchmArea_assignNetID.Rd ├── calc_attributes_edges.Rd ├── calc_attributes_sites_approx.Rd ├── calc_attributes_sites_exact.Rd ├── calc_binary.Rd ├── calc_catchment_attributes_rast.Rd ├── calc_catchment_attributes_rast_rec.Rd ├── calc_catchment_attributes_vect.Rd ├── calc_catchment_attributes_vect_rec.Rd ├── calc_edges.Rd ├── calc_offset.Rd ├── calc_outdist.Rd ├── calc_prediction_sites.Rd ├── calc_sites.Rd ├── check_compl_confluences.Rd ├── check_projection.Rd ├── check_ssn.Rd ├── correct_compl_confluences.Rd ├── delete_lakes.Rd ├── derive_streams.Rd ├── export_ssn.Rd ├── get_all_raster_values.Rd ├── get_cats_edges_in_catchment.Rd ├── get_n_val_raster.Rd ├── get_streams_edges_in_catchment.Rd ├── grass_v.to.db.Rd ├── import_data.Rd ├── import_vector_data.Rd ├── merge_sites_measurements.Rd ├── openSTARS.Rd ├── prepare_sites.Rd ├── restrict_network.Rd ├── setup_grass_environment.Rd └── watershed_memory.Rd └── vignettes └── Warnings_and_Errors.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | # RStudio 2 | ^.*\.Rproj$ 3 | ^\.Rproj\.user$ 4 | openSTARS.Rproj 5 | LICENSE_full 6 | 7 | # Git 8 | .gitignore 9 | # Travis 10 | .travis.yml 11 | # appveyor 12 | ^appveyor\.yml$ 13 | travis-tool.sh.cmd 14 | # READMES 15 | README.html 16 | README.Rmd 17 | README.md 18 | README_files 19 | CONTRIBUTING.md 20 | # CRAN-COMMENTS 21 | cran-comments.md 22 | ^NEWS\.md$ 23 | # Release announcement 24 | release_files 25 | ^CRAN-RELEASE$ 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | 6 | openSTARS.Rproj 7 | README.html 8 | inst/doc 9 | junk 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # CONTRIBUTING # 2 | 3 | ### Please contribute! 4 | We love collaboration. 5 | 6 | ### Found a Bug? 7 | 8 | * Submit an issue on our Issues page [here](https://github.com/MiKatt/openSTARS/issues). 9 | 10 | ### Code contributions? 11 | 12 | * The dev branch holds the newest version but also including untested changes 13 | * **Fork** this repo to your Github account. 14 | * **Clone** your version on your account down to your machine from your account, e.g,. `git clone https://github.com//openSTARS.git`. 15 | * Make sure to **track upstream** progress (i.e., on our version of `openSTARS` at `MiKatt/openSTARS`) by doing `git remote add upstream https://github.com/MiKatt/openSTARS.git`. Before making changes make sure to pull changes in from upstream by doing either `git fetch upstream` then merge later or `git pull upstream` to fetch and merge in one step 16 | * Make your **changes** (bonus points for making changes on a new branch). 17 | * **Push** up to your account. 18 | * Submit a **pull request** to home base at `MiKatt/openSTARS`. 19 | 20 | ### Code guidelines 21 | 22 | We do not have strong guideline for our contributions and are happy to help. 23 | 24 | 1. Please follow [this](http://adv-r.had.co.nz/Style.html) styleguide for your contributions. 25 | 26 | 27 | ### Questions? 28 | 29 | Get in touch: [Mira Kattwinkel](mailto:mira.kattwinkel@gmx.net) 30 | 31 | ### Thanks for contributing! 32 | -------------------------------------------------------------------------------- /CRAN-RELEASE: -------------------------------------------------------------------------------- 1 | This package was submitted to CRAN on 2022-02-04. 2 | Once it is accepted, delete this file and tag the release (commit daa8cfd). 3 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: openSTARS 2 | Type: Package 3 | Title: An Open Source Implementation of the 'ArcGIS' Toolbox 'STARS' 4 | Description: An open source implementation of the 'STARS' toolbox 5 | (Peterson & Ver Hoef, 2014, ) using 'R' and 'GRASS GIS'. 6 | It prepares the *.ssn object needed for the 'SSN' package. 7 | A Digital Elevation Model (DEM) is used to derive stream networks 8 | (in contrast to 'STARS' that can clean an existing stream network). 9 | Version: 1.2.3 10 | Authors@R: c(person("Mira", "Kattwinkel", role = c("aut","cre"), 11 | email = "mira.kattwinkel@gmx.net"), 12 | person("Eduard", "Szöcs", role = c("aut"), 13 | email = "eduardszoecs@gmail.com")) 14 | URL: https://github.com/MiKatt/openSTARS 15 | Depends: 16 | R (>= 3.3), 17 | data.table, 18 | rgrass7 19 | License: MIT + file LICENSE 20 | Encoding: UTF-8 21 | LazyLoad: true 22 | Imports: 23 | methods, 24 | progress, 25 | rgdal, 26 | sp, 27 | raster, 28 | SSN 29 | RoxygenNote: 7.1.2 30 | Suggests: knitr, 31 | rmarkdown 32 | VignetteBuilder: knitr 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2016 2 | COPYRIGHT HOLDER: Eduard Szöcs -------------------------------------------------------------------------------- /LICENSE_full: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Eduard Szöcs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(calc_attributes_edges) 4 | export(calc_attributes_sites_approx) 5 | export(calc_attributes_sites_exact) 6 | export(calc_binary) 7 | export(calc_edges) 8 | export(calc_prediction_sites) 9 | export(calc_sites) 10 | export(check_compl_confluences) 11 | export(check_projection) 12 | export(check_ssn) 13 | export(correct_compl_confluences) 14 | export(delete_lakes) 15 | export(derive_streams) 16 | export(export_ssn) 17 | export(grass_v.to.db) 18 | export(import_data) 19 | export(merge_sites_measurements) 20 | export(restrict_network) 21 | export(setup_grass_environment) 22 | import(SSN) 23 | import(data.table) 24 | import(progress) 25 | import(rgrass7) 26 | import(sp) 27 | importFrom(methods,as) 28 | importFrom(rgdal,readOGR) 29 | importFrom(utils,write.table) 30 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # openSTARS 1.2.3 2 | 3 | * fixed issue #18 on GitHub: in correct_complex_confluences, set threshold for breaking (v.edit) to 4 | cellsize/(12 *2) instead of 1 to prevent additional cutting of the other inflows 5 | * fixed issue #25 concerning many complex confluences to be corrected 6 | 7 | # openSTARS 1.2.2 8 | 9 | * release on CRAN 10 | * typo corrections etc. 11 | 12 | # openSTARS 1.2.1 13 | 14 | * catch error arising in GRASS 7.8 due to changes in v.to.db 15 | * update setup_grass_environment to use 'dem' as SG in initGRASS 16 | * update examples to GRASS 7.8 17 | * several small changes and bug fixes 18 | * update of setup_grass_environment 19 | 20 | # openSTARS 1.2.0 21 | 22 | * Renamed check_compl_junctions and correct_compl_junctions to 23 | check_compl_confluences and correct_compl_confluences, respectively, 24 | to be in line with the STARS publications. 25 | * correct_compl_confluences now works for up to seven inflows to one outflow 26 | * Corrected an error in correct_compl_confluences: If there were outflows from a 27 | complex junction that were also inflows to complex confluences a wrong assignment 28 | might have occurred. 29 | * Updated all lengths in correct_complex_confluences 30 | (segment length, cum_length, out_dist) 31 | * correct_complex_confluences now works correctly for non-sqared cells (use max 32 | cell dimension instead of min for calculation of flow directions at confluences) 33 | * New function to delete lakes from the network ('delete_lakes'). Added "lakes.shp" 34 | to example data. 35 | * New function to restrict edges to certain networks based on sites (observation 36 | or prediction) or given netIDs and delete the rest ('restrict_network'). 37 | * New functionality in calc_attributes_edges and calc_attributes_sites_exact: 38 | It is now possible to calculate percentages from raster maps with multiple values 39 | (e.g. land cover classes). 40 | * New function to merge an external table with (repeated) measurements to the sites 41 | (merge_sites_measurements). 42 | * Sites has now the column 'str_edge' (for 'stream') instead of 'cat_edge' 43 | to relate the site to its edge. 44 | * Several smaller fixes. 45 | * Delete stream segments with zero length in derive_streams 46 | 47 | 48 | # openSTARS 1.1.0 49 | 50 | * Fixed error when loading (import_data) example shapes: no empty cell allowed 51 | in attribute table (used to work when checking). 52 | * Bug fix in correct_compl_junctions(): if the cut outflow was also the inflow 53 | to another complex junction an error occurred like 54 | "Error in `[<-.data.table`(`*tmp*`, ii, jj, value = c(12013, 12019)) : NA in j". 55 | * Set round rcaArea and H2OArea to 6 digits instead of 4 in calc_edges() to be 56 | able to retrieve squared meters from the squared km. 57 | * Updated calc_attributes_edges() for vector input data; it is now possible to 58 | calculate percentage of a certain attribute within a catchment (polygons) or 59 | count the number of features in a catchment (points). 60 | * Changed setup_grass_environment: Extent and projection are set to the one of the 61 | DEM (compared to setting the projections according to the sites object, epsg 62 | code or proj4 string as it was done before). The reason for this change is 63 | that rather vector files should be projected than raster files (i.e. sites and 64 | streams instead of DEM) and that the extent of the DEM should be larger than 65 | that of the sites. 66 | * Change import_data: Now vector files are reprojected to match the projection 67 | of the DEM (i.e. the one of the current location). 68 | * Updated calc_attributes_sites_exact() for vector input data. It is now possible 69 | to calculate percentages of a certain attribute class in a catchment (polygons) 70 | or count the numbers of features in a catchment (points) 71 | 72 | # openSTARS 1.0.0 73 | 74 | * First CRAN release Aug. 2017 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /R/calc_attributes_sites_approx.R: -------------------------------------------------------------------------------- 1 | #' Calculate attributes of the sites. 2 | #' 3 | #' For each site (observations or predictions) attributes (potential predictor variables) 4 | #' are derived based on the values calculated for the edge the site lies on. 5 | #' This function calculates approximate values for site catchments as described 6 | #' in Peterson & Ver Hoef, 2014: STARS: An ArcGIS Toolset Used to Calculate the 7 | #' Spatial Information Needed to Fit Spatial Statistical Models to Stream 8 | #' Network Data. J. Stat. Softw., 56 (2). 9 | #' 10 | #' @param sites_map character; name of the sites (observation or prediction) 11 | #' attributes shall be calculated for. "sites" (default) refers to the observation sites. 12 | #' @param input_attr_name character vector; input column name(s) in the 13 | #' attribute table of the vector map "edges". 14 | #' @param output_attr_name character vector (optional); output column name(s) 15 | #' appended to the site attribute data table. If not provided it is set to 16 | #' \code{input_attr_name}. Attribute names must not be longer than 10 17 | #' characters. 18 | #' @param stat name or character vector giving the statistics to be calculated. 19 | #' See details below. 20 | #' @param round_dig integer; number of digits to round results to. 21 | #' @param calc_basin_area boolean; shall the catchment area be calculated? (Useful 22 | #' to set to FALSE if the function has been called before.) 23 | #' @param overwrite boolean; shall existing columns be overwritten; defaults to FALSE 24 | #' 25 | #' @return Nothing. The function appends new columns to the \code{sites_map} 26 | #' attribute table 27 | #' \itemize{ 28 | #' \item{'H2OAreaA':} {Total watershed area of the watershed upstream of each site.} 29 | #' \item{attr_name:} {Additional optional attributes calculated based on \code{input_attr_name}.} 30 | #' } 31 | #' 32 | #' @details The approximate total catchment area (H2OAreaA) is always calculated 33 | #' if \code{calc_basin_area} is TRUE. If \code{stat} is one of 34 | #' "min", "max", "mean" or "percent" the function assigns the value of the edge the site lies on. 35 | #' Otherwise, the value is calculated as the sum of all edges upstream of the previous 36 | #' confluence and the proportional value of the edge the site lies on (based on 37 | #' the distance ratio 'ratio'); this is useful e.g. for counts of dams or waste water 38 | #' treatment plants or total catchment area. 39 | #' 40 | #' \code{input_attr_name} must give the column names of the edges attribute table 41 | #' for that the statistics should be calculated. 42 | #' 43 | #' @note \code{\link{import_data}}, \code{\link{derive_streams}}, 44 | #' \code{\link{calc_edges}}, \code{\link{calc_sites}} or 45 | #' \code{\link{calc_prediction_sites}} and \code{\link{calc_attributes_edges}} 46 | #' must be run before. 47 | #' 48 | #' @author Mira Kattwinkel, \email{mira.kattwinkel@@gmx.net} 49 | #' @export 50 | #' 51 | #' @examples 52 | #' \donttest{ 53 | #' # Initiate and setup GRASS 54 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 55 | #' if(.Platform$OS.type == "windows"){ 56 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 57 | #' } else { 58 | #' grass_program_path = "/usr/lib/grass78/" 59 | #' } 60 | #' 61 | #' setup_grass_environment(dem = dem_path, 62 | #' gisBase = grass_program_path, 63 | #' remove_GISRC = TRUE, 64 | #' override = TRUE 65 | #' ) 66 | #' gmeta() 67 | #' 68 | #' # Load files into GRASS 69 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 70 | #' preds_v_path <- system.file("extdata", "nc", "geology.shp", package = "openSTARS") 71 | #' import_data(dem = dem_path, sites = sites_path, predictor_vector = preds_v_path,) 72 | #' 73 | #' # Derive streams from DEM 74 | #' derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 75 | #' 76 | #' # Check and correct complex confluences (there are no complex confluences in this 77 | #' # example date set; set accum_threshold in derive_streams to a smaller value 78 | #' # to create complex confluences) 79 | #' cj <- check_compl_confluences() 80 | #' if(cj){ 81 | #' correct_compl_confluences() 82 | #' } 83 | #' 84 | #' # Prepare edges 85 | #' calc_edges() 86 | #' 87 | #' # Derive slope from the DEM as an example raster map to calculate attributes from 88 | #' execGRASS("r.slope.aspect", flags = c("overwrite","quiet"), 89 | #' parameters = list( 90 | #' elevation = "dem", 91 | #' slope = "slope" 92 | #' )) 93 | #' calc_attributes_edges(input_raster = "slope", stat_rast = "max", attr_name_rast = "maxSlo", 94 | #' input_vector = "geology", stat_vect = "percent", attr_name_vect = "GEO_NAME") 95 | #' 96 | #' calc_sites() 97 | #' 98 | #' # approximate potential predictor variables for each site based on edge values 99 | #' calc_attributes_sites_approx( 100 | #' input_attr_name = c('maxSlo', 'CZamp', 'CZbgp', 'CZfgp', 'CZgp', 'CZigp', 'CZlgp', 'CZvep', 'Kmp'), 101 | #' stat = c("max", rep("percent", 8))) 102 | #' 103 | #' # plot share of a certain geology in the sampling point's catchment as 104 | #' # point size 105 | #' library(sp) 106 | #' edges <- readVECT('edges', ignore.stderr = TRUE) 107 | #' sites <- readVECT('sites', ignore.stderr = TRUE) 108 | #' geo <- readVECT("geology", ignore.stderr = TRUE) 109 | #' plot(geo, col = adjustcolor(1:8, alpha.f = 0.5)[as.factor(geo$GEO_NAME)]) 110 | #' plot(edges, col = "blue", add = TRUE) 111 | #' plot(sites, col = 1, add = TRUE, pch = 19, cex = (sites$CZbgp + 0.15) * 2) 112 | #' legend("left", col = adjustcolor(1:8, alpha.f = 0.5), bty = "n", 113 | #' legend = unique(geo$GEO_NAME), pch = 15, title = "geology") 114 | #' legend("right", col = 1, pch = 19, legend = seq(0, 1, 0.2), bty = "n", 115 | #' title = "share CZbg\nin catchment", pt.cex = (seq(0, 1, 0.2) + 0.15) * 2) 116 | #'} 117 | #' 118 | calc_attributes_sites_approx <- function(sites_map = "sites", 119 | input_attr_name = NULL, 120 | output_attr_name = NULL, 121 | stat = NULL, 122 | round_dig = 2, 123 | calc_basin_area = TRUE, 124 | overwrite = FALSE){ 125 | cnames <- execGRASS("db.columns", 126 | parameters = list( 127 | table = sites_map), 128 | intern = TRUE) 129 | 130 | if(is.null(output_attr_name)) 131 | output_attr_name <- input_attr_name 132 | if(calc_basin_area == TRUE){ 133 | output_attr_name <- c("H2OAreaA", output_attr_name) 134 | input_attr_name <- c("H2OArea", input_attr_name) 135 | stat <- c("totalArea", stat) 136 | 137 | if("H2OAreaA" %in% cnames){ 138 | execGRASS("v.db.dropcolumn", flags = "quiet", 139 | parameters = list( 140 | map = sites_map, 141 | columns = "H2OAreaA" 142 | )) 143 | } 144 | } 145 | if(calc_basin_area == FALSE & is.null(input_attr_name)){ 146 | stop("Either input attribute name(s) must be given or calc_basin_area must be TRUE.") 147 | } 148 | 149 | if(any(output_attr_name %in% cnames) & overwrite == FALSE){ 150 | stop(paste0("Column(s) ", paste0(output_attr_name[which(output_attr_name %in% cnames)], collapse = ", "), " already exist; use overwrite = TRUE to overwrite them.")) 151 | } 152 | 153 | if(any(output_attr_name %in% cnames) & overwrite == TRUE){ 154 | ii <- which(output_attr_name %in% cnames) 155 | jj <- which(output_attr_name[ii] == "H2OAreaA") 156 | if(length(jj) > 0) 157 | ii <- ii[-jj] 158 | for(i in ii){ 159 | execGRASS("v.db.dropcolumn", flags = "quiet", 160 | parameters = list( 161 | map = sites_map, 162 | columns = output_attr_name[i] 163 | )) 164 | } 165 | } 166 | 167 | if(length(input_attr_name) != length(output_attr_name)) 168 | stop("There must be the same number of input and output attribute names.") 169 | 170 | if(any(nchar(output_attr_name)) > 10) 171 | stop("Attribute names must not be longer than ten characters.") 172 | 173 | if(length(round_dig) == 1) 174 | round_dig <- rep(round_dig, length(output_attr_name)) 175 | 176 | if(length(round_dig) < length(output_attr_name)) 177 | round_dig <- c(max(round_dig), round_dig) 178 | 179 | if(length(unique(c(length(input_attr_name), length(stat),length(output_attr_name)))) > 1) 180 | stop(paste0("There must be the same number of input attribute names (",length(input_attr_name), "), 181 | output attribute names (", length(output_attr_name), ") and 182 | statistics to calculate (", length(stat),").")) 183 | 184 | cnames <- execGRASS("db.columns", 185 | parameters = list( 186 | table = sites_map), 187 | intern = TRUE) 188 | 189 | execGRASS("v.db.addcolumn", 190 | flags = c("quiet"), 191 | parameters = list( 192 | map = sites_map, 193 | columns = paste0(output_attr_name," double precision", collapse = ", ") 194 | )) 195 | 196 | for(i in seq_along(input_attr_name)){ 197 | if(stat[i] %in% c("min", "max", "mean", "percent")){ 198 | execGRASS("db.execute", 199 | parameters = list( 200 | sql = paste0("UPDATE ", sites_map," SET ", output_attr_name[i], "=", 201 | "(SELECT ", paste0(input_attr_name[i],"_c"), 202 | " FROM edges WHERE edges.stream=", sites_map,".str_edge)") 203 | )) 204 | } else { 205 | # calculate site attribute as attribute of the two previous edges + 206 | # (1-ratio) * contribution of edge to total edge attribute 207 | # for H2O Area or e.g. for total numbers (no of WWTP per catchment) 208 | # e.g. calculated with stat = "sum" in calc_attributes_edges 209 | stream_prev1 <- paste0("(SELECT prev_str01 FROM edges WHERE edges.stream=",sites_map,".str_edge)") 210 | stream_prev2 <- paste0("(SELECT prev_str02 FROM edges WHERE edges.stream=",sites_map,".str_edge)") 211 | if(input_attr_name[i] == "H2OArea"){ 212 | sql_str <-paste0("UPDATE ", sites_map," SET ",output_attr_name[i], 213 | " = ROUND(((1-ratio)*", 214 | "(SELECT rcaArea FROM edges WHERE ", sites_map,".str_edge = edges.stream) +", 215 | "(SELECT H2OArea FROM edges WHERE edges.stream=",stream_prev1,") +", 216 | "(SELECT H2OArea FROM edges WHERE edges.stream=",stream_prev2,")),",round_dig[i],")") 217 | } else { 218 | sql_str <-paste0("UPDATE ", sites_map," SET ",output_attr_name[i], 219 | " = ROUND(((1-ratio)*", 220 | "(SELECT ", paste0(input_attr_name[i],"_e"), " FROM edges WHERE ", sites_map,".str_edge = edges.stream) +", 221 | "(SELECT ", paste0(input_attr_name[i],"_c"), " FROM edges WHERE edges.stream=",stream_prev1,") +", 222 | "(SELECT ", paste0(input_attr_name[i],"_c"), " FROM edges WHERE edges.stream=",stream_prev2,")),",round_dig[i],")") 223 | } 224 | execGRASS("db.execute", 225 | parameters = list( 226 | sql = sql_str 227 | )) 228 | # correct for those segments that do not have previous streams 229 | if(input_attr_name[i] == "H2OArea"){ 230 | sql_str <- paste0("UPDATE ", sites_map," SET ",output_attr_name[i], 231 | " = (1-ratio)*(SELECT rcaArea FROM edges WHERE ", 232 | sites_map,".str_edge = edges.stream) WHERE str_edge IN ", 233 | "(SELECT stream FROM edges WHERE prev_str01=0)") 234 | } else { 235 | sql_str <- paste0("UPDATE ", sites_map," SET ",output_attr_name[i], 236 | " = (1-ratio)*(SELECT ", paste0(input_attr_name[i],"_e"), 237 | " FROM edges WHERE ", sites_map,".str_edge = edges.stream) WHERE str_edge IN ", 238 | "(SELECT stream FROM edges WHERE prev_str01=0)") 239 | } 240 | execGRASS("db.execute", 241 | parameters = list( 242 | sql = sql_str 243 | )) 244 | # ROUND does not work with WHERE ... IN ... 245 | execGRASS("db.execute", 246 | parameters = list( 247 | sql = paste0("UPDATE ",sites_map, " SET ",output_attr_name[i], 248 | "= ROUND(",output_attr_name[i],",",round_dig[i],")") 249 | )) 250 | } 251 | } 252 | cnames2 <- execGRASS("db.columns", flags = "quiet", 253 | parameters = list( 254 | table = sites_map 255 | ), intern = TRUE) 256 | cnames2 <- cnames2[-(which(cnames2 %in% cnames))] 257 | #message(writeLines(strwrap(paste0("\nNew attributes values are stored as ", paste("'", cnames2, "'", sep = "", collapse = ", "), " in 'sites'."), 258 | # width = 80))) 259 | message(paste0("\nNew attributes values are stored as ", paste("'", cnames2, "'", sep = "", collapse = ", "), " in 'sites'.")) 260 | 261 | } 262 | -------------------------------------------------------------------------------- /R/calc_binary.R: -------------------------------------------------------------------------------- 1 | #' Calculate binary IDs for each stream network. 2 | #' 3 | #' Calculate binary IDs for each stream network built up by '0' and '1'. 4 | #' This function is called by \code{\link{export_ssn}} and there is no need for it 5 | #' be called by the users. 6 | #' 7 | #' @import data.table 8 | #' 9 | #' @return A list with one slot for each network id containing a data frame 10 | #' with 'rid' and 'binaryID' for each segment belonging to this network. 11 | #' 12 | #' @note \code{\link{import_data}}, \code{\link{derive_streams}}, 13 | #' \code{\link{calc_edges}} and \code{\link{calc_sites}} must be run before. 14 | #' 15 | #' @author Eduard Szoecs, \email{eduardszoecs@@gmail.com}; Mira Kattwinkel, \email{mira.kattwinkel@@gmx.net} 16 | #' @export 17 | #' 18 | #' @examples 19 | #' \donttest{ 20 | #' # Initiate and setup GRASS 21 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 22 | #' if(.Platform$OS.type == "windows"){ 23 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 24 | #' } else { 25 | #' grass_program_path = "/usr/lib/grass78/" 26 | #' } 27 | #' 28 | #' setup_grass_environment(dem = dem_path, 29 | #' gisBase = grass_program_path, 30 | #' remove_GISRC = TRUE, 31 | #' override = TRUE 32 | #' ) 33 | #' gmeta() 34 | #' 35 | #' # Load files into GRASS 36 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 37 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 38 | #' streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 39 | #' preds_v_path <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS") 40 | #' preds_r_path <- system.file("extdata", "nc", "landuse_r.tif", package = "openSTARS") 41 | #' 42 | #' import_data(dem = dem_path, sites = sites_path, streams = streams_path, 43 | #' predictor_vector = preds_v_path, predictor_raster = preds_r_path) 44 | #' 45 | #' # Derive streams from DEM 46 | #' derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 47 | #' 48 | #' # Check and correct complex confluences (there are no complex confluences in this 49 | #' # example date set; set accum_threshold in derive_streams to a smaller value 50 | #' # to create complex confluences) 51 | #' cj <- check_compl_confluences() 52 | #' if(cj){ 53 | #' correct_compl_confluences() 54 | #' } 55 | #' 56 | #' # Prepare edges 57 | #' calc_edges() 58 | #' 59 | #' # Prepare site 60 | #' calc_sites() 61 | #' 62 | #' binaries <- calc_binary() 63 | #' head(binaries[[1]]) 64 | #' } 65 | 66 | calc_binary <- function(){ 67 | vect <- execGRASS("g.list", 68 | parameters = list( 69 | type = 'vect' 70 | ), 71 | intern = TRUE) 72 | if (!'sites_o' %in% vect) 73 | stop('Sites not found. Did you run import_data()?') 74 | if (!'edges' %in% vect) 75 | stop('edges not found. Did you run calc_edges()?') 76 | if (!'sites' %in% vect) 77 | stop('sites not found. Did you run calc_sites()?') 78 | 79 | dt.streams<-execGRASS('db.select', 80 | flags = 'c', 81 | parameters = list( 82 | sql = 'select rid,stream,next_str,prev_str01,prev_str02,netID from edges', 83 | separator = ',' 84 | ), 85 | ignore.stderr = TRUE, 86 | intern = TRUE) 87 | 88 | dt.streams<-do.call(rbind,strsplit(dt.streams,split=",")) 89 | dt.streams<-apply(dt.streams,2,as.numeric) 90 | colnames(dt.streams)<-c("rid","stream","next_str","prev_str01","prev_str02","netID") 91 | dt.streams <- data.frame(dt.streams) 92 | setDT(dt.streams) 93 | dt.streams[, binaryID := "0"] 94 | outlets <- dt.streams[next_str == -1, stream] 95 | 96 | for(i in outlets){ 97 | assign_binIDs(dt = dt.streams, id=i, 1, NULL) 98 | } 99 | 100 | bins<-lapply(outlets, function(x) dt.streams[netID == dt.streams[stream == x, netID], list(rid,binaryID)]) 101 | names(bins)<- dt.streams[stream %in% outlets, netID] 102 | return(bins) 103 | } 104 | 105 | #' assign_binIDs 106 | #' Recursive function to assign binary id to stream segments 107 | #' 108 | #' Should be run for all outlets in the network ( = most downstream segments) and fills the binID for all segments 109 | #' @param id: stream segment 110 | #' @param binID: binary ID 111 | #' @param lastbit: last char to be added (0 or 1) 112 | #' @keywords internal 113 | #' 114 | assign_binIDs <- function(dt, id, binID, lastbit){ 115 | if(dt[stream == id, prev_str01 ] == 0){ # check only one of prev01 and prev02 because they are always both 0 116 | dt[stream == id, binaryID := paste0(binID, lastbit)] 117 | } else { 118 | dt[stream == id, binaryID := paste0(binID,lastbit)] 119 | assign_binIDs(dt, dt[stream == id, prev_str01], dt[stream == id, binaryID], 0) 120 | assign_binIDs(dt, dt[stream == id, prev_str02], dt[stream == id, binaryID], 1) 121 | } 122 | } 123 | 124 | -------------------------------------------------------------------------------- /R/calc_prediction_sites.R: -------------------------------------------------------------------------------- 1 | #'Calculate prediction sites for 'SSN' object. 2 | #' 3 | #'@description A vector (points) map of prediction sites is created and several 4 | #'attributes are assigned. 5 | #' 6 | #'@param predictions string giving the name for the prediction sites map. 7 | #'@param dist number giving the distance between the points to create in map 8 | #' units. 9 | #'@param nsites integer giving the approximate number of sites to create 10 | #'@param netIDs integer (optional): create prediction sites only on streams with 11 | #' these netID(s). 12 | #' 13 | #'@details Either \code{dist} or \code{nsites} must be provided. If \code{dist} 14 | #'is NULL, it is estimated by dividing the total stream length in the map by 15 | #'\code{nsites}; the number of sites actually derived might therefore be a bit 16 | #'smaller than \code{nsites}. 17 | #' 18 | #'Steps include: 19 | #'\itemize{ 20 | #'\item{Place points on edges with given distance from each other} 21 | #'\item{Save the point coordinates in NEAR_X and NEAR_Y.} 22 | #'\item{Assign unique identifiers (needed by the 'SSN' package) 'pid' 23 | #'and 'locID'.} 24 | #'\item{Get 'rid' and 'netID' of the stream segment the site 25 | #'intersects with (from map 'edges').} 26 | #'\item{Calculate upstream distance for 27 | #'each point ('upDist').} 28 | #'\item{Calculate distance ratio ('distRatio') between 29 | #'position of the site on the edge (= distance traveled from lower end of the 30 | #'edge to the site) and the total length of the edge.} } 31 | #' 32 | #''pid' and 'locID' are identical, unique numbers. 'upDist' is calculated using 33 | #'\href{https://grass.osgeo.org/grass78/manuals/addons/r.stream.distance.html}{r.stream.distance}. 34 | #'Points are created using 35 | #'\href{https://grass.osgeo.org/grass78/manuals/v.segment.html}{v.segment}. 36 | #' 37 | #'@note \code{\link{import_data}}, \code{\link{derive_streams}} and 38 | #' \code{\link{calc_edges}} must be run before. 39 | #' 40 | #'@author Mira Kattwinkel \email{mira.kattwinkel@@gmx.net} 41 | #'@export 42 | #' 43 | #' @examples 44 | #' \donttest{ 45 | #' # Initiate and setup GRASS 46 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 47 | #' if(.Platform$OS.type == "windows"){ 48 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 49 | #' } else { 50 | #' grass_program_path = "/usr/lib/grass78/" 51 | #' } 52 | #' 53 | #' setup_grass_environment(dem = dem_path, 54 | #' gisBase = grass_program_path, 55 | #' remove_GISRC = TRUE, 56 | #' override = TRUE 57 | #' ) 58 | #' gmeta() 59 | #' 60 | #' # Load files into GRASS 61 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 62 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 63 | #' import_data(dem = dem_path, sites = sites_path) 64 | #' 65 | #' # Derive streams from DEM 66 | #' derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 67 | #' 68 | #' check_compl_confluences() 69 | #' calc_edges() 70 | #' calc_sites() 71 | #' calc_prediction_sites(predictions = "preds", dist = 2500) 72 | #' 73 | #' library(sp) 74 | #' dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 75 | #' sites <- readVECT('sites', ignore.stderr = TRUE) 76 | #' preds <- readVECT('preds', ignore.stderr = TRUE) 77 | #' edges <- readVECT('edges', ignore.stderr = TRUE) 78 | #' plot(dem, col = terrain.colors(20)) 79 | #' lines(edges, col = 'blue', lwd = 2) 80 | #' points(sites, pch = 4) 81 | #' points(preds, pch = 19, col = "darkred") 82 | #' } 83 | 84 | calc_prediction_sites <- function(predictions, dist = NULL, nsites = 10, 85 | netIDs = NULL) { 86 | 87 | # MiKatt 20200717 88 | # WARNING: Values in column will be overwritten 89 | 90 | vect <- execGRASS("g.list", 91 | parameters = list( 92 | type = "vect" 93 | ), 94 | intern = TRUE) 95 | if (!"edges" %in% vect) 96 | stop("Edges not found. Did you run calc_edges()?") 97 | if(predictions %in% vect) 98 | execGRASS("g.remove", 99 | flags = c("quiet", "f"), 100 | parameters = list( 101 | type = "vector", 102 | name = predictions 103 | )) 104 | 105 | if(all(is.null(c(dist, nsites)))) 106 | stop("Either the distance between prediction sites (dist) or the number of 107 | prediction sites (nsites) must be given.") 108 | 109 | temp_dir <- tempdir() 110 | 111 | dt.streams <- do.call(rbind,strsplit( 112 | execGRASS("db.select", 113 | parameters = list( 114 | sql = "select cat, stream, next_str, prev_str01,prev_str02,netID,Length from edges" 115 | ),intern = T), 116 | split = '\\|')) 117 | colnames(dt.streams) <- dt.streams[1,] 118 | dt.streams <- data.table(dt.streams[-1,,drop = FALSE]) 119 | dt.streams[, names(dt.streams) := lapply(.SD, as.numeric)] 120 | dt.streams[, offset := 0] 121 | 122 | # omit all segements that do not belong to the netIDs given 123 | if(!is.null(netIDs)){ 124 | dt.streams[!(netID %in% netIDs), offset := NA] 125 | dt.streams <- stats::na.omit(dt.streams, cols = "offset") 126 | } 127 | if(nrow(dt.streams) == 0) 128 | stop("No streams to place prediction points on. Please check netIDs.") 129 | 130 | if(is.null(dist)) 131 | dist <- ceiling(sum(dt.streams[,Length]) / nsites) 132 | 133 | message("Calculating point positions ...") 134 | outlets <- dt.streams[next_str == -1, stream] 135 | for(i in outlets){ 136 | calc_offset(dt.streams, id=i, offs = 0, dist) 137 | } 138 | 139 | pt <- 1 140 | str1 <- NULL 141 | for(i in 1:nrow(dt.streams)){ 142 | offs <- dt.streams[i, "offset", with = FALSE] 143 | while(offs > 0){ 144 | str1 <- paste0(str1, "\n", paste("P", pt, dt.streams[i, "cat", with = FALSE], offs, sep=" ")) 145 | offs <- offs - dist 146 | pt <- pt + 1 147 | } 148 | } 149 | str1 <- substring(str1, 2) 150 | write(str1, file.path(temp_dir,"pt.txt")) 151 | 152 | execGRASS("v.segment", flags = c("overwrite", "quiet"), 153 | parameters = list( 154 | input = "edges", 155 | output = predictions, 156 | rules = file.path(temp_dir,"pt.txt") 157 | )) 158 | 159 | # MiKatt: No line break in long strings on Windows! 160 | message("Creating attribute table ...") 161 | execGRASS("v.db.addtable", flags = c("quiet"), 162 | parameters = list( 163 | map = predictions, 164 | columns = "cat_edge int,str_edge int,dist double precision,nx double precision,ny double precision,pid int,loc int,net int,rid int,out_dist double,distalong double precision,ratio double precision" 165 | ), ignore.stderr = TRUE, intern = TRUE) 166 | 167 | # MiKatt: Necessary to get upper and lower case column names 168 | execGRASS("v.db.renamecolumn", flags = "quiet", 169 | parameters = list( 170 | map = predictions, 171 | column = "loc,locID" 172 | )) 173 | execGRASS("v.db.renamecolumn", flags = "quiet", 174 | parameters = list( 175 | map = predictions, 176 | column = "net,netID" 177 | )) 178 | execGRASS("v.db.renamecolumn", flags = "quiet", 179 | parameters = list( 180 | map = predictions, 181 | column = "out_dist,upDist" 182 | )) 183 | execGRASS("v.db.renamecolumn", flags = "quiet", 184 | parameters = list( 185 | map = predictions, 186 | column = "nx,NEAR_X" 187 | )) 188 | execGRASS("v.db.renamecolumn", flags = "quiet", 189 | parameters = list( 190 | map = predictions, 191 | column = "ny,NEAR_Y" 192 | )) 193 | 194 | message("Setting cat_edge ...") 195 | # MiKatt: additionally get x and y coordinate 196 | execGRASS("v.distance", 197 | flags = c("overwrite", "quiet"), 198 | parameters = list(from = predictions, 199 | to = "edges", 200 | upload = "cat,dist,to_x,to_y", 201 | column = "cat_edge,dist,NEAR_X,NEAR_Y")) 202 | 203 | message("Setting pid and locID ...") 204 | execGRASS("v.db.update", 205 | parameters = list(map = predictions, 206 | column = "pid", 207 | value = "cat")) 208 | execGRASS("v.db.update", 209 | parameters = list(map = predictions, 210 | column = "locID", 211 | value = "pid")) 212 | 213 | # Set netID and rid from network --------- 214 | message("Assigning netID and rid ...") 215 | 216 | sql_str<- paste0("UPDATE ", predictions, " SET rid=(SELECT rid FROM edges WHERE ", 217 | predictions,".cat_edge=edges.cat)") 218 | execGRASS("db.execute", 219 | parameters = list( 220 | sql = sql_str 221 | )) 222 | sql_str<- paste0("UPDATE ", predictions, " SET netID=(SELECT netID FROM edges WHERE ", 223 | predictions,".cat_edge=edges.cat)") 224 | execGRASS("db.execute", 225 | parameters = list( 226 | sql = sql_str 227 | )) 228 | sql_str<- paste0("UPDATE ", predictions, " SET str_edge=(SELECT stream FROM edges WHERE ", 229 | predictions,".cat_edge=edges.cat)") 230 | execGRASS("db.execute", 231 | parameters = list( 232 | sql = sql_str 233 | )) 234 | 235 | # Calculate upDist --------- 236 | message("Calculating upDist ...") 237 | ## MiKatt was not exact enough, results in identical upDist if two points lay 238 | ## in the same raster cell 239 | 240 | # execGRASS("r.stream.distance", 241 | # flags = c("overwrite", "quiet", "o"), 242 | # parameters = list( 243 | # stream_rast = "streams_r", 244 | # direction = "dirs", 245 | # method = "downstream", 246 | # distance = "upDist" 247 | # )) 248 | # 249 | # execGRASS("v.what.rast", 250 | # flags = c("quiet"), 251 | # parameters = list( 252 | # map = predictions, 253 | # raster = "upDist", 254 | # column = "upDist" 255 | # )) 256 | # MiKatt: ! Round upDist to m 257 | 258 | execGRASS("v.distance", flags = c("quiet"), 259 | parameters =list( 260 | from = predictions, 261 | to = "edges", 262 | to_type = "line", 263 | upload = "to_along", 264 | column = "distalong" 265 | )) 266 | sql_str <- paste0('UPDATE ', predictions, ' SET upDist=', 267 | 'round(((SELECT upDist FROM edges WHERE edges.cat=', 268 | predictions, '.cat_edge)-distalong),2)') 269 | execGRASS("db.execute", 270 | parameters = list( 271 | sql=sql_str 272 | )) 273 | # Calculate distRatio = distance from lower end of edge to site / length edge 274 | message("Calculating distance ratio ...") 275 | 276 | sql_str <- paste0('UPDATE ', predictions, ' SET ratio=1-', 277 | 'distalong/', 278 | '(SELECT Length FROM edges WHERE edges.cat=', predictions, '.cat_edge)') 279 | execGRASS("db.execute", 280 | parameters = list( 281 | sql=sql_str 282 | )) 283 | execGRASS("v.db.dropcolumn", 284 | map = predictions, 285 | columns = "cat_edge") 286 | } 287 | 288 | #' Calculate offset 289 | #' @description Recursive function to calculate the offset from the downstream 290 | #' junction needed to place points with fixed distance along the streams. 291 | #' It is called by \code{\link{calc_prediction_sites}} for each 292 | #' outlet and should not be called by the user. 293 | #' 294 | #' @param dt data.table containing the attributes of the stream segments 295 | #' @param id integer; 'stream' of the stream segment 296 | #' @param offs number; offset from outlet of the stream segment (downstream); 297 | #' equals the length of the segment if the point shall be placed directly at the 298 | #' downstream junction. 299 | #' @param dist number giving the distance between the points to create in map units. 300 | #' @keywords internal 301 | #' 302 | #' @return Nothing; change 'offset' in dt. 303 | #' 304 | #' @author Mira Kattwinkel, \email{mira.kattwinkel@@gmx.net} 305 | #' 306 | #'@examples 307 | #'\dontrun{ 308 | #' outlets <- dt.streams[next_str == -1, stream] 309 | #' netID <- 1 310 | #' for(i in outlets){ 311 | #' calc_offset(dt.streams, id = i, offs = 0, dist = 200) 312 | #' } 313 | #'} 314 | 315 | calc_offset <- function(dt, id, offs, dist){ 316 | if(dt[stream == id, prev_str01,] == 0){ # check only one of prev01 and prev02 because they are always both 0 317 | dt[stream == id, offset := floor(Length - offs)] 318 | } else { 319 | dt[stream == id, offset := floor(Length - offs)] 320 | if(offs < 0){ 321 | offs <- offs + dt[stream == id, Length] 322 | } else { 323 | offs <- dist - (dt[stream == id, Length - offs] %% dist) 324 | } 325 | calc_offset(dt, dt[stream == id, prev_str01], offs, dist) 326 | calc_offset(dt, dt[stream == id, prev_str02] ,offs, dist) 327 | } 328 | } 329 | -------------------------------------------------------------------------------- /R/check_compl_confluences.R: -------------------------------------------------------------------------------- 1 | #' Check if there are more than two inflows to an outflow. 2 | #' 3 | #' It is checked, if more than two line segments flow into a node, i.e. 4 | #' if there are more than two inflows to an outflow. 5 | #' 6 | #' @return TRUE if there are complex confluences. 7 | #' 8 | #' @details It is checked, if there are columns named 'prev_str03', 'prev_str04' and 9 | #' 'prev_str05' in the attribute table of streams_v derived with \code{derive_streams} 10 | #' (i.e. based on the GRASS function 11 | #' \href{https://grass.osgeo.org/grass78/manuals/addons/r.stream.order.html}{r.stream.order}). 12 | #' 13 | #' @note \code{\link{setup_grass_environment}}, \code{\link{import_data}} and 14 | #' \code{\link{derive_streams}} must be run before. 15 | #' 16 | #' @author Mira Kattwinkel \email{mira.kattwinkel@@gmx.net} 17 | #' @export 18 | #' 19 | #' @examples 20 | #' \donttest{ 21 | #' # Initiate and setup GRASS 22 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 23 | #' if(.Platform$OS.type == "windows"){ 24 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 25 | #' } else { 26 | #' grass_program_path = "/usr/lib/grass78/" 27 | #' } 28 | #' 29 | #' setup_grass_environment(dem = dem_path, 30 | #' gisBase = grass_program_path, 31 | #' remove_GISRC = TRUE, 32 | #' override = TRUE 33 | #' ) 34 | #' gmeta() 35 | #' 36 | #' # Load files into GRASS 37 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 38 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 39 | #' import_data(dem = dem_path, sites = sites_path) 40 | #' 41 | #' # Derive streams from DEM 42 | #' derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 43 | #' 44 | #' check_compl_confluences() 45 | #' 46 | #' # Deriving streams with finer resolution will result in complext confluences 47 | #' derive_streams(burn = 0, accum_threshold = 150, condition = TRUE, clean = TRUE) 48 | #' check_compl_confluences() 49 | #' } 50 | 51 | check_compl_confluences <- function(){ 52 | ret <- FALSE 53 | cnames<-execGRASS("db.columns", 54 | parameters = list( 55 | table = "streams_v" 56 | ), intern = TRUE) 57 | if("prev_str03" %in% cnames){ 58 | #message(writeLines(strwrap("There are complex confluences in the stream network. Please run correct_compl_confluences for correction.", width = 80))) 59 | message("There are complex confluences in the stream network. Please run correct_compl_confluences() for correction.") 60 | ret <- TRUE 61 | } 62 | return(ret) 63 | } 64 | -------------------------------------------------------------------------------- /R/check_ssn.R: -------------------------------------------------------------------------------- 1 | #' Checking 'SSN' object. 2 | #' 3 | #' This function roughly checks the 'SSN' object. It returns FALSE if some 4 | #' essential columns are missing or values have illegal values. 5 | #' 6 | #' @importFrom rgdal readOGR 7 | #' @import SSN 8 | #' 9 | #' @param path character; path to .ssn object. 10 | #' @param predictions name(s) of prediction map(s) (optional). 11 | #' 12 | #' @return TRUE or FALSE depending if checks pass. 13 | #' @author Mira Kattwinkel, \email{mira.kattwinkel@@gmx.net}, 14 | #' Eduard Szoecs, \email{eduardszoecs@@gmail.com} 15 | #' @export 16 | #' 17 | #' @examples 18 | #' \donttest{ 19 | #' # Initiate and setup GRASS 20 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 21 | #' if(.Platform$OS.type == "windows"){ 22 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 23 | #' } else { 24 | #' grass_program_path = "/usr/lib/grass78/" 25 | #' } 26 | #' 27 | #' setup_grass_environment(dem = dem_path, 28 | #' gisBase = grass_program_path, 29 | #' remove_GISRC = TRUE, 30 | #' override = TRUE 31 | #' ) 32 | #' gmeta() 33 | #' 34 | #' # Load files into GRASS 35 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 36 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 37 | #' import_data(dem = dem_path, sites = sites_path) 38 | 39 | #' 40 | #' # Derive streams from DEM 41 | #' derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 42 | #' 43 | #' # Check and correct complex confluences (there are no complex confluences in this 44 | #' # example date set; set accum_threshold in derive_streams to a smaller value 45 | #' # to create complex confluences) 46 | #' cj <- check_compl_confluences() 47 | #' if(cj){ 48 | #' correct_compl_confluences() 49 | #' } 50 | #' 51 | #' # Prepare edges 52 | #' calc_edges() 53 | #' 54 | #' # Prepare site 55 | #' calc_sites() 56 | #' # Calculate H2OArea 57 | #' calc_attributes_sites_exact() 58 | #' 59 | #' # Plot data 60 | #' library(sp) 61 | #' dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 62 | #' sites <- readVECT('sites', ignore.stderr = TRUE) 63 | #' sites_orig <- readVECT('sites_o', ignore.stderr = TRUE) 64 | #' edges <- readVECT('edges', ignore.stderr = TRUE) 65 | #' plot(dem, col = terrain.colors(20)) 66 | #' lines(edges, col = 'blue') 67 | #' points(sites_orig, pch = 4) 68 | #' cols <- colorRampPalette(c("blue", 'red'))(length(sites$H2OArea))[rank(sites$H2OArea)] 69 | #' points(sites, pch = 16, col = cols) 70 | #' 71 | #' # Write data to SSN Folder 72 | #' ssn_dir <- file.path(tempdir(), 'nc.ssn') 73 | #' export_ssn(ssn_dir, delete_directory = TRUE) 74 | #' 75 | #' # Check if all files are ok 76 | #' library(SSN) 77 | #' check_ssn(ssn_dir) 78 | #' } 79 | 80 | 81 | 82 | check_ssn <- function(path, predictions = NULL) { 83 | out <- TRUE 84 | 85 | # neccesary Files --------------------------------------------------------- 86 | message("Checking necessary files...") 87 | if(file.exists(file.path(path, "edges.shp"))) { 88 | message("\tedges.shp...OK") 89 | } else { 90 | out <- out & FALSE 91 | message("\tedges.shp...FAIL!") 92 | } 93 | if(file.exists(file.path(path, "sites.shp"))) { 94 | message("\tsites.shp...OK") 95 | } else { 96 | out <- out & FALSE 97 | message("\tsites.shp...FAIL!") 98 | } 99 | if(!is.null(predictions)){ 100 | for(i in 1:length(predictions)){ 101 | if(file.exists(file.path(path, predictions[i], ".shp"))) { 102 | message(paste0("\t", predictions[i], ".shp...OK")) 103 | } else { 104 | out <- out & FALSE 105 | message(paste0("\t", predictions[i], ".shp...FAIL!")) 106 | } 107 | } 108 | } 109 | 110 | edges <- readOGR(path, "edges", verbose = FALSE) 111 | netIDs <- unique(edges$netID) 112 | bin_files <- list.files(path, pattern = "*.dat") 113 | if (all(bin_files %in% paste0("netID", netIDs, ".dat"))) { 114 | message("\tbinary files...OK") 115 | } else { 116 | out <- out & FALSE 117 | message("\tbinary files...FAIL!") 118 | } 119 | 120 | # edges ------------------------------------------------------------------- 121 | message("Checking edges.shp...") 122 | obl_cols <- c("rid", "netID", "OBJECTID", "upDist", "Length", "H2OArea", "rcaArea") 123 | if (all(obl_cols %in% names(edges@data))) { 124 | message("\tColumns...OK") 125 | } else { 126 | out <- out & FALSE 127 | message("\tColumns...FAIL! 128 | \tMissing columns: ", obl_cols[!obl_cols %in% names(edges@data)]) 129 | } 130 | if (length(unique(edges$rid)) == nrow(edges)) { 131 | message("\tUnique rids...OK") 132 | } else { 133 | out <- out & FALSE 134 | message("\tUnique rids...FAIL!") 135 | } 136 | 137 | if (max(edges$rid) >= nrow(edges) - 1) { 138 | message("\tMax rid...OK") 139 | } else { 140 | out <- out & FALSE 141 | message("\tMax rid...FAIL!") 142 | } 143 | 144 | # if (all(edges$upDist > 0)) { 145 | # message("\tupDist > 0...OK") 146 | # } else { 147 | # out <- out & FALSE 148 | # message("\tupDist > 0...FAIL!") 149 | # } 150 | 151 | if (!any(is.na(edges$netID))) { 152 | message("\tnetID...OK") 153 | } else { 154 | out <- out & FALSE 155 | message("\tnetID > 0...FAIL!") 156 | } 157 | 158 | ssn <- importSSN(path) 159 | ssn <- additive.function(ssn, "H2OArea", "afv_computed") 160 | r_afv <- range(ssn@data$afv_computed) 161 | 162 | if (all(r_afv >= 0 & r_afv <= 1)) { 163 | message("\tadditive function value range...OK") 164 | } else { 165 | out <- out & FALSE 166 | message("\tadditive function value range...FAIL!") 167 | } 168 | # columns 169 | 170 | # Obs. sites ------------------------------------------------------------------- 171 | message("Checking sites.shp...") 172 | sites <- readOGR(path, "sites", verbose = FALSE) 173 | obl_cols <- c("rid", "pid", "locID", "netID", "upDist") 174 | obl_cols2 <- "H2OArea" 175 | if (all(obl_cols %in% names(sites@data)) & any(grepl(obl_cols2, names(sites@data)))) { 176 | message("\tColumns...OK") 177 | } else { 178 | out <- out & FALSE 179 | message("\tColumns...FAIL! 180 | \tMissing columns: ", obl_cols[!obl_cols %in% names(sites@data)]) 181 | } 182 | 183 | if(any(sites$ratio > 1) | any(sites$ratio < 0)) { 184 | out <- out & FALSE 185 | message("\tratio > 0 and < 1 ...FAIL!") 186 | } else { 187 | message("\tratio...OK") 188 | } 189 | 190 | if (!any(is.na(sites$netID))) { 191 | message("\tnetID...OK") 192 | } else { 193 | out <- out & FALSE 194 | message("\tnetID > 0...FAIL!") 195 | } 196 | if (!any(is.na(sites$rid))) { 197 | message("\trid...OK") 198 | } else { 199 | out <- out & FALSE 200 | message("\trid > 0...FAIL!") 201 | } 202 | 203 | 204 | # Prediction sites ------------------------------------------------------------------- 205 | if(!is.null(predictions)){ 206 | message("Checking Prediction sites...") 207 | for(i in 1:length(predictions)){ 208 | preds <- readOGR(path, predictions[i], verbose = FALSE) 209 | obl_cols <- c("rid", "pid", "locID", "netID", "upDist", "H2OArea") 210 | if (all(obl_cols %in% names(preds@data))) { 211 | message("\tColumns...OK") 212 | } else { 213 | out <- out & FALSE 214 | message("\tColumns...FAIL! 215 | \tMissing columns: ", obl_cols[!obl_cols %in% names(preds@data)]) 216 | } 217 | 218 | if (any(preds$ratio > 1) | any (preds$ratio < 0)) { 219 | message("\tratio...OK") 220 | } else { 221 | out <- out & FALSE 222 | message("\tratio > 0 and < 1 ...FAIL!") 223 | } 224 | 225 | if (!any(is.na(preds$netID))) { 226 | message("\tnetID...OK") 227 | } else { 228 | out <- out & FALSE 229 | message("\tnetID > 0...FAIL!") 230 | } 231 | if (!any(is.na(preds$rid))) { 232 | message("\trid...OK") 233 | } else { 234 | out <- out & FALSE 235 | message("\trid > 0...FAIL!") 236 | } 237 | } 238 | } 239 | 240 | # Binary ids -------------------------------------------------------------- 241 | message("Checking Binary files...") 242 | if (all(as.numeric(gsub("netID(.*)\\.dat", "\\1", bin_files)) %in% unique(edges@data$netID)) & 243 | all(unique(edges@data$netID) %in% as.numeric(gsub("netID(.*)\\.dat", "\\1", bin_files)))) { 244 | message("\tBinary files...OK") 245 | } else { 246 | out <- out & FALSE 247 | message("\tBinary files...FAIL!") 248 | } 249 | 250 | netids <- as.numeric(gsub("netID(.*)\\.dat", "\\1", bin_files)) 251 | rid_ok <- TRUE 252 | for(i in netids){ 253 | rids_bin <- utils::read.table(file.path(path, paste0("netID", i, ".dat")), header = T, sep = ",")[,1] 254 | rids_edge <- edges$rid[edges$netID == i] 255 | if(! all(rids_bin %in% rids_bin) & all(rids_edge %in% rids_bin)){ 256 | rid_ok <- rid_ok & FALSE 257 | } 258 | } 259 | if(rid_ok){ 260 | message("\trids in binary files...OK") 261 | } else { 262 | out <- out & FALSE 263 | message("\trids in binary files...FAIL!") 264 | } 265 | 266 | # preds same in edges and sites 267 | # per network id net*.dat same as rid 268 | return(out) 269 | } 270 | -------------------------------------------------------------------------------- /R/export_ssn.R: -------------------------------------------------------------------------------- 1 | #' Export 'SSN' object 2 | #' 3 | #' This function exports the calculated sites, edges and binary IDs 4 | #' to a folder which then can be read using the 'SSN' package. 5 | #' 6 | #' @import rgrass7 7 | #' @importFrom utils write.table 8 | #' 9 | #' @param path character; path to write .ssn object to. 10 | #' @param predictions name(s) of prediction map(s) (optional). 11 | #' @param delete_directory boolean; shall the ssn directory and all files be 12 | #' deleted before export in case it already exists? See details. 13 | #' 14 | #' @return Nothing. Files are written to the specified folder 15 | #' 16 | #' @details First it is checked if one of the column names is longer than 10 17 | #' characters (which cannot be exported to ESRI shape files as required by 'SSN'). 18 | #' 19 | #' \code{delete_directory = TRUE} is useful if the same directory name has been 20 | #' used before and the existing data shall be overwritten. 21 | #' 22 | #' @author Mira Kattwinkel,\email{mira.kattwinkel@gmx.net}, 23 | #' Eduard Szoecs, \email{eduardszoecs@@gmail.com} 24 | #' @export 25 | #' 26 | #' @examples 27 | #' \donttest{ 28 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 29 | #' if(.Platform$OS.type == "windows"){ 30 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 31 | #' } else { 32 | #' grass_program_path = "/usr/lib/grass78/" 33 | #' } 34 | #' 35 | #' setup_grass_environment(dem = dem_path, 36 | #' gisBase = grass_program_path, 37 | #' remove_GISRC = TRUE, 38 | #' override = TRUE 39 | #' ) 40 | #' gmeta() 41 | #' 42 | #' # Load files into GRASS 43 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 44 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 45 | #' import_data(dem = dem_path, sites = sites_path) 46 | #' 47 | #' # Derive streams from DEM 48 | #' derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 49 | #' 50 | #' # Check and correct complex confluences (there are no complex confluences in this 51 | #' # example date set; set accum_threshold in derive_streams to a smaller value 52 | #' # to create complex confluences) 53 | #' cj <- check_compl_confluences() 54 | #' if(cj){ 55 | #' correct_compl_confluences() 56 | #' } 57 | #' 58 | #' # Prepare edges 59 | #' calc_edges() 60 | #' 61 | #' # Prepare site 62 | #' calc_sites() 63 | #' 64 | #' # Write data to SSN Folder 65 | #' ssn_dir <- file.path(tempdir(), 'nc.ssn') 66 | #' export_ssn(ssn_dir, delete_directory = TRUE) 67 | #' list.files(ssn_dir) 68 | #' } 69 | #' 70 | export_ssn <- function(path, predictions = NULL, delete_directory = FALSE){ 71 | if(file.exists(path) & delete_directory == FALSE) 72 | stop(paste(path, "already exists. To delete it use 'delete_directory = TRUE'.")) 73 | if(file.exists(path) & delete_directory == TRUE) 74 | unlink(path, recursive = T) 75 | cnames<-execGRASS("db.columns", 76 | parameters = list( 77 | table = "edges" 78 | ), intern = TRUE) 79 | 80 | cnames<-c(cnames,execGRASS("db.columns", 81 | parameters = list( 82 | table = "sites" 83 | ), intern = TRUE)) 84 | 85 | if(!is.null(predictions)){ 86 | cnames <- NULL 87 | for(i in 1:length(predictions)){ 88 | cnames<-c(cnames,c(cnames,execGRASS("db.columns", 89 | parameters = list( 90 | table = predictions[i] 91 | ), intern = TRUE))) 92 | } 93 | } 94 | 95 | if(any(unlist(lapply(cnames,nchar)) > 10)) 96 | stop(paste("Some column names are longer than 10 characters and cannot be exported 97 | to shape files; please correct. Nothing exported.\n", 98 | paste(cnames[unlist(lapply(cnames,nchar)) > 10], collapse = ", "))) 99 | 100 | message("Exporting to ", path) 101 | # write edges 102 | # MiKatt first copy edges and drop attributes not needed for ssn 103 | execGRASS("g.copy", 104 | flags = c("overwrite", "quiet"), 105 | parameters = list( 106 | vector = "edges,edges2")) 107 | execGRASS("v.db.dropcolumn", 108 | flags = "quiet", 109 | parameters = list( 110 | map = "edges2", 111 | columns = "next_str,prev_str01,prev_str02" 112 | )) 113 | # 20180219: ESRI_Shapefile is no longer the default format 114 | a <- execGRASS("v.out.ogr", 115 | flags = c("overwrite", "quiet"), 116 | parameters = list( 117 | input = "edges2", 118 | type = "line", 119 | output = path, 120 | format = "ESRI_Shapefile", 121 | output_layer = "edges" 122 | #dsco = "RESIZE=YES" does not help to prevent warning 123 | ), intern = TRUE, ignore.stderr = TRUE) 124 | execGRASS("g.remove", 125 | flags = c("quiet", "f"), 126 | parameters = list( 127 | type = "vector", 128 | name = "edges2" 129 | )) 130 | 131 | # write sites 132 | a <- execGRASS("v.out.ogr", 133 | c("overwrite", "quiet"), 134 | parameters = list( 135 | input = "sites", 136 | type = "point", 137 | output = path, 138 | format = "ESRI_Shapefile", 139 | output_layer = "sites" 140 | ), intern = TRUE, ignore.stderr = TRUE) 141 | 142 | # write preds 143 | if(!is.null(predictions)){ 144 | for(i in seq_along(predictions)) 145 | a <- execGRASS("v.out.ogr", 146 | c("overwrite", "quiet"), 147 | parameters = list( 148 | input = predictions[i], 149 | type = "point", 150 | output = path, 151 | format = "ESRI_Shapefile", 152 | output_layer = predictions[i] 153 | ), intern = TRUE, ignore.stderr = TRUE) 154 | } 155 | 156 | # create binary 157 | binary <- calc_binary() 158 | 159 | # write binary files 160 | # temporarly turn off scientific notation 161 | old_scipen <- options("scipen")$scipen 162 | options(scipen = 999) 163 | # write files 164 | lapply(names(binary), 165 | function(y) { 166 | write.table(binary[[y]], 167 | file.path(path, paste0("netID", y, ".dat")), 168 | sep = ",", 169 | row.names = FALSE) 170 | }) 171 | # restore 172 | options(scipen = old_scipen) 173 | } 174 | 175 | #' Calculate binary IDs for stream networks. 176 | #' 177 | #' Calculate binary IDs for each stream network built up by '0' and '1'. 178 | #' This function is called by \code{\link{export_ssn}} and there is no need for it 179 | #' be called by the users. 180 | #' 181 | #' @import data.table 182 | #' 183 | #' @return A list with one slot for each network id containing a data frame 184 | #' with 'rid' and 'binaryID' for each segment belonging to this network. 185 | #' 186 | #' @note \code{\link{import_data}}, \code{\link{derive_streams}}, 187 | #' \code{\link{calc_edges}} and \code{\link{calc_sites}} must be run before. 188 | #' 189 | #' @author Eduard Szoecs, \email{eduardszoecs@@gmail.com}; Mira Kattwinkel, \email{mira.kattwinkel@@gmx.net} 190 | #' 191 | #' @export 192 | #' 193 | calc_binary <- function(){ 194 | vect <- execGRASS("g.list", 195 | parameters = list( 196 | type = "vect" 197 | ), 198 | intern = TRUE) 199 | #if (!"sites_o" %in% vect) 200 | # stop("Sites not found. Did you run import_data()?") 201 | if (!"edges" %in% vect) 202 | stop("edges not found. Did you run calc_edges()?") 203 | if (!"sites" %in% vect) 204 | stop("sites not found. Did you run calc_sites()?") 205 | 206 | dt.streams<-execGRASS("db.select", 207 | flags = "c", 208 | parameters = list( 209 | sql = "select rid,stream,next_str,prev_str01,prev_str02,netID from edges", 210 | separator = "," 211 | ), intern = TRUE) 212 | 213 | dt.streams<-do.call(rbind,strsplit(dt.streams,split=",")) 214 | dt.streams<-apply(dt.streams,2,as.numeric) 215 | colnames(dt.streams)<-c("rid","stream","next_str","prev_str01","prev_str02","netID") 216 | dt.streams <- data.frame(dt.streams) 217 | setDT(dt.streams) 218 | dt.streams[, binaryID := "0"] 219 | outlets <- dt.streams[next_str == -1, stream] 220 | 221 | for(i in outlets){ 222 | assign_binIDs(dt = dt.streams, id=i, 1, NULL) 223 | } 224 | 225 | bins<-lapply(outlets, function(x) dt.streams[netID == dt.streams[stream == x, netID], list(rid,binaryID)]) 226 | names(bins)<- dt.streams[stream %in% outlets, netID] 227 | return(bins) 228 | } 229 | 230 | #' Recursive function to assign binary id to stream segments. 231 | #' 232 | #' This function is run for all outlets in the network ( = most downstream segments) 233 | #' and fills the binID for all segments. It is called by \code{calc_binary} in 234 | #' \code{export_ssn} and should not be called by the user. 235 | #' 236 | #' @param id: stream segment 237 | #' @param binID: binary ID 238 | #' @param lastbit: last char to be added (0 or 1) 239 | #' @keywords internal 240 | #' 241 | assign_binIDs <- function(dt, id, binID, lastbit){ 242 | if(dt[stream == id, prev_str01 ] == 0){ # check only one of prev01 and prev02 because they are always both 0 243 | dt[stream == id, binaryID := paste0(binID, lastbit)] 244 | } else { 245 | dt[stream == id, binaryID := paste0(binID,lastbit)] 246 | assign_binIDs(dt, dt[stream == id, prev_str01], dt[stream == id, binaryID], 0) 247 | assign_binIDs(dt, dt[stream == id, prev_str02], dt[stream == id, binaryID], 1) 248 | } 249 | } 250 | -------------------------------------------------------------------------------- /R/globals.R: -------------------------------------------------------------------------------- 1 | # define column names of data.tables as global variables to prevent a NOTE in 2 | # R CMD check 'no visible binding for global variable'. 3 | if(getRversion() >= "2.15.1") 4 | utils::globalVariables(c(".", "all_cells", "area", "binaryID", "cat_", "cat_large", "cat_small", "changed", "cum_length", "cumsum_cells", "cut_stream", "cut_stream_prev", "cut_x", "cut_y", "dif", "edge_cat", "end_x", "end_xy", "end_y", "H2OArea", "len", "Length", "length_new", "move_stream", "move_stream_prev", "netID", "newlen", "new_length","next_str", "non_null_cells", "OBJECTID", "offset", "out_dist", "pcat", "prev_str01", "prev_str02", "prev_str03", "prev_str04", "rcaArea","rid", "start_x", "start_xy", "start_y", "str_new_lake","str_new_small", "str_new_large", "stream", "total_area", "value","variable", "X1", "X2")) -------------------------------------------------------------------------------- /R/merge_sites_measurements.R: -------------------------------------------------------------------------------- 1 | #'Merge a table with measurements to the sites. 2 | #' 3 | #'@description After all processing steps are done and before exporting as an SSN object 4 | #' measurements can be added to the site map. They can contain multiple parameters and 5 | #' repeated measurements at the same site. 6 | #' 7 | #'@param measurements character string, data.table or data.frame object; 8 | #' path to table data containing the data or a data.table or data.frame object 9 | #'@param site_id character string; columns name that gives the unique name of the 10 | #' site. Must be identical in both the sites vector object and the table of 11 | #' measurements 12 | #'@param all_sites logical; should sites without measurements be preserved (default FALSE) 13 | #'@param ... additional arguments to read.table in case \code{measuremtes} is a file path 14 | #' to table data; see \code{\link{read.table}} for details. 15 | #' 16 | #'@details Measurements are merged to the sites objects based on \code{site_id}. If 17 | #' there are repeated measurements, point features are duplicated and the 'pid' of the 18 | #' sites is updated accordingly to be unique. 19 | #' 20 | #'@author Mira Kattwinkel \email{mira.kattwinkel@@gmx.net} 21 | #' 22 | #'@export 23 | #' 24 | #' @examples 25 | #' \donttest{ 26 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 27 | #' if(.Platform$OS.type == "windows"){ 28 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 29 | #' } else { 30 | #' grass_program_path = "/usr/lib/grass78/" 31 | #' } 32 | #' 33 | #' setup_grass_environment(dem = dem_path, 34 | #' gisBase = grass_program_path, 35 | #' remove_GISRC = TRUE, 36 | #' override = TRUE 37 | #' ) 38 | #' gmeta() 39 | #' 40 | #' # Load files into GRASS 41 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 42 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 43 | #' import_data(dem = dem_path, sites = sites_path) 44 | #' 45 | #' # Derive streams from DEM 46 | #' derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 47 | #' 48 | #' # Check and correct complex junctions (there are no complex confluences in this 49 | #' # example date set) 50 | #' cj <- check_compl_confluences() 51 | #' if(cj){ 52 | #' correct_compl_confluences() 53 | #' } 54 | #' 55 | #' # Prepare edges 56 | #' calc_edges() 57 | #' 58 | #' # Prepare site 59 | #' calc_sites() 60 | #' 61 | #' merge_sites_measurements(measurements = system.file( 62 | #' "extdata", "nc", "obs_data.csv", package = "openSTARS"), 63 | #' site_id = "site_id", sep = ",", dec = ".") 64 | #' # note the dublicated rows, and the new columns at the end 65 | #' sites <- readVECT("sites", ignore.stderr = TRUE) 66 | #' head(sites@data, n = 6) 67 | #'} 68 | 69 | 70 | merge_sites_measurements <- function(measurements, site_id, all_sites = FALSE, ...) { 71 | 72 | if(!is.data.frame(measurements) & !is.data.table(measurements)){ 73 | d <- try(measurements <- utils::read.table(measurements, header = T, stringsAsFactors = FALSE, ...)) 74 | if(class(d) == "try-error") 75 | stop("'measurements' must contain a valid path name to table data.") 76 | } 77 | 78 | if(!site_id %in% colnames(measurements)){ 79 | #stop(writeLines(strwrap(paste0("'site_id' (", site_id, ") must contain a valid colum name in 'measurements'. Options are: ", 80 | # paste0(colnames(measurements), collapse = ", ")), width = 80))) 81 | stop(paste0("'site_id' (", site_id, ") must contain a valid colum name in 'measurements'. Options are: ", 82 | paste0(colnames(measurements), collapse = ", "))) 83 | } 84 | 85 | sites <- readVECT("sites", ignore.stderr = TRUE) 86 | if(!site_id %in% colnames(sites@data)){ 87 | #stop(writeLines(strwrap(paste0("'site_id' (", site_id, ") must contain a valid colum name in 'sites'. Options are: ", 88 | # paste0(colnames(sites@data), collapse = ", ")), width = 80))) 89 | stop(paste0("'site_id' (", site_id, ") must contain a valid colum name in 'sites'. Options are: ", 90 | paste0(colnames(sites@data), collapse = ", "))) 91 | } 92 | 93 | sites <- sp::merge(sites, measurements, by = site_id, duplicateGeoms = TRUE, all.x = all_sites) 94 | d <- sites@data 95 | d$pid <- 1:nrow(d) 96 | row.names(d) <- 1:nrow(d) 97 | i <- which(colnames(d) %in% c("cat", "cat_")) 98 | d <- d[,-i] 99 | sites@data <- d 100 | sink("temp.txt") 101 | writeVECT(sites, "sites", v.in.ogr_flags = c("overwrite", "quiet", "o"), ignore.stderr = TRUE) 102 | sink() 103 | } 104 | -------------------------------------------------------------------------------- /R/openSTARS.R: -------------------------------------------------------------------------------- 1 | #' openSTARS: An Open Source Implementation of the 'ArcGIS' Toolbox 'STARS'. 2 | #' 3 | #' openSTARS provides functions to prepare data so that it can be imported by 4 | #' the \code{\link[SSN]{SSN}} package for spatial modelling on stream networks. 5 | #' 'GRASS GIS 7.0' (or greater) with installed addons r.stream.basins, 6 | #' r.stream.distance, r.stream.order, and r.hydrodem is needed. 7 | #' 8 | #' 9 | #' @import rgrass7 10 | #' @import data.table 11 | #' 12 | #' @docType package 13 | #' @name openSTARS 14 | #' 15 | #' @examples 16 | #' \donttest{ 17 | #' # Initiate and setup GRASS 18 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 19 | #' if(.Platform$OS.type == "windows"){ 20 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 21 | #' } else { 22 | #' grass_program_path = "/usr/lib/grass78/" 23 | #' } 24 | #' 25 | #' setup_grass_environment(dem = dem_path, 26 | #' gisBase = grass_program_path, 27 | #' remove_GISRC = TRUE, 28 | #' override = TRUE 29 | #' ) 30 | #' gmeta() 31 | #' 32 | #' # Load files into GRASS 33 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 34 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 35 | #' streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 36 | #' preds_v_path <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS") 37 | #' preds_r_path <- system.file("extdata", "nc", "landuse_r.tif", package = "openSTARS") 38 | #' 39 | #' import_data(dem = dem_path, sites = sites_path, streams = streams_path, 40 | #' predictor_vector = preds_v_path, predictor_raster = preds_r_path) 41 | #' 42 | #' # Derive streams from DEM 43 | #' # burn in 'streams' 10 meters 44 | #' derive_streams(burn = 10, accum_threshold = 700, condition = TRUE, clean = TRUE) 45 | #' 46 | #' # Check and correct complex confluences (there are no complex confluences in this 47 | #' # example date set; set accum_threshold in derive_streams to a smaller value 48 | #' # to create complex confluences) 49 | #' cj <- check_compl_confluences() 50 | #' if(cj){ 51 | #' correct_compl_confluences() 52 | #' } 53 | #' 54 | #' # calculate slope as potential predictor 55 | #' execGRASS("r.slope.aspect", flags = c("overwrite","quiet"), 56 | #' parameters = list( 57 | #' elevation = "dem", 58 | #' slope = "slope" 59 | #' )) 60 | #' 61 | #' # Prepare edges 62 | #' calc_edges() 63 | #' calc_attributes_edges(input_raster = c("slope", "landuse_r"), 64 | #' stat_rast = c("max", "percent"), 65 | #' attr_name_rast = c("maxSlo", "luse"), 66 | #' input_vector = "pointsources", stat_vect = "count", 67 | #' attr_name_vect = "psource") 68 | #' 69 | #' # Prepare site 70 | #' calc_sites() 71 | #' 72 | #' # Usually, only one of the following methods is needed. The exact one takes 73 | #' # longer to run 74 | #' # approximate potential predictor variables for each site based on edge values 75 | #' calc_attributes_sites_approx(input_attr_name = c("maxSlo", "lusep_1", "lusep_2", 76 | #' "lusep_3", "lusep_4", "lusep_5", 77 | #' "lusep_6", "lusep_7"), 78 | #' output_attr_name = c("maxSloA","luse1A", "luse2A", 79 | #' "luse_3A", "luse4A", "luse5A", 80 | #' "luse6A", "luse7A"), 81 | #' stat = c("max", rep("percent", 7))) 82 | #' 83 | #' # exact potential predictor variables for each site based on catchments 84 | #' calc_attributes_sites_exact(input_raster = c("slope", "landuse_r"), 85 | #' attr_name_rast = c("maxSloEx", "luseE"), 86 | #' stat_rast = c("max", "percent")) 87 | #' 88 | #' # Plot data 89 | #' library(sp) 90 | #' dem <- readRAST("dem", ignore.stderr = TRUE, plugin = FALSE) 91 | #' sites <- readVECT("sites", ignore.stderr = TRUE) 92 | #' sites_orig <- readVECT("sites_o", ignore.stderr = TRUE) 93 | #' edges <- readVECT("edges", ignore.stderr = TRUE) 94 | #' plot(dem, col = terrain.colors(20)) 95 | #' lines(edges, col = "blue") 96 | #' points(sites_orig, pch = 4) 97 | #' cols <- colorRampPalette(c("blue", "red"))(length(sites$H2OArea))[rank(sites$H2OArea)] 98 | #' points(sites, pch = 16, col = cols) 99 | #' 100 | #' # Write data to SSN Folder 101 | #' ssn_dir <- file.path(tempdir(), "nc.ssn") 102 | #' export_ssn(ssn_dir, delete_directory = TRUE) 103 | #' 104 | #' # Check if all files are ok 105 | #' library(SSN) 106 | #' check_ssn(ssn_dir) 107 | #' 108 | #' # Load into SSN-package 109 | #' ssn_obj <- importSSN(ssn_dir, o.write = TRUE) 110 | #' print(ssn_obj) 111 | #' } 112 | #' 113 | #' 114 | #' #Datasets shipped with openSTARS 115 | #' 116 | #' @name openSTARS_data 117 | #' All data has been taken from the GRASS GIS North Carolina data set. 118 | #' Source \url{https://grass.osgeo.org/download/sample-data/} or artificially 119 | #' created. 120 | #' 121 | #' @section obs_data.csv: 122 | #' Artificial observation data with arbitrary measurements. 123 | #' @section elev_ned30m.tif: 124 | #' South-West Wake county National Elevation Data 30m. 125 | #' @section sites_nc.shp: 126 | #' Arbitrary sites along rivers in North Carolina. 127 | #' @section streams.shp: 128 | #' Rivers in North Carolina. 129 | #' @section geology.shp: 130 | #' Geological data. 131 | #' @section landuse_r.tif: 132 | #' Land use date in North Carolina. 133 | #' @section lakes.shp: 134 | #' Artificial lakes (not at topologically correct locations) 135 | #' @section pointsources.shp: 136 | #' Artificial point sources. 137 | NULL -------------------------------------------------------------------------------- /R/restrict_network.R: -------------------------------------------------------------------------------- 1 | #' Restrict edges to certain stream networks 2 | #' 3 | #' When the stream network is derived from a DEM, the network will cover the whole 4 | #' extent of the DEM input. However, the observation sites might be restricted 5 | #' to a certain area, i.e. to certain networks. This function deletes edges 6 | #' that belong to networks (based on their netID) without sites (observation or prediction). 7 | #' This can help to save computation time before calculating edge attributes. 8 | #' 9 | #' @param sites name(s) of sites. 10 | #' @param keep_netIDs numeric (optional); vector of netIDs to keep 11 | #' @param delete_netIDs numeric (optional); vector of netIDs to delete 12 | #' @param keep boolean; should the original 'edges' be saved? Default is TRUE. 13 | #' @param filename character string; file name to save the original edges vector file; 14 | #' defaults to 'edges_o'. 15 | #' 16 | #' @return Nothing. The function updates 'edges' and (if keep = TRUE) saves 17 | #' the original file to the file name provided. 18 | #' 19 | #' 20 | #' @author Mira Kattwinkel \email{mira.kattwinkel@@gmx.net} 21 | #' @export 22 | #' 23 | #' @examples 24 | #' \donttest{ 25 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 26 | #' if(.Platform$OS.type == "windows"){ 27 | #' grass_program_path = "c:/Program Files/GRASS GIS 7.6" 28 | #' } else { 29 | #' grass_program_path = "/usr/lib/grass78/" 30 | #' } 31 | #' 32 | #' setup_grass_environment(dem = dem_path, 33 | #' gisBase = grass_program_path, 34 | #' remove_GISRC = TRUE, 35 | #' override = TRUE 36 | #' ) 37 | #' gmeta() 38 | #' 39 | #' # Load files into GRASS 40 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 41 | #' sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 42 | #' lakes_path <- system.file("extdata", "nc", "lakes.shp", package = "openSTARS") 43 | #' import_data(dem = dem_path, sites = sites_path) 44 | #' 45 | #' # Derive streams from DEM 46 | #' derive_streams(burn = 0, accum_threshold = 100, condition = TRUE, clean = TRUE) 47 | #' 48 | #' # Check and correct complex confluences (there are no complex confluences in this 49 | #' # example date set; set accum_threshold in derive_streams to a smaller value 50 | #' # to create complex confluences) 51 | #' cj <- check_compl_confluences() 52 | #' if(cj){ 53 | #' correct_compl_confluences() 54 | #' } 55 | #' 56 | #' calc_edges() 57 | #' calc_sites() 58 | #' restrict_network(sites = "sites", keep = TRUE, filename = "edges_o") 59 | #' 60 | #' # plot 61 | #' library(sp) 62 | #' edges <- readVECT('edges', ignore.stderr = TRUE) 63 | #' edges_o <- readVECT('edges_o', ignore.stderr = TRUE) 64 | #' sites <- readVECT('sites', ignore.stderr = TRUE) 65 | #' plot(edges_o, col = "lightblue", lwd = 2) 66 | #' lines(edges, col = "blue4", lty = 2) 67 | #' points(sites, pch = 16, col = "red") 68 | #' legend("topright", col = c("red", "lightblue", "blue4"), lty = c(NA, 1, 2), 69 | #' lwd = c(NA,2,1), pch = c(16,NA,NA), 70 | #' legend = c("sites", "edges original", "edges restricted")) 71 | #' } 72 | 73 | restrict_network <- function(sites = NULL, 74 | keep_netIDs = NULL, 75 | delete_netIDs = NULL, 76 | keep = TRUE, 77 | filename = "edges_o"){ 78 | 79 | if(! is.null(sites)){ 80 | vect <- execGRASS("g.list", 81 | parameters = list( 82 | type = "vector" 83 | ), intern = TRUE) 84 | 85 | if(!any(sites %in% vect)){ 86 | stop(ifelse(length(sites) == 1, paste(sites, "does not exist."), paste(paste(sites, collapse = ", "), "do not exist."))) 87 | } 88 | } 89 | 90 | if((sum(is.null(keep_netIDs), is.null(delete_netIDs), is.null(sites)) == 3) | 91 | (!is.null(keep_netIDs) & ! is.null(delete_netIDs)) | 92 | (!is.null(sites) & ! is.null(delete_netIDs))) 93 | stop("Only 'sites' and 'keep_netIDs' can be combined; 'delete_netIDs' must be provided without the other two.") 94 | 95 | 96 | netIDs <- NULL 97 | if(!is.null(sites)){ 98 | for(i in 1:length(sites)){ 99 | netIDs <- c(netIDs,unique(as.numeric(execGRASS("v.db.select", flags = "quiet", 100 | parameters = list( 101 | map = sites[i], 102 | columns = "netID", 103 | separator = "," 104 | ), 105 | ignore.stderr = TRUE, 106 | intern = TRUE)[-1]))) 107 | } 108 | } 109 | if(!is.null(keep_netIDs)){ 110 | netIDs <- c(netIDs, keep_netIDs) 111 | } 112 | netIDs <- unique(netIDs) 113 | 114 | if(!is.null(delete_netIDs)){ 115 | all_netIDs <- unlist(strsplit( 116 | execGRASS("db.select", 117 | parameters = list( 118 | sql = "select netID from edges" 119 | ), intern = T), 120 | split = "\\|"))[-1] 121 | all_netIDs <- sort(as.numeric(unique(all_netIDs))) 122 | netIDs <- all_netIDs[-which(all_netIDs %in% delete_netIDs)] 123 | } 124 | 125 | if(keep == TRUE){ 126 | execGRASS("g.copy", flags = c("quiet", "overwrite"), 127 | parameters = list( 128 | vector = paste0("edges,", filename) 129 | ), intern = TRUE, ignore.stderr = TRUE) 130 | message(paste0("Original edges moved to ", filename, ".")) 131 | } 132 | 133 | message(paste("Deleting edges with netIDs other than", paste(netIDs, collapse = ", "), "...")) 134 | # This deletes the features but keeps them in the attribute table 135 | # execGRASS("v.edit", flags = c("overwrite", "quiet"), 136 | # parameters = list( 137 | # map = "edges", 138 | # type = "line", 139 | # tool = "delete", 140 | # where = paste0("netID NOT IN (", paste0(netIDs, collapse = "," ), ")") 141 | # )) 142 | a <- execGRASS("v.extract", flags = c("overwrite", "quiet"), 143 | parameters = list( 144 | input = filename, 145 | output = "edges", 146 | type = "line", 147 | where = paste0("netID IN (", paste0(netIDs, collapse = "," ), ")") 148 | ), intern = TRUE, ignore.stderr = TRUE) 149 | } 150 | 151 | -------------------------------------------------------------------------------- /R/setup_grass_environment.R: -------------------------------------------------------------------------------- 1 | #' Setup 'GRASS' environment. 2 | #' 3 | #' This function sets the 'GRASS' mapset to PERMANENT and sets its projection and extension. 4 | #' 5 | #' @param dem character; path to DEM. 6 | #' @param gisBase character; the directory path to GRASS binaries and libraries, containing 7 | #' bin and lib subdirectories among others (see details). 8 | #' @param epsg integer (deprecated); not used any more. Only included for compatibility with previous version. 9 | #' @param sites (deprecated); not used any more. Only included for compatibility with previous version. 10 | #' @param ... Optional arguments to be passed to \code{\link[rgrass7]{initGRASS}} (see details). 11 | #' 12 | #' @return Nothing. A GRASS session is initiated and the 'GRASS' mapset is set to PERMANENT. 13 | #' The geographical projection, geographical extension, number of columns and number of 14 | #' rows for the data and the resolution are defined by the dem. They are stored the DEFAULT_WIND file. 15 | #' 16 | #' @details A GRASS session is initiated using \code{\link[rgrass7]{initGRASS}}. The path to 17 | #' the GRASS program must be provided as \code{gisBase}. For Linux, this might look like 18 | #' "/usr/lib/grass78/" and for Windows "c:/Program Files/GRASS GIS 7.8". 19 | #' Optional arguments are for example 20 | #' * \code{gisDbase}: the GRASS GISBASE directory for this session; defaults to tempdir() 21 | #' * \code{location}: name of the location for this session; defaults to tempfile() 22 | #' * \code{override}: TRUE for allowing to override an existing location. 23 | #' 24 | #' @note It is no longer required to initiate a GRASS session before using \code{\link[rgrass7]{initGRASS}}! 25 | #' 26 | #' @author Mira Kattwinkel, \email{mira.kattwinkel@@gmx.net} 27 | #' @export 28 | #' 29 | #' @examples 30 | #' \donttest{ 31 | #' # path to GRASS 32 | #' if(.Platform$OS.type == "windows"){ 33 | #' gisbase = "c:/Program Files/GRASS GIS 7.6" 34 | #' } else { 35 | #' gisbase = "/usr/lib/grass78/" 36 | #' } 37 | #' # path to the dem 38 | #' dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 39 | #' setup_grass_environment(dem = dem_path, 40 | #' gisBase = gisbase, 41 | #' location = "nc_example_location", 42 | #' override = TRUE) 43 | #' gmeta() 44 | #' } 45 | 46 | setup_grass_environment <- function(dem, gisBase, epsg = NULL, sites = NULL, ...){ 47 | if(!is.null(sites)) 48 | message(writeLines(strwrap("'sites' is no longer a parameter of setup_grass_environment (see help).\n 49 | The function will still execute normally. Please update your code.", 50 | width = 80))) 51 | if(!is.null(epsg)) 52 | message(writeLines(strwrap("'epsg' is no longer a parameter of setup_grass_environment (see help).\n 53 | The function will still execute normally. Please update your code.", 54 | width = 80))) 55 | 56 | use_sp() 57 | dem_grid <- rgdal::readGDAL(dem, silent = TRUE) 58 | initGRASS(gisBase = gisBase, 59 | SG = dem_grid, 60 | mapset = "PERMANENT", 61 | ...) 62 | execGRASS("g.proj", flags = c("c", "quiet"), 63 | parameters = list( 64 | georef = dem 65 | )) 66 | } 67 | 68 | 69 | #' Update attribute table. 70 | #' 71 | #' Wrapper for v.to.db to catch errors arising in some GRASS versions 72 | #' 73 | #' @param map character; name of the map where values should be uploaded. 74 | #' @param option character; what values should be uploaded 75 | #' @param columns character; name of the column top upload data to 76 | #' @param format character; data format of the new columns (if it must be created) 77 | #' @param type character; feature type (default = "line") 78 | #' 79 | #' @return Nothing. Uses \href{https://grass.osgeo.org/grass78/manuals/v.to.db.html}{v.to.db} 80 | #' to populate attribute values from vector features. 81 | #' 82 | #' @details Since different versions of GRASS handle v.to.db differently (older versions <= 7.4 need 83 | #' the column to exists. while newer ones create the column) different implementations are 84 | #' necessary 85 | #' 86 | #' @author Mira Kattwinkel, \email{mira.kattwinkel@@gmx.net} 87 | #' @export 88 | #' 89 | grass_v.to.db <- function(map, option, type = "line", columns, format){ 90 | # MiKatt 20200724: 91 | # Does not work with 7.4; gives 92 | # DBMI-SQLite driver error: 93 | # Error in sqlite3_prepare(): 94 | # no such column: length_new 95 | # check <- try(execGRASS("v.to.db", flags = c("quiet"), 96 | # parameters = list( 97 | # map = map, 98 | # option = option, 99 | # type = type, 100 | # columns = paste(columns, collapse = ","))), silent = TRUE) 101 | # # create column first, then fill it version < 7.6 102 | # if(class(check) == "try-error"){ 103 | # execGRASS("v.db.addcolumn", flags = "quiet", 104 | # parameters = list( 105 | # map = map, 106 | # columns = paste0(paste(columns, format), collapse = ",") 107 | # )) 108 | # execGRASS("v.to.db", flags = c("quiet"), 109 | # parameters = list( 110 | # map = map, 111 | # option = option, 112 | # type = type, 113 | # columns = paste(columns, collapse = ",") 114 | # ), ignore.stderr = TRUE) 115 | # } 116 | 117 | execGRASS("v.db.addcolumn", flags = "quiet", 118 | parameters = list( 119 | map = map, 120 | columns = paste0(paste(columns, format), collapse = ",") 121 | )) 122 | 123 | check <- try(execGRASS("v.to.db", flags = c("quiet"), 124 | parameters = list( 125 | map = map, 126 | option = option, 127 | type = type, 128 | columns = paste(columns, collapse = ",") 129 | ), ignore.stderr = TRUE), silent = TRUE) 130 | # use overwrite for Grass 7.8 131 | # still does not know overwrite flag! 132 | if(class(check) == "try-error"){ 133 | execGRASS("v.db.dropcolumn", flags = "quiet", 134 | parameters = list( 135 | map = map, 136 | columns = paste0(columns, collapse = ",") 137 | )) 138 | 139 | execGRASS("v.to.db", flags = c("quiet"), 140 | parameters = list( 141 | map = map, 142 | option = option, 143 | type = type, 144 | columns = paste(columns, collapse = ",") 145 | ), ignore.stderr = TRUE) 146 | } 147 | } -------------------------------------------------------------------------------- /README_files/compl_junction1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/compl_junction1.PNG -------------------------------------------------------------------------------- /README_files/compl_junction2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/compl_junction2.PNG -------------------------------------------------------------------------------- /README_files/figure-html/plot_compl_confl-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_compl_confl-1.png -------------------------------------------------------------------------------- /README_files/figure-html/plot_compl_junctions-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_compl_junctions-1.png -------------------------------------------------------------------------------- /README_files/figure-html/plot_data1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_data1-1.png -------------------------------------------------------------------------------- /README_files/figure-html/plot_data1.1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_data1.1-1.png -------------------------------------------------------------------------------- /README_files/figure-html/plot_data1.2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_data1.2-1.png -------------------------------------------------------------------------------- /README_files/figure-html/plot_data2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_data2-1.png -------------------------------------------------------------------------------- /README_files/figure-html/plot_data3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_data3-1.png -------------------------------------------------------------------------------- /README_files/figure-html/plot_data4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/plot_data4-1.png -------------------------------------------------------------------------------- /README_files/figure-html/ssn_test-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/ssn_test-1.png -------------------------------------------------------------------------------- /README_files/figure-html/ssn_test-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-html/ssn_test-2.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/plot_compl_confl-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/plot_compl_confl-1.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/plot_data1.1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/plot_data1.1-1.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/plot_data1.2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/plot_data1.2-1.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/plot_data2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/plot_data2-1.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/plot_data3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/plot_data3-1.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/plot_data4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/plot_data4-1.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/ssn_test-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/ssn_test-1.png -------------------------------------------------------------------------------- /README_files/figure-markdown_github/ssn_test-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/README_files/figure-markdown_github/ssn_test-2.png -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "cran-comments.md" 3 | author: "Mira Kattwinkel" 4 | date: "Feb 1704, 2022" 5 | output: html_document 6 | --- 7 | 8 | ## Test environments 9 | * Linux Mint 19.0 Tara, R 3.6.3, grass 7.4 10 | * Linux Mint 19.0 Tara, R 4.1.1, grass 7.8.4 11 | * win-builder (devel, release, oldrelease) 12 | 13 | ## R CMD check results 14 | There were no ERRORs or WARNINGs. 15 | 16 | There was one NOTE: 17 | * checking installed package size ... 18 | installed size is 9.4Mb 19 | sub-directories of 1Mb or more: 20 | extdata 9.0Mb 21 | 22 | The external data consists of GIS data file (shapes and raster) to enable helpful examples. 23 | 24 | Some examples (within donttest) take quite a while to run (> 5s) because they all contain the full 25 | workflow of loading and processing GIS files to be stand-alone examples. 26 | 27 | ## Downstream dependencies 28 | There are no downstream dependencies -------------------------------------------------------------------------------- /inst/extdata/nc/elev_ned_30m.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/elev_ned_30m.tif -------------------------------------------------------------------------------- /inst/extdata/nc/elev_ned_30m.tif.aux.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 55.12299659919739 6 | 156.4371268444061 7 | 1000 8 | 0 9 | 0 10 | 1|0|0|0|0|0|2|3|3|1|2|2|2|3|3|2|4|5|11|9|13|10|7|6|6|4|13|11|9|7|8|10|10|7|7|2|3|7|2|1|7|7|4|4|4|4|5|9|5|7|1|6|5|3|8|3|2|1|3|4|3|3|8|2|5|3|7|13|12|8|14|16|9|21|25|25|19|18|18|14|22|13|25|17|27|30|25|32|33|22|22|29|25|28|33|29|31|33|28|36|41|36|37|39|42|32|43|44|39|47|54|41|45|39|39|48|45|60|50|43|45|43|37|42|41|35|40|41|47|42|37|33|29|39|36|45|37|49|43|48|41|42|40|42|51|55|37|49|38|51|48|37|62|51|46|60|47|60|47|50|64|60|64|54|61|45|61|69|72|74|73|72|61|66|73|73|88|84|80|77|83|86|109|93|117|97|105|108|106|96|110|110|101|103|100|98|114|99|96|97|106|109|130|96|113|100|108|118|126|134|121|137|142|153|157|165|150|152|156|159|179|150|172|153|157|182|167|162|177|164|194|158|180|161|154|170|164|170|162|154|149|154|173|160|131|172|172|198|165|173|159|154|169|196|166|166|162|175|171|158|198|178|194|178|194|180|191|206|206|209|185|199|208|214|202|210|200|201|212|206|237|223|212|228|232|239|222|249|225|211|237|240|204|259|259|239|237|246|264|293|442|543|595|339|269|299|495|324|349|351|298|328|282|279|249|281|277|296|305|287|256|295|294|279|264|318|312|327|354|296|290|290|281|285|325|283|331|313|316|330|291|307|331|338|352|364|303|326|309|324|332|335|307|349|360|310|296|354|351|316|322|339|356|367|361|380|373|337|369|363|349|357|359|366|386|373|397|358|407|431|397|359|377|395|386|405|380|452|415|410|416|434|413|409|376|414|369|429|399|387|383|406|420|419|431|435|423|414|403|357|364|410|427|392|412|393|360|379|434|433|386|404|391|412|411|384|410|408|398|411|436|406|417|442|406|394|381|405|413|412|443|390|387|377|435|384|384|391|419|428|419|420|418|406|389|442|433|390|440|442|519|530|469|407|441|423|429|419|430|446|464|477|437|407|434|435|438|422|412|430|390|429|459|424|429|426|429|401|409|415|446|416|395|387|396|390|385|360|410|401|401|389|412|370|377|390|386|398|373|371|367|367|379|355|362|363|385|349|372|377|364|364|379|345|366|333|354|333|355|385|345|348|349|356|388|379|357|350|346|345|363|371|364|324|345|371|350|384|342|314|363|332|331|308|327|311|352|302|315|301|315|275|322|286|310|309|316|307|311|299|326|282|277|304|306|300|318|301|307|305|288|310|320|282|332|328|308|323|277|297|311|306|291|282|315|326|309|318|298|287|293|344|294|278|295|317|308|313|303|304|326|301|320|300|345|330|314|314|316|322|282|319|288|312|317|322|302|322|280|315|297|301|307|300|345|351|327|307|318|308|297|292|323|303|334|302|291|310|300|335|330|299|299|313|348|331|313|288|288|324|307|332|310|347|334|317|334|298|373|327|311|309|285|367|298|332|333|361|303|330|311|300|313|338|309|319|318|297|322|304|341|323|317|331|305|325|321|318|327|324|296|310|306|309|322|327|296|322|331|274|314|292|285|317|292|312|320|317|330|331|315|318|333|302|310|293|319|304|275|288|312|319|294|275|286|309|281|304|301|288|301|292|333|313|295|312|282|318|303|300|333|279|294|290|322|320|279|315|289|282|299|289|264|279|284|282|280|277|276|291|280|293|319|253|280|303|283|258|291|286|267|270|287|234|272|263|256|270|278|288|307|273|280|319|261|268|284|273|276|293|269|289|274|269|254|291|278|288|282|288|258|267|258|241|258|242|245|253|253|253|251|258|256|242|271|255|234|248|264|247|241|253|286|220|231|266|211|246|249|260|247|229|253|216|223|241|228|245|233|210|223|218|214|244|223|201|221|208|219|197|213|204|218|244|232|226|210|229|219|200|200|221|190|212|209|199|197|213|199|191|149|202|192|208|147|177|160|176|168|162|164|159|155|156|148|180|167|157|165|175|170|152|171|169|136|139|144|153|144|126|138|127|145|144|116|125|143|98|105|111|115|99|111|87|115|114|90|108|110|114|85|104|92|102|78|101|102|101|100|86|86|78|82|85|80|75|67|69|59|61|67|59|51|42|44|56|43|39|47|33|41|34|35|33|26|19|12|17|20|19|13|18|16|14|12|12|8|11|13|14|9|6|9|9|7|4|10|8|8|5|0|2|4|4|2|1|1|3|1|1 11 | 12 | 13 | 14 | 5 15 | 5.517360e+01 7.541619e+01 0 191 191 0 255 0 16 | 7.541619e+01 9.565877e+01 0 255 0 255 255 0 17 | 9.565877e+01 1.159014e+02 255 255 0 255 127 0 18 | 1.159014e+02 1.361439e+02 255 127 0 191 127 63 19 | 1.361439e+02 1.563865e+02 191 127 63 20 20 20 20 | GRASS GIS 7.0.4 21 | 156.38652038574 22 | 110.30757108714 23 | 55.173603057861 24 | 20.311997672653 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /inst/extdata/nc/geology.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/geology.dbf -------------------------------------------------------------------------------- /inst/extdata/nc/geology.prj: -------------------------------------------------------------------------------- 1 | PROJCS["Lambert_Conformal_Conic",GEOGCS["GCS_grs80",DATUM["D_North_American_1983",SPHEROID["Geodetic_Reference_System_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["standard_parallel_1",36.16666666666666],PARAMETER["standard_parallel_2",34.33333333333334],PARAMETER["latitude_of_origin",33.75],PARAMETER["central_meridian",-79],PARAMETER["false_easting",609601.22],PARAMETER["false_northing",0],UNIT["Meter",1]] -------------------------------------------------------------------------------- /inst/extdata/nc/geology.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/geology.shp -------------------------------------------------------------------------------- /inst/extdata/nc/geology.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/geology.shx -------------------------------------------------------------------------------- /inst/extdata/nc/lakes.cpg: -------------------------------------------------------------------------------- 1 | UTF-8 -------------------------------------------------------------------------------- /inst/extdata/nc/lakes.dbf: -------------------------------------------------------------------------------- 1 | _A idN 2 | 1 2 3 4 5 6 -------------------------------------------------------------------------------- /inst/extdata/nc/lakes.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] -------------------------------------------------------------------------------- /inst/extdata/nc/lakes.qpj: -------------------------------------------------------------------------------- 1 | GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]] 2 | -------------------------------------------------------------------------------- /inst/extdata/nc/lakes.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/lakes.shp -------------------------------------------------------------------------------- /inst/extdata/nc/lakes.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/lakes.shx -------------------------------------------------------------------------------- /inst/extdata/nc/landuse_r.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/landuse_r.tif -------------------------------------------------------------------------------- /inst/extdata/nc/obs_data.csv: -------------------------------------------------------------------------------- 1 | "","site_id","obs.time","val1","val2" 2 | "1",1,"2018-01-01",4.24463037625,13.7527186441046 3 | "2",1,"2018-03-01",5.04471394526138,13.3129858414697 4 | "3",1,"2018-07-01",4.76970203671057,10.7043476096993 5 | "4",4,"2018-01-01",5.00044755354386,9.41690283452135 6 | "5",4,"2018-03-01",4.0262591200863,13.0370512902919 7 | "6",4,"2018-07-01",4.94620614664538,14.2103259494348 8 | "7",5,"2018-01-01",3.47800186936337,11.3704621006304 9 | "8",5,"2018-03-01",3.65301388596865,15.3033188533589 10 | "9",5,"2018-07-01",2.88196325735764,13.3834467224701 11 | "10",7,"2018-01-01",2.88390661678557,16.0223447037962 12 | "11",7,"2018-03-01",3.13884669437701,14.7492659026699 13 | "12",7,"2018-07-01",3.85298142942178,13.5270045000065 14 | "13",8,"2018-01-01",4.45897332757634,13.8771276815885 15 | "14",8,"2018-03-01",3.69289275738432,11.2001123303916 16 | "15",8,"2018-07-01",3.53907300458513,10.9209809772721 17 | "16",11,"2018-01-01",5.75147036098756,13.4993636210895 18 | "17",11,"2018-03-01",5.66440942601811,16.034507720393 19 | "18",11,"2018-07-01",6.00724375636375,12.5408675970236 20 | "19",12,"2018-01-01",7.16743644202007,14.843721356863 21 | "20",12,"2018-03-01",5.93429460386342,11.7206637276434 22 | "21",12,"2018-07-01",5.32064474528572,14.0810318374133 23 | "22",15,"2018-01-01",6.4013082879423,12.9607079793713 24 | "23",15,"2018-03-01",6.37712768348743,17.1947710923474 25 | "24",15,"2018-07-01",7.03125637899568,12.8434623341325 26 | "25",16,"2018-01-01",5.45858494621072,13.6613145670772 27 | "26",16,"2018-03-01",5.49698122963598,14.7860905704373 28 | "27",16,"2018-07-01",6.68303533049208,12.7309255154163 29 | "28",17,"2018-01-01",5.95068287442078,14.201436846503 30 | "29",17,"2018-03-01",6.20803303888554,14.6625215928708 31 | "30",17,"2018-07-01",5.17802448070462,11.3866955418269 32 | "31",18,"2018-01-01",5.49175596180644,13.9814971862128 33 | "32",18,"2018-03-01",4.60149420975255,15.4734874273814 34 | "33",18,"2018-07-01",5.36258471321053,16.5084461778251 35 | "34",19,"2018-01-01",2.57990426816622,18.4376545250569 36 | "35",19,"2018-03-01",2.88661727786162,12.9798309799318 37 | "36",19,"2018-07-01",3.24890058158576,10.2424384189689 38 | "37",20,"2018-01-01",5.66490705727646,13.73349758842 39 | "38",20,"2018-03-01",4.79421455748756,11.3160273358919 40 | "39",20,"2018-07-01",5.10761820557713,13.4922864973169 41 | "40",21,"2018-01-01",8.01845182357112,12.2564922901554 42 | "41",21,"2018-03-01",6.61075244873718,11.7438910344893 43 | "42",21,"2018-07-01",7.06866107413424,11.329767766894 44 | "43",22,"2018-01-01",5.07089659254543,14.8235542275628 45 | "44",22,"2018-03-01",5.97342496203032,11.1799125019009 46 | "45",22,"2018-07-01",5.57303219745907,15.34463542616 47 | "46",23,"2018-01-01",4.09656176037676,12.0604868470132 48 | "47",23,"2018-03-01",3.05510467617967,14.7705265586317 49 | "48",23,"2018-07-01",2.91566900037106,14.052987302989 50 | "49",24,"2018-01-01",5.01840565690104,15.1100864102972 51 | "50",24,"2018-03-01",5.31030100622548,11.0354904497781 52 | "51",24,"2018-07-01",5.02853526310053,13.9136043167186 53 | "52",25,"2018-01-01",4.61073248054074,16.7536722601495 54 | "53",25,"2018-03-01",5.20749639331483,12.4529335623653 55 | "54",25,"2018-07-01",4.694308674717,17.9817671696663 56 | "55",26,"2018-01-01",6.3563834842793,14.4801399705276 57 | "56",26,"2018-03-01",5.9826236772739,11.0142731773351 58 | "57",26,"2018-07-01",4.22469130697797,13.1692029032694 59 | "58",28,"2018-01-01",5.34889631120914,15.2946651773135 60 | "59",28,"2018-03-01",5.56619326308708,11.7104752321007 61 | "60",28,"2018-07-01",5.44971046175762,10.4800206751222 62 | "61",30,"2018-01-01",4.10300491364984,11.7644153537958 63 | "62",30,"2018-03-01",3.88615677096859,13.3780022433628 64 | "63",30,"2018-07-01",3.83873751959076,12.4148053252003 65 | "64",31,"2018-01-01",6.56936753046795,13.2531234392487 66 | "65",31,"2018-03-01",6.3807760876766,17.2286173112877 67 | "66",31,"2018-07-01",6.70053878920262,15.2182863959872 68 | "67",32,"2018-01-01",6.6631619081488,14.074206181893 69 | "68",32,"2018-03-01",5.81300211041354,13.3076585238049 70 | "69",32,"2018-07-01",5.18261373277768,11.7105251618301 71 | "70",34,"2018-01-01",3.72570982288779,13.814923813624 72 | "71",34,"2018-03-01",4.92054300222959,15.2093735553482 73 | "72",34,"2018-07-01",3.40289549380649,11.685604416871 74 | "73",36,"2018-01-01",5.36373265534932,15.510166706544 75 | "74",36,"2018-03-01",4.19442219535189,12.9187412287312 76 | "75",36,"2018-07-01",5.5477622748949,10.4446838046175 77 | "76",37,"2018-01-01",4.90697213626808,12.5112096944053 78 | "77",37,"2018-03-01",4.09320382551628,12.7843875291574 79 | "78",37,"2018-07-01",4.4726858576967,11.3769275178703 80 | "79",38,"2018-01-01",5.00015259221129,14.7463322768967 81 | "80",38,"2018-03-01",4.49415513249569,14.8714025748049 82 | "81",38,"2018-07-01",3.77458724915903,14.5138758332084 83 | "82",40,"2018-01-01",6.09554504658435,18.52895879146 84 | "83",40,"2018-03-01",4.9385994085058,17.901154724865 85 | "84",40,"2018-07-01",4.53656622200411,9.58714213762084 86 | "85",41,"2018-01-01",4.44872223845649,11.6478303175084 87 | "86",41,"2018-03-01",5.38161948164008,12.0171993921765 88 | "87",41,"2018-07-01",4.35187242282609,13.0346688294987 89 | "88",43,"2018-01-01",5.84640519669898,12.6502342800532 90 | "89",43,"2018-03-01",5.39754641686754,14.8193164930294 91 | "90",43,"2018-07-01",4.76030696859385,12.7568921616693 92 | "91",44,"2018-01-01",4.61939963018199,14.076661268537 93 | "92",44,"2018-03-01",4.54080787454168,9.18461755900194 94 | "93",44,"2018-07-01",4.59323915955721,12.9792423770964 95 | "94",45,"2018-01-01",3.97625734673665,15.2219125723838 96 | "95",45,"2018-03-01",3.29167245471188,14.1620398285031 97 | "96",45,"2018-07-01",3.61867641698104,12.6297206337255 98 | "97",46,"2018-01-01",3.72299439247244,14.8483566673482 99 | "98",46,"2018-03-01",4.42417485474307,10.4270493885354 100 | "99",46,"2018-07-01",5.41143786520671,13.7024892084359 101 | "100",47,"2018-01-01",3.93665346964748,12.1134750010325 102 | "101",47,"2018-03-01",4.12115046357444,9.69308855306364 103 | "102",47,"2018-07-01",4.37137854729792,14.7183233274258 104 | "103",50,"2018-01-01",7.44718601268208,12.7274442171044 105 | "104",50,"2018-03-01",6.66872457128424,15.144063846078 106 | "105",50,"2018-07-01",6.68523558699815,14.1476410858802 107 | "106",52,"2018-01-01",5.1335691487188,13.8438500401207 108 | "107",52,"2018-03-01",5.1111372372893,14.1901894424839 109 | "108",52,"2018-07-01",5.3136468545887,13.4142807280824 110 | "109",53,"2018-01-01",5.0430176249258,14.7452380154266 111 | "110",53,"2018-03-01",4.4502420740126,12.8393399985602 112 | "111",53,"2018-07-01",5.24702177272446,11.2413375618481 113 | "112",55,"2018-01-01",7.21119451988121,11.4827515425015 114 | "113",55,"2018-03-01",8.35297661420869,17.4514098166967 115 | "114",55,"2018-07-01",7.29004752989597,11.7086374604891 116 | "115",56,"2018-01-01",4.42089814986137,12.2760211972775 117 | "116",56,"2018-03-01",5.20656985158692,11.2807034637298 118 | "117",56,"2018-07-01",4.66487307517876,13.2233267807624 119 | "118",58,"2018-01-01",5.09869853101356,13.2303720369355 120 | "119",58,"2018-03-01",4.08522846593498,11.9517890928114 121 | "120",58,"2018-07-01",4.68753112180454,10.4894944662354 122 | "121",59,"2018-01-01",5.61390289588056,14.6448265831797 123 | "122",59,"2018-03-01",5.81684674605746,12.4085948769804 124 | "123",59,"2018-07-01",6.25921930015479,15.6348462786032 125 | "124",61,"2018-01-01",5.84496804028675,12.1148167462234 126 | "125",61,"2018-03-01",5.08203642234022,14.1193741960746 127 | "126",61,"2018-07-01",6.43410357853345,12.7964474055287 128 | "127",62,"2018-01-01",4.83547928767073,11.6332456013731 129 | "128",62,"2018-03-01",5.42129003938563,13.4560284958745 130 | "129",62,"2018-07-01",4.90803428090224,15.9286426505012 131 | "130",63,"2018-01-01",3.02554045899168,16.5104510493188 132 | "131",63,"2018-03-01",3.55123075157282,15.8356094587728 133 | "132",63,"2018-07-01",2.96134725069852,10.4866350177765 134 | "133",66,"2018-01-01",3.72570558716247,13.7262481037589 135 | "134",66,"2018-03-01",3.39284314355896,14.4407552918293 136 | "135",66,"2018-07-01",4.31384783943553,13.3250710652098 137 | "136",67,"2018-01-01",5.90914584449762,10.633042368393 138 | "137",67,"2018-03-01",6.23023198029491,13.1695421885382 139 | "138",67,"2018-07-01",6.43359702910624,13.4862965490036 140 | "139",69,"2018-01-01",5.3424124561766,13.3730353638062 141 | "140",69,"2018-03-01",5.048793801253,13.3848568560309 142 | "141",69,"2018-07-01",4.88709460994451,13.6058343867641 143 | "142",70,"2018-01-01",3.96640172326378,11.6225071320437 144 | "143",70,"2018-03-01",3.99582631681334,12.3054310980386 145 | "144",70,"2018-07-01",4.03339238364565,12.7675201742838 146 | "145",71,"2018-01-01",2.7189419197057,11.9292484531681 147 | "146",71,"2018-03-01",2.81476785178775,12.0475894820457 148 | "147",71,"2018-07-01",3.4096979996982,11.1321697313824 149 | "148",72,"2018-01-01",5.62821479969385,16.5451624714371 150 | "149",72,"2018-03-01",5.78670032502296,11.9108877415606 151 | "150",72,"2018-07-01",5.08611123044679,12.3157278574557 152 | "151",73,"2018-01-01",4.71102998262776,15.0568917613733 153 | "152",73,"2018-03-01",4.10722148079322,14.2478280648405 154 | "153",73,"2018-07-01",5.05319264669812,8.67099023637251 155 | "154",74,"2018-01-01",3.91289854464901,17.3861814362764 156 | "155",74,"2018-03-01",4.89079843980913,14.0982826215501 157 | "156",74,"2018-07-01",5.10817218713883,10.5912834818991 158 | "157",76,"2018-01-01",4.74220154382407,14.0601551844582 159 | "158",76,"2018-03-01",4.82278555169203,13.8755313595473 160 | "159",76,"2018-07-01",4.35423893832518,13.6586506010033 161 | "160",77,"2018-01-01",3.88925575404616,9.90922013498571 162 | "161",77,"2018-03-01",4.93512537757997,13.8885818860156 163 | "162",77,"2018-07-01",5.47660708059249,12.7518640174201 164 | "163",78,"2018-01-01",4.05854985394336,12.8709562700182 165 | "164",78,"2018-03-01",5.41345247255935,16.2501195146511 166 | "165",78,"2018-07-01",4.60122299569454,12.1548056262411 167 | "166",79,"2018-01-01",5.01379580485078,10.3567095747663 168 | "167",79,"2018-03-01",4.88703447046385,14.5646471449035 169 | "168",79,"2018-07-01",4.44688806965339,11.6194089880081 170 | "169",80,"2018-01-01",5.68487389852675,8.78259217038927 171 | "170",80,"2018-03-01",6.39385640525624,11.9864312604554 172 | "171",80,"2018-07-01",6.64804619278475,11.8517575222185 173 | "172",81,"2018-01-01",3.32833907702139,12.9504300097571 174 | "173",81,"2018-03-01",3.64542884479296,12.9985883582174 175 | "174",81,"2018-07-01",4.20903941370939,9.64082965392378 176 | "175",84,"2018-01-01",5.35130307135614,12.5030988405172 177 | "176",84,"2018-03-01",5.21952075165462,13.7084581701304 178 | "177",84,"2018-07-01",4.35400322059981,11.6286179330403 179 | "178",85,"2018-01-01",5.60943895508628,13.7198359292618 180 | "179",85,"2018-03-01",4.15285564724442,14.4268999522734 181 | "180",85,"2018-07-01",5.27072948722944,13.4460661047705 182 | -------------------------------------------------------------------------------- /inst/extdata/nc/pointsources.dbf: -------------------------------------------------------------------------------- 1 | va[idN 2 | psourceCP 1some text 2text 3more text 4other text -------------------------------------------------------------------------------- /inst/extdata/nc/pointsources.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] -------------------------------------------------------------------------------- /inst/extdata/nc/pointsources.qpj: -------------------------------------------------------------------------------- 1 | GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]] 2 | -------------------------------------------------------------------------------- /inst/extdata/nc/pointsources.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/pointsources.shp -------------------------------------------------------------------------------- /inst/extdata/nc/pointsources.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/pointsources.shx -------------------------------------------------------------------------------- /inst/extdata/nc/sites_nc.dbf: -------------------------------------------------------------------------------- 1 | w<aWsite_idC 2 | valueN 3 | 1 1 4 1 5 1 7 1 8 1 11 2 12 3 15 4 16 5 17 5 18 6 19 4 20 6 21 7 22 8 23 8 24 9 25 10 26 8 28 8 30 10 31 9 32 1 34 4 36 4 37 3 38 1 40 1 41 8 43 7 44 7 45 8 46 1 47 1 50 2 52 9 53 3 55 3 56 2 58 10 59 1 61 1 62 8 63 7 66 1 67 1 69 6 70 2 71 8 72 3 73 1 74 3 76 4 77 1 78 6 79 6 80 7 81 3 84 10 85 3 -------------------------------------------------------------------------------- /inst/extdata/nc/sites_nc.prj: -------------------------------------------------------------------------------- 1 | PROJCS["NAD83_HARN_North_Carolina",GEOGCS["GCS_grs80",DATUM["D_North_American_1983_HARN",SPHEROID["Geodetic_Reference_System_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["standard_parallel_1",36.16666666666666],PARAMETER["standard_parallel_2",34.33333333333334],PARAMETER["latitude_of_origin",33.75],PARAMETER["central_meridian",-79],PARAMETER["false_easting",609601.22],PARAMETER["false_northing",0],UNIT["Meter",1]] -------------------------------------------------------------------------------- /inst/extdata/nc/sites_nc.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/sites_nc.shp -------------------------------------------------------------------------------- /inst/extdata/nc/sites_nc.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/sites_nc.shx -------------------------------------------------------------------------------- /inst/extdata/nc/streams.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/streams.dbf -------------------------------------------------------------------------------- /inst/extdata/nc/streams.prj: -------------------------------------------------------------------------------- 1 | PROJCS["NAD83_HARN_North_Carolina",GEOGCS["GCS_NAD83(HARN)",DATUM["D_North_American_1983_HARN",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["standard_parallel_1",36.16666666666666],PARAMETER["standard_parallel_2",34.33333333333334],PARAMETER["latitude_of_origin",33.75],PARAMETER["central_meridian",-79],PARAMETER["false_easting",609601.22],PARAMETER["false_northing",0],UNIT["Meter",1]] -------------------------------------------------------------------------------- /inst/extdata/nc/streams.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/streams.shp -------------------------------------------------------------------------------- /inst/extdata/nc/streams.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MiKatt/openSTARS/09b36612720151a857e0fc60bffd9969393c2d30/inst/extdata/nc/streams.shx -------------------------------------------------------------------------------- /man/assign_binIDs.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_binary.R, R/export_ssn.R 3 | \name{assign_binIDs} 4 | \alias{assign_binIDs} 5 | \title{assign_binIDs 6 | Recursive function to assign binary id to stream segments} 7 | \usage{ 8 | assign_binIDs(dt, id, binID, lastbit) 9 | 10 | assign_binIDs(dt, id, binID, lastbit) 11 | } 12 | \arguments{ 13 | \item{id:}{stream segment} 14 | 15 | \item{binID:}{binary ID} 16 | 17 | \item{lastbit:}{last char to be added (0 or 1)} 18 | } 19 | \description{ 20 | Should be run for all outlets in the network ( = most downstream segments) and fills the binID for all segments 21 | 22 | This function is run for all outlets in the network ( = most downstream segments) 23 | and fills the binID for all segments. It is called by \code{calc_binary} in 24 | \code{export_ssn} and should not be called by the user. 25 | } 26 | \keyword{internal} 27 | -------------------------------------------------------------------------------- /man/calcCatchmArea_assignNetID.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_edges.R 3 | \name{calcCatchmArea_assignNetID} 4 | \alias{calcCatchmArea_assignNetID} 5 | \title{Calculate total catchment area of a stream segment and assign a network id.} 6 | \usage{ 7 | calcCatchmArea_assignNetID(dt, id, net_ID) 8 | } 9 | \arguments{ 10 | \item{dt}{data.table containing the attributes of the stream segments} 11 | 12 | \item{id}{integer; 'stream' of the stream segment} 13 | 14 | \item{netID}{integer; network ID} 15 | } 16 | \value{ 17 | Total catchment area upstream of the segment 18 | } 19 | \description{ 20 | Recursive function to calculate the upstream area of each stream segment and 21 | assign a unique network id. It is called by \code{\link{calc_edges}} for each 22 | outlet and should not be called by the user. 23 | } 24 | \details{ 25 | calcCatchmArea_assignNetID 26 | } 27 | \author{ 28 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 29 | } 30 | \keyword{internal} 31 | -------------------------------------------------------------------------------- /man/calc_attributes_edges.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_edges.R 3 | \name{calc_attributes_edges} 4 | \alias{calc_attributes_edges} 5 | \title{Calculate attributes of the edges.} 6 | \usage{ 7 | calc_attributes_edges( 8 | input_raster = NULL, 9 | stat_rast = NULL, 10 | attr_name_rast = NULL, 11 | input_vector = NULL, 12 | stat_vect = NULL, 13 | attr_name_vect = NULL, 14 | round_dig = 2 15 | ) 16 | } 17 | \arguments{ 18 | \item{input_raster}{name(s) of raster map(s) to calculate attributes from.} 19 | 20 | \item{stat_rast}{name(s) giving the statistics to be calculated, 21 | from the raster maps, must be one of: "min", "max", "mean", "sum", "percent", "area" 22 | for each \code{input_raster}.} 23 | 24 | \item{attr_name_rast}{of new column name(s) for the attribute(s) 25 | to be calculated. Attribute names must not be longer than 8 characters as ESRI shapefiles 26 | cannot have colum names with more than 10 characters. See notes.} 27 | 28 | \item{input_vector}{name(s) of vector map(s) to calculate attributes from.} 29 | 30 | \item{stat_vect}{name(s) giving the statistics to be calculated 31 | from the vector maps, must be one of: "count" (for point data), "percent" or "area" 32 | (for polygon data) for each \code{input_vector}.} 33 | 34 | \item{attr_name_vect}{name(s) of attribute column(s), case sensitive. For polygon data, this is 35 | the column to calculate the statistics from; the results column names are 36 | created by the content of this column. For point data, a column will be created 37 | with this name to hold the counts. See notes.} 38 | 39 | \item{round_dig}{integer; number of digits to round results to. Can be a vector 40 | of different values or just one value for all attributes. 41 | #@param clean logical; should intermediate files be deleted} 42 | } 43 | \value{ 44 | Nothing. The function appends new columns to the 'edges' attribute 45 | table with column names given in \code{attr_name_rast} and derived from the attribute classes for 46 | vector data. For each attribute, two columns are appended: one giving the attribute for the rca of the edge 47 | ("attribute_name_e") and one for the attribute of the total catchment of 48 | the edge ("attribute_name_c"). 49 | } 50 | \description{ 51 | For each edge (i.e. stream segment) additional attributes (potential predictor 52 | variables) are derived based on given raster or vector maps. 53 | } 54 | \details{ 55 | First, the reach contributing areas (= subcatchments) for all edges are calculated. 56 | Then these are intersected with the given raster and/or vector maps and the desired 57 | statistics are computed. 58 | This function must be run before computing approximate attribute values for 59 | sites \code{\link{calc_attributes_sites_approx}}. 60 | 61 | For \code{stat_rast} = "percent" or "area" the \code{input_raster} can be either coded as 1 and 0 62 | (e.g., cells occupied by the land use under consideration and not) or as different classes. 63 | The percentage or area of each class in the catchment is calculated. If 64 | the \code{input_raster} consists of percentages per cell (e.g., proportional land 65 | use of a certain type per cell) \code{stat_rast} = "mean" gives the overall proportion 66 | of this land use in the catchment. 67 | 68 | For \code{stat_vect} = "percent" or "area" \code{input_vector} must contain polygons of 69 | e.g. different land use types. The column \code{attr_name_vect} would then 70 | give the code for the different land uses. Then, the percentage for each land 71 | use type in the catchment of the edge is calculated and given in separate columns 72 | with column names resampling the different categories given in column 73 | \code{attr_name_vect} 74 | 75 | For \code{stat_vect} = "count" \code{input_vector} must contain points of 76 | e.g. waste water treatment plants. The column \code{attr_name_vect} gives the 77 | name of the column to hold the count value, e.g. nWWTP. 78 | 79 | Both raster and vector maps to be used must be read in to the GRASS session, 80 | either in \code{\link{import_data}} or using the GRASS function r.in.rast or 81 | v.in.ogr (see examples). 82 | } 83 | \note{ 84 | Column names for the results are created as follows: 85 | Raster data - the column names given in \code{attr_name_rast} are used. The user should 86 | take care to use unique, clear names. For \code{stat_rast} = 'percentage' or 'area', 87 | the output column name will be concatenated 'p' or 'a', respectively. 88 | For vector data, column names are constructed from the entries in in the column 89 | \code{attr_name_vect}. For counts of points, the new column name containing the counts 90 | is just the given name. For polygon data ('percentage' or 'area'), the names are constructed using 91 | the unique entries of the column with a concatenated 'p' or 'a', respectively. If, for instance, 92 | for a landuse vector containing the classes 'urban' and 'arable' percentages would be calculated, 93 | edges would contain two new columns 'urbanp' and 'arablep'. 94 | 95 | \code{\link{setup_grass_environment}}, \code{\link{import_data}}, 96 | \code{\link{derive_streams}} and \code{\link{calc_edges}} must be run before. 97 | } 98 | \examples{ 99 | \donttest{ 100 | # Initiate and setup GRASS 101 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 102 | if(.Platform$OS.type == "windows"){ 103 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 104 | } else { 105 | grass_program_path = "/usr/lib/grass78/" 106 | } 107 | 108 | setup_grass_environment(dem = dem_path, 109 | gisBase = grass_program_path, 110 | remove_GISRC = TRUE, 111 | override = TRUE 112 | ) 113 | gmeta() 114 | 115 | # Load files into GRASS 116 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 117 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 118 | streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 119 | preds_v_path <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS") 120 | preds_r_path <- system.file("extdata", "nc", "landuse_r.tif", package = "openSTARS") 121 | 122 | import_data(dem = dem_path, sites = sites_path, streams = streams_path, 123 | predictor_vector = preds_v_path, predictor_raster = preds_r_path) 124 | 125 | # Derive streams from DEM 126 | # burn in 'streams' 10 meters 127 | derive_streams(burn = 10, accum_threshold = 700, condition = TRUE, clean = TRUE) 128 | 129 | # Check and correct complex confluences (there are no complex confluences in this 130 | # example date set; set accum_threshold in derive_streams to a smaller value 131 | # to create complex confluences) 132 | cj <- check_compl_confluences() 133 | if(cj){ 134 | correct_compl_confluences() 135 | } 136 | 137 | # calculate slope as potential predictor 138 | execGRASS("r.slope.aspect", flags = c("overwrite","quiet"), 139 | parameters = list( 140 | elevation = "dem", 141 | slope = "slope" 142 | )) 143 | 144 | # Prepare edges 145 | calc_edges() 146 | calc_attributes_edges(input_raster = c("slope", "landuse_r"), 147 | stat_rast = c("max", "percent"), 148 | attr_name_rast = c("maxSlo", "luse"), 149 | input_vector = "pointsources", stat_vect = "count", 150 | attr_name_vect = "psource") 151 | 152 | # Plot eges with percentage of forest in the catchment (lusep_5) as line width 153 | edges <- readVECT('edges', ignore.stderr = TRUE) 154 | head(edges@data) 155 | lu <- readRAST("landuse_r", ignore.stderr = TRUE, plugin = FALSE) 156 | 157 | # plot landuse data 158 | library(raster) 159 | op <- par() 160 | par(xpd = FALSE) 161 | plot(raster(lu), legend = FALSE, xaxt = "n", yaxt = "n", bty = "n", 162 | col = adjustcolor(c("red", "goldenrod", "green", "forestgreen", 163 | "darkgreen", "blue", "lightblue"), alpha.f = 0.7)) 164 | par(xpd = TRUE) 165 | legend("bottom", cex = 0.75, 166 | legend = c("developed", "agriculture", "herbaceous", "shrubland", "forest", "water", "sediment"), 167 | fill = c("red", "goldenrod", "green", "forestgreen","darkgreen", "blue", "lightblue"), 168 | horiz = TRUE, inset = -0.175) 169 | # line width is relative to the area of land use class 5 (forest) in the rca of the edge segment 170 | plot(edges, lwd = edges$lusep_5_c * 10, add = TRUE) 171 | par <- op 172 | } 173 | } 174 | \author{ 175 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 176 | } 177 | -------------------------------------------------------------------------------- /man/calc_attributes_sites_approx.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_sites_approx.R 3 | \name{calc_attributes_sites_approx} 4 | \alias{calc_attributes_sites_approx} 5 | \title{Calculate attributes of the sites.} 6 | \usage{ 7 | calc_attributes_sites_approx( 8 | sites_map = "sites", 9 | input_attr_name = NULL, 10 | output_attr_name = NULL, 11 | stat = NULL, 12 | round_dig = 2, 13 | calc_basin_area = TRUE, 14 | overwrite = FALSE 15 | ) 16 | } 17 | \arguments{ 18 | \item{sites_map}{character; name of the sites (observation or prediction) 19 | attributes shall be calculated for. "sites" (default) refers to the observation sites.} 20 | 21 | \item{input_attr_name}{character vector; input column name(s) in the 22 | attribute table of the vector map "edges".} 23 | 24 | \item{output_attr_name}{character vector (optional); output column name(s) 25 | appended to the site attribute data table. If not provided it is set to 26 | \code{input_attr_name}. Attribute names must not be longer than 10 27 | characters.} 28 | 29 | \item{stat}{name or character vector giving the statistics to be calculated. 30 | See details below.} 31 | 32 | \item{round_dig}{integer; number of digits to round results to.} 33 | 34 | \item{calc_basin_area}{boolean; shall the catchment area be calculated? (Useful 35 | to set to FALSE if the function has been called before.)} 36 | 37 | \item{overwrite}{boolean; shall existing columns be overwritten; defaults to FALSE} 38 | } 39 | \value{ 40 | Nothing. The function appends new columns to the \code{sites_map} 41 | attribute table 42 | \itemize{ 43 | \item{'H2OAreaA':} {Total watershed area of the watershed upstream of each site.} 44 | \item{attr_name:} {Additional optional attributes calculated based on \code{input_attr_name}.} 45 | } 46 | } 47 | \description{ 48 | For each site (observations or predictions) attributes (potential predictor variables) 49 | are derived based on the values calculated for the edge the site lies on. 50 | This function calculates approximate values for site catchments as described 51 | in Peterson & Ver Hoef, 2014: STARS: An ArcGIS Toolset Used to Calculate the 52 | Spatial Information Needed to Fit Spatial Statistical Models to Stream 53 | Network Data. J. Stat. Softw., 56 (2). 54 | } 55 | \details{ 56 | The approximate total catchment area (H2OAreaA) is always calculated 57 | if \code{calc_basin_area} is TRUE. If \code{stat} is one of 58 | "min", "max", "mean" or "percent" the function assigns the value of the edge the site lies on. 59 | Otherwise, the value is calculated as the sum of all edges upstream of the previous 60 | confluence and the proportional value of the edge the site lies on (based on 61 | the distance ratio 'ratio'); this is useful e.g. for counts of dams or waste water 62 | treatment plants or total catchment area. 63 | 64 | \code{input_attr_name} must give the column names of the edges attribute table 65 | for that the statistics should be calculated. 66 | } 67 | \note{ 68 | \code{\link{import_data}}, \code{\link{derive_streams}}, 69 | \code{\link{calc_edges}}, \code{\link{calc_sites}} or 70 | \code{\link{calc_prediction_sites}} and \code{\link{calc_attributes_edges}} 71 | must be run before. 72 | } 73 | \examples{ 74 | \donttest{ 75 | # Initiate and setup GRASS 76 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 77 | if(.Platform$OS.type == "windows"){ 78 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 79 | } else { 80 | grass_program_path = "/usr/lib/grass78/" 81 | } 82 | 83 | setup_grass_environment(dem = dem_path, 84 | gisBase = grass_program_path, 85 | remove_GISRC = TRUE, 86 | override = TRUE 87 | ) 88 | gmeta() 89 | 90 | # Load files into GRASS 91 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 92 | preds_v_path <- system.file("extdata", "nc", "geology.shp", package = "openSTARS") 93 | import_data(dem = dem_path, sites = sites_path, predictor_vector = preds_v_path,) 94 | 95 | # Derive streams from DEM 96 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 97 | 98 | # Check and correct complex confluences (there are no complex confluences in this 99 | # example date set; set accum_threshold in derive_streams to a smaller value 100 | # to create complex confluences) 101 | cj <- check_compl_confluences() 102 | if(cj){ 103 | correct_compl_confluences() 104 | } 105 | 106 | # Prepare edges 107 | calc_edges() 108 | 109 | # Derive slope from the DEM as an example raster map to calculate attributes from 110 | execGRASS("r.slope.aspect", flags = c("overwrite","quiet"), 111 | parameters = list( 112 | elevation = "dem", 113 | slope = "slope" 114 | )) 115 | calc_attributes_edges(input_raster = "slope", stat_rast = "max", attr_name_rast = "maxSlo", 116 | input_vector = "geology", stat_vect = "percent", attr_name_vect = "GEO_NAME") 117 | 118 | calc_sites() 119 | 120 | # approximate potential predictor variables for each site based on edge values 121 | calc_attributes_sites_approx( 122 | input_attr_name = c('maxSlo', 'CZamp', 'CZbgp', 'CZfgp', 'CZgp', 'CZigp', 'CZlgp', 'CZvep', 'Kmp'), 123 | stat = c("max", rep("percent", 8))) 124 | 125 | # plot share of a certain geology in the sampling point's catchment as 126 | # point size 127 | library(sp) 128 | edges <- readVECT('edges', ignore.stderr = TRUE) 129 | sites <- readVECT('sites', ignore.stderr = TRUE) 130 | geo <- readVECT("geology", ignore.stderr = TRUE) 131 | plot(geo, col = adjustcolor(1:8, alpha.f = 0.5)[as.factor(geo$GEO_NAME)]) 132 | plot(edges, col = "blue", add = TRUE) 133 | plot(sites, col = 1, add = TRUE, pch = 19, cex = (sites$CZbgp + 0.15) * 2) 134 | legend("left", col = adjustcolor(1:8, alpha.f = 0.5), bty = "n", 135 | legend = unique(geo$GEO_NAME), pch = 15, title = "geology") 136 | legend("right", col = 1, pch = 19, legend = seq(0, 1, 0.2), bty = "n", 137 | title = "share CZbg\nin catchment", pt.cex = (seq(0, 1, 0.2) + 0.15) * 2) 138 | } 139 | 140 | } 141 | \author{ 142 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 143 | } 144 | -------------------------------------------------------------------------------- /man/calc_attributes_sites_exact.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_sites_exact.R 3 | \name{calc_attributes_sites_exact} 4 | \alias{calc_attributes_sites_exact} 5 | \title{Calculate attributes of the sites.} 6 | \usage{ 7 | calc_attributes_sites_exact( 8 | sites_map = "sites", 9 | input_raster = NULL, 10 | stat_rast = NULL, 11 | attr_name_rast = NULL, 12 | input_vector = NULL, 13 | stat_vect = NULL, 14 | attr_name_vect = NULL, 15 | round_dig = 2, 16 | calc_basin_area = TRUE, 17 | keep_basins = FALSE, 18 | overwrite = FALSE 19 | ) 20 | } 21 | \arguments{ 22 | \item{sites_map}{character; name of the sites (observation or prediction) 23 | attributes shall be calculated for. "sites" (default) refers to the observation sites.} 24 | 25 | \item{input_raster}{character vector (optional); name of additional raster 26 | maps to calculate attributes from.} 27 | 28 | \item{stat_rast}{character vector (optional); statistics to be calculated, one of: 29 | "min", "max", "mean", "stddev", "variance", "sum", "median", "percent", "area" or "percentile_X" (where X 30 | gives the desired percentile e.g. 25 for the first). Must be provided if 31 | \code{input_raster} are given.} 32 | 33 | \item{attr_name_rast}{character vector (optional); column name for the attributes 34 | to be calculated. Attribute names must not be longer than 10 characters. 35 | Must be provided if \code{input_raster} are given.} 36 | 37 | \item{input_vector}{character string vector (optional); name of additional vector 38 | maps to calculate attributes from.} 39 | 40 | \item{stat_vect}{character string vector (optional); statistics to be calculated, 41 | one of: "percent", "area" or "count." Must be provided if \code{input_vector} is given.} 42 | 43 | \item{attr_name_vect}{character string vector (optional); column name(s) in 44 | the vector file provided to calculate the attributes from (if \code{input_vector} 45 | is a polygon map and stat_vect is "percent") or giving the new name attributes 46 | to calculate (if \code{input_vector} is a point map and stat_vect is "count". 47 | Must be provided if \code{input_vector} is given.} 48 | 49 | \item{round_dig}{integer; number of digits to round results to. Can be a vector 50 | of different values or just one value for all attributes.} 51 | 52 | \item{calc_basin_area}{boolean; shall the catchment area be calculated? (Useful 53 | to set to FALSE if the function has been called before with \code{keep_basins = TRUE}.)} 54 | 55 | \item{keep_basins}{boolean; shall raster and vector maps of all the watersheds be kept? 56 | Defaults to FALSE.} 57 | 58 | \item{overwrite}{boolean; shall existing columns be overwritten; defaults to FALSE} 59 | } 60 | \value{ 61 | Nothing. The function appends new columns to the \code{sites_map} attribute table 62 | \itemize{ 63 | \item{'H2OArea':} {Total watershed area of the watershed upstream of each site.} 64 | \item{attr_name_rast:} {Additional optional attributes calculated based on input_raster maps.} 65 | \item{attributes form vector maps:}{Additional optional attributes 66 | calculated based on input_vector maps. The column names are based on the unique entries 67 | of the column(s) given in \code{attr_name_vect}.} 68 | } 69 | Please note that for sampling points that lie in the same DEM raster cell 70 | along a stream identical values are calculated because identical watersheds 71 | are derived. 72 | } 73 | \description{ 74 | For each site (observation or prediction) the total catchment area is 75 | calculated ('H2OArea'). Additionally, other attributes (predictor variables) 76 | can be derived based on given raster or vector maps. This function calculates 77 | exact values for catchments derived with 78 | \href{https://grass.osgeo.org/grass78/manuals/addons/r.stream.basins.html}{r.stream.basins} 79 | and can take considerable time if there are many sites. 80 | Catchment raster maps can optionally be stored as "sitename_catchm_X" (X = locID). 81 | } 82 | \note{ 83 | \code{\link{import_data}}, \code{\link{derive_streams}}, 84 | \code{\link{calc_edges}} and \code{\link{calc_sites}} or 85 | \code{\link{calc_prediction_sites}} must be run before. 86 | 87 | If \code{calc_basin_area = F} but there are no raster maps called 'sitename_catchm_x' 88 | with x = locID of all sites the catchments (and their area) are derived. 89 | } 90 | \examples{ 91 | \donttest{ 92 | # Initiate and setup GRASS 93 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 94 | if(.Platform$OS.type == "windows"){ 95 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 96 | } else { 97 | grass_program_path = "/usr/lib/grass78/" 98 | } 99 | 100 | setup_grass_environment(dem = dem_path, 101 | gisBase = grass_program_path, 102 | remove_GISRC = TRUE, 103 | override = TRUE 104 | ) 105 | gmeta() 106 | 107 | # Load files into GRASS 108 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 109 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 110 | import_data(dem = dem_path, sites = sites_path) 111 | 112 | # Derive streams from DEM 113 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 114 | 115 | # Prepare edges 116 | calc_edges() 117 | 118 | # caluclate slope as potential predictor 119 | execGRASS("r.slope.aspect", flags = c("overwrite","quiet"), 120 | parameters = list( 121 | elevation = "dem", 122 | slope = "slope" 123 | )) 124 | calc_attributes_edges(input_raster = "slope", stat_rast = "max", attr_name_rast = "maxSlo") 125 | 126 | # Prepare sites 127 | calc_sites() 128 | calc_attributes_sites_approx(input_attr_name = "maxSlo", output_attr_name = "maxSloA", stat = "max") 129 | calc_attributes_sites_exact(input_raster = "slope", attr_name_rast = "maxSloE", stat_rast = "max") 130 | 131 | # Plot data 132 | library(sp) 133 | dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 134 | edges <- readVECT('edges', ignore.stderr = TRUE) 135 | sites <- readVECT('sites', ignore.stderr = TRUE) 136 | plot(dem, col = gray(seq(0,1,length.out=20))) 137 | mm <- range(c(edges$maxSlo_e, sites$maxSloA, sites$maxSloE)) 138 | b <- seq(from = mm[1], to = mm[2] + diff(mm) * 0.01, length.out = 10) 139 | c_ramp <- colorRampPalette(c("white", "blue", "orange", "red")) 140 | cols <- c_ramp(length(b))[as.numeric(cut(edges$maxSlo_e, breaks = b, right = FALSE))] 141 | # plot stream edges, color depending on maxSlope of the edge 142 | plot(edges,col = cols, lwd = 2, add = TRUE) 143 | cols <- c_ramp(length(b))[as.numeric(cut(sites$maxSloA,breaks = b,right = FALSE))] 144 | # plot sites as points with color corresponding to maxSlop approximate 145 | plot(sites, pch = 19, col = cols, cex = 2, add = TRUE) 146 | cols <- c_ramp(length(b))[as.numeric(cut(sites$maxSloE,breaks = b,right = FALSE))] 147 | #' # plot sites as ring around points with color corresponding to maxSlop exact 148 | plot(sites, pch = 21, bg = cols, cex = 1.1, add = TRUE) 149 | # Some points in the lower centre of the map indicate a difference in max slope between 150 | # approximate and exact calculation (different colors for inner and outer points). However, 151 | # for most points the values are similar 152 | } 153 | } 154 | \author{ 155 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net}, 156 | Eduard Szoecs, \email{eduardszoecs@gmail.com} 157 | } 158 | -------------------------------------------------------------------------------- /man/calc_binary.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_binary.R, R/export_ssn.R 3 | \name{calc_binary} 4 | \alias{calc_binary} 5 | \title{Calculate binary IDs for each stream network.} 6 | \usage{ 7 | calc_binary() 8 | 9 | calc_binary() 10 | } 11 | \value{ 12 | A list with one slot for each network id containing a data frame 13 | with 'rid' and 'binaryID' for each segment belonging to this network. 14 | 15 | A list with one slot for each network id containing a data frame 16 | with 'rid' and 'binaryID' for each segment belonging to this network. 17 | } 18 | \description{ 19 | Calculate binary IDs for each stream network built up by '0' and '1'. 20 | This function is called by \code{\link{export_ssn}} and there is no need for it 21 | be called by the users. 22 | 23 | Calculate binary IDs for each stream network built up by '0' and '1'. 24 | This function is called by \code{\link{export_ssn}} and there is no need for it 25 | be called by the users. 26 | } 27 | \note{ 28 | \code{\link{import_data}}, \code{\link{derive_streams}}, 29 | \code{\link{calc_edges}} and \code{\link{calc_sites}} must be run before. 30 | 31 | \code{\link{import_data}}, \code{\link{derive_streams}}, 32 | \code{\link{calc_edges}} and \code{\link{calc_sites}} must be run before. 33 | } 34 | \examples{ 35 | \donttest{ 36 | # Initiate and setup GRASS 37 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 38 | if(.Platform$OS.type == "windows"){ 39 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 40 | } else { 41 | grass_program_path = "/usr/lib/grass78/" 42 | } 43 | 44 | setup_grass_environment(dem = dem_path, 45 | gisBase = grass_program_path, 46 | remove_GISRC = TRUE, 47 | override = TRUE 48 | ) 49 | gmeta() 50 | 51 | # Load files into GRASS 52 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 53 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 54 | streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 55 | preds_v_path <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS") 56 | preds_r_path <- system.file("extdata", "nc", "landuse_r.tif", package = "openSTARS") 57 | 58 | import_data(dem = dem_path, sites = sites_path, streams = streams_path, 59 | predictor_vector = preds_v_path, predictor_raster = preds_r_path) 60 | 61 | # Derive streams from DEM 62 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 63 | 64 | # Check and correct complex confluences (there are no complex confluences in this 65 | # example date set; set accum_threshold in derive_streams to a smaller value 66 | # to create complex confluences) 67 | cj <- check_compl_confluences() 68 | if(cj){ 69 | correct_compl_confluences() 70 | } 71 | 72 | # Prepare edges 73 | calc_edges() 74 | 75 | # Prepare site 76 | calc_sites() 77 | 78 | binaries <- calc_binary() 79 | head(binaries[[1]]) 80 | } 81 | } 82 | \author{ 83 | Eduard Szoecs, \email{eduardszoecs@gmail.com}; Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 84 | 85 | Eduard Szoecs, \email{eduardszoecs@gmail.com}; Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 86 | 87 | @export 88 | } 89 | -------------------------------------------------------------------------------- /man/calc_catchment_attributes_rast.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_edges.R 3 | \name{calc_catchment_attributes_rast} 4 | \alias{calc_catchment_attributes_rast} 5 | \title{calc_catchment_attributes_rast 6 | Aggregate attributes for the total catchment of each stream segment.} 7 | \usage{ 8 | calc_catchment_attributes_rast(dt, stat_rast, attr_name_rast, round_dig) 9 | } 10 | \arguments{ 11 | \item{dt}{data.table of stream topology and attributes per segment.} 12 | 13 | \item{stat_rast}{name or character vector giving the statistics to be calculated, 14 | must be one of: min, max, mean, percent, sum.} 15 | 16 | \item{attr_name_rast}{name or character vector of column names for the attribute(s) 17 | to be calculated.} 18 | 19 | \item{round_dig}{integer; number of digits to round results to. Can be a vector 20 | of different values or just one value for all attributes.} 21 | } 22 | \value{ 23 | Nothing. The function changes the values of the columns attr_name_rast in dt. 24 | } 25 | \description{ 26 | This function aggregates the attributes of each segment for the total 27 | catchment of each stream segment. It is called within \code{\link{calc_attributes_edges}} 28 | and should not be called by the user. 29 | } 30 | -------------------------------------------------------------------------------- /man/calc_catchment_attributes_rast_rec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_edges.R 3 | \name{calc_catchment_attributes_rast_rec} 4 | \alias{calc_catchment_attributes_rast_rec} 5 | \title{calc_catchment_attributes_rast_rec 6 | Aggregate attributes for the total catchment of each stream segment.} 7 | \usage{ 8 | calc_catchment_attributes_rast_rec(dt, id, stat, attr_name) 9 | } 10 | \arguments{ 11 | \item{dt}{data.table of stream topology and attributes per segment.} 12 | 13 | \item{id}{integer; 'stream' of outlet segment to start the calculation from.} 14 | 15 | \item{stat}{name or character vector giving the statistics to be calculated, 16 | must be one of: min, max, mean, percent.} 17 | 18 | \item{attr_name}{name or character vector of column names for the attribute(s) 19 | to be calculated.} 20 | } 21 | \value{ 22 | One row data.table with the cumulative number of cells of the total 23 | catchment of each segment and the values for each attribute and changes the 24 | values in dt. 25 | } 26 | \description{ 27 | Recursive function to calculate the catchment attributes of each stream 28 | segment. It is called by \code{\link{calc_catchment_attributes_rast}} for each 29 | outlet and should not be called by the user. 30 | } 31 | \note{ 32 | The values for stats "mean" and "percent" need to be divided by the cumulative 33 | number of cells of the total catchment in a subsequent step. 34 | } 35 | -------------------------------------------------------------------------------- /man/calc_catchment_attributes_vect.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_edges.R 3 | \name{calc_catchment_attributes_vect} 4 | \alias{calc_catchment_attributes_vect} 5 | \title{calc_catchment_attributes_vect 6 | Aggregate attributes for the total catchment of each stream segment.} 7 | \usage{ 8 | calc_catchment_attributes_vect(dt, stat_vect, attr_name_vect, round_dig) 9 | } 10 | \arguments{ 11 | \item{dt}{data.table of stream topology and attributes per segment.} 12 | 13 | \item{stat_vect}{name or character vector giving the statistics to be calculated, 14 | must be one of: percent, sum.} 15 | 16 | \item{attr_name_vect}{name or character vector of column names for the attribute(s) 17 | to be calculated.} 18 | 19 | \item{round_dig}{integer; number of digits to round results to. Can be a vector 20 | of different values or just one value for all attributes.} 21 | } 22 | \value{ 23 | Nothing. The function changes the values of the columns attr_name_vect in dt. 24 | } 25 | \description{ 26 | This function aggregates the attributes of each segment for the total 27 | catchment of each stream segment. It is called within \code{\link{calc_attributes_edges}} 28 | and should not be called by the user. 29 | } 30 | -------------------------------------------------------------------------------- /man/calc_catchment_attributes_vect_rec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_edges.R 3 | \name{calc_catchment_attributes_vect_rec} 4 | \alias{calc_catchment_attributes_vect_rec} 5 | \title{calc_catchment_attributes_vect_rec 6 | Aggregate attributes for the total catchment of each stream segment.} 7 | \usage{ 8 | calc_catchment_attributes_vect_rec(dt, id, stat_vect, attr_name_vect) 9 | } 10 | \arguments{ 11 | \item{dt}{data.table of stream topology and attributes per segment.} 12 | 13 | \item{id}{integer; 'stream' of outlet segment to start the calculation from.} 14 | 15 | \item{stat_vect}{name or character vector giving the statistics to be calculated, 16 | must be one of: min, max, mean, percent.} 17 | 18 | \item{attr_name_vect}{name or character vector of column names for the attribute(s) 19 | to be calculated.} 20 | } 21 | \value{ 22 | One row data.table with the cumulative number of cells of the total 23 | catchment of each segment and the values for each attribute and changes the 24 | values in dt. 25 | } 26 | \description{ 27 | Recursive function to calculate the catchment attributes of each stream 28 | segment. It is called by \code{\link{calc_catchment_attributes_vect}} for each 29 | outlet and should not be called by the user. 30 | } 31 | \keyword{internal} 32 | -------------------------------------------------------------------------------- /man/calc_edges.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_edges.R 3 | \name{calc_edges} 4 | \alias{calc_edges} 5 | \title{Calculate edges for SSN object.} 6 | \usage{ 7 | calc_edges() 8 | } 9 | \value{ 10 | Nothing. The function produces the following map: \itemize{ 11 | \item{'edges':} {derived stream segments with computed attributes needed for 12 | 'SSN' (vector)} } 13 | } 14 | \description{ 15 | A vector (lines) map 'edges' is derived from 'streams_v' and 16 | several attributes are assigned. 17 | } 18 | \details{ 19 | Steps include: 20 | \itemize{ \item{Assign unique 'rid' to each stream segment} 21 | \item{Find different stream networks in the region and assign 'netID'} 22 | \item{Calculate segments upstream distance, 'upDist' = flow length from the 23 | upstream node of the stream segment to the outlet of the network} 24 | \item{Calculate reach contributing areas (RCA ) per segment, 25 | 'rcaArea' = subcatchment area of each segment in square km} 26 | \item{Calculate catchment areas, 'H2OArea' = total catchment area of each 27 | segment in square km} } 28 | All lengths are rounded to 2 and all areas to 6 decimal places, respectively. 29 | } 30 | \note{ 31 | \code{\link{setup_grass_environment}}, \code{\link{import_data}} and 32 | \code{\link{derive_streams}} must be run before. 33 | } 34 | \examples{ 35 | \donttest{ 36 | # Initiate and setup GRASS 37 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 38 | if(.Platform$OS.type == "windows"){ 39 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 40 | } else { 41 | grass_program_path = "/usr/lib/grass78/" 42 | } 43 | 44 | setup_grass_environment(dem = dem_path, 45 | gisBase = grass_program_path, 46 | remove_GISRC = TRUE, 47 | override = TRUE 48 | ) 49 | gmeta() 50 | 51 | # Load files into GRASS 52 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 53 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 54 | import_data(dem = dem_path, sites = sites_path) 55 | 56 | # Derive streams from DEM 57 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 58 | 59 | check_compl_confluences() 60 | 61 | # Prepare edges 62 | calc_edges() 63 | 64 | # Plot data 65 | library(sp) 66 | dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 67 | edges <- readVECT('edges', ignore.stderr = TRUE) 68 | plot(dem, col = terrain.colors(20)) 69 | lines(edges, col = 'blue') 70 | } 71 | 72 | } 73 | \author{ 74 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net}, 75 | Eduard Szoecs, \email{eduardszoecs@gmail.com} 76 | } 77 | -------------------------------------------------------------------------------- /man/calc_offset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_prediction_sites.R 3 | \name{calc_offset} 4 | \alias{calc_offset} 5 | \title{Calculate offset} 6 | \usage{ 7 | calc_offset(dt, id, offs, dist) 8 | } 9 | \arguments{ 10 | \item{dt}{data.table containing the attributes of the stream segments} 11 | 12 | \item{id}{integer; 'stream' of the stream segment} 13 | 14 | \item{offs}{number; offset from outlet of the stream segment (downstream); 15 | equals the length of the segment if the point shall be placed directly at the 16 | downstream junction.} 17 | 18 | \item{dist}{number giving the distance between the points to create in map units.} 19 | } 20 | \value{ 21 | Nothing; change 'offset' in dt. 22 | } 23 | \description{ 24 | Recursive function to calculate the offset from the downstream 25 | junction needed to place points with fixed distance along the streams. 26 | It is called by \code{\link{calc_prediction_sites}} for each 27 | outlet and should not be called by the user. 28 | } 29 | \examples{ 30 | \dontrun{ 31 | outlets <- dt.streams[next_str == -1, stream] 32 | netID <- 1 33 | for(i in outlets){ 34 | calc_offset(dt.streams, id = i, offs = 0, dist = 200) 35 | } 36 | } 37 | } 38 | \author{ 39 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 40 | } 41 | \keyword{internal} 42 | -------------------------------------------------------------------------------- /man/calc_outdist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/delete_lakes.R 3 | \name{calc_outdist} 4 | \alias{calc_outdist} 5 | \title{Update flow length from the upstream node of each stream segment 6 | to the outlet of the network .} 7 | \usage{ 8 | calc_outdist(dt, id) 9 | } 10 | \arguments{ 11 | \item{dt}{data.table containing the attributes of the stream segments} 12 | 13 | \item{id}{integer; 'stream' of the stream segment} 14 | } 15 | \value{ 16 | nothing 17 | } 18 | \description{ 19 | Recursive function to calculate the flow length from the 20 | upstream node of the stream segment to the outlet of the network. 21 | It is called by \code{\link{delete_lakes}} for each 22 | outlet and should not be called by the user. 23 | } 24 | \details{ 25 | calc_outdist 26 | } 27 | \author{ 28 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 29 | } 30 | \keyword{internal} 31 | -------------------------------------------------------------------------------- /man/calc_prediction_sites.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_prediction_sites.R 3 | \name{calc_prediction_sites} 4 | \alias{calc_prediction_sites} 5 | \title{Calculate prediction sites for 'SSN' object.} 6 | \usage{ 7 | calc_prediction_sites(predictions, dist = NULL, nsites = 10, netIDs = NULL) 8 | } 9 | \arguments{ 10 | \item{predictions}{string giving the name for the prediction sites map.} 11 | 12 | \item{dist}{number giving the distance between the points to create in map 13 | units.} 14 | 15 | \item{nsites}{integer giving the approximate number of sites to create} 16 | 17 | \item{netIDs}{integer (optional): create prediction sites only on streams with 18 | these netID(s).} 19 | } 20 | \description{ 21 | A vector (points) map of prediction sites is created and several 22 | attributes are assigned. 23 | } 24 | \details{ 25 | Either \code{dist} or \code{nsites} must be provided. If \code{dist} 26 | is NULL, it is estimated by dividing the total stream length in the map by 27 | \code{nsites}; the number of sites actually derived might therefore be a bit 28 | smaller than \code{nsites}. 29 | 30 | Steps include: 31 | \itemize{ 32 | \item{Place points on edges with given distance from each other} 33 | \item{Save the point coordinates in NEAR_X and NEAR_Y.} 34 | \item{Assign unique identifiers (needed by the 'SSN' package) 'pid' 35 | and 'locID'.} 36 | \item{Get 'rid' and 'netID' of the stream segment the site 37 | intersects with (from map 'edges').} 38 | \item{Calculate upstream distance for 39 | each point ('upDist').} 40 | \item{Calculate distance ratio ('distRatio') between 41 | position of the site on the edge (= distance traveled from lower end of the 42 | edge to the site) and the total length of the edge.} } 43 | 44 | 'pid' and 'locID' are identical, unique numbers. 'upDist' is calculated using 45 | \href{https://grass.osgeo.org/grass78/manuals/addons/r.stream.distance.html}{r.stream.distance}. 46 | Points are created using 47 | \href{https://grass.osgeo.org/grass78/manuals/v.segment.html}{v.segment}. 48 | } 49 | \note{ 50 | \code{\link{import_data}}, \code{\link{derive_streams}} and 51 | \code{\link{calc_edges}} must be run before. 52 | } 53 | \examples{ 54 | \donttest{ 55 | # Initiate and setup GRASS 56 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 57 | if(.Platform$OS.type == "windows"){ 58 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 59 | } else { 60 | grass_program_path = "/usr/lib/grass78/" 61 | } 62 | 63 | setup_grass_environment(dem = dem_path, 64 | gisBase = grass_program_path, 65 | remove_GISRC = TRUE, 66 | override = TRUE 67 | ) 68 | gmeta() 69 | 70 | # Load files into GRASS 71 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 72 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 73 | import_data(dem = dem_path, sites = sites_path) 74 | 75 | # Derive streams from DEM 76 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 77 | 78 | check_compl_confluences() 79 | calc_edges() 80 | calc_sites() 81 | calc_prediction_sites(predictions = "preds", dist = 2500) 82 | 83 | library(sp) 84 | dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 85 | sites <- readVECT('sites', ignore.stderr = TRUE) 86 | preds <- readVECT('preds', ignore.stderr = TRUE) 87 | edges <- readVECT('edges', ignore.stderr = TRUE) 88 | plot(dem, col = terrain.colors(20)) 89 | lines(edges, col = 'blue', lwd = 2) 90 | points(sites, pch = 4) 91 | points(preds, pch = 19, col = "darkred") 92 | } 93 | } 94 | \author{ 95 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net} 96 | } 97 | -------------------------------------------------------------------------------- /man/calc_sites.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_sites.R 3 | \name{calc_sites} 4 | \alias{calc_sites} 5 | \title{Calculate sites for SSN object.} 6 | \usage{ 7 | calc_sites( 8 | locid_col = NULL, 9 | pid_col = NULL, 10 | predictions = NULL, 11 | maxdist = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{locid_col}{character (optional); column name in the sites attribute 16 | table giving a unique site identifier. If not provided, it is created 17 | automatically (based on the 'cat' field; default).} 18 | 19 | \item{pid_col}{character (optional); column name in the sites attribute table 20 | that distinguishes between repeated measurements at a sampling site, e.g. by 21 | date. If not provided, it is created automatically.} 22 | 23 | \item{predictions}{character vector (optional); names for prediction sites 24 | (loaded with \code{import_data}).} 25 | 26 | \item{maxdist}{integer (optional); maximum snapping distance in map units (see details). 27 | Sites farther away from edges will be deleted.} 28 | } 29 | \description{ 30 | A vector (points) map 'sites' is derived and several attributes 31 | are assigned. 32 | } 33 | \details{ 34 | Steps include: 35 | \itemize{ 36 | \item{Snap points to derived network (edges). 'dist' 37 | gives the distance of the original position to the closest streams segment. 38 | If this is a too large value consider running \code{\link{derive_streams}} again with 39 | smaller value for \code{accum_threshold} and/or \code{min_stream_length}.} 40 | \item{Save the new point coordinates in NEAR_X and NEAR_Y.} 41 | \item{Assign unique 'pid' and 'locID' (needed by the 'SSN' package).} 42 | \item{Get 'rid' and 'netID' of the 43 | stream segment the site intersects with (from map "edges").} 44 | \item{Calculate upstream distance for each point ('upDist').} 45 | \item{Calculate distance ratio 46 | ('ratio') between position of site on edge (distance traveled from lower 47 | end of the edge to the site) and the total length of the edge.} } 48 | Often, survey sites do not lay exactly on the stream network (due to GPS imprecision, 49 | stream representation as lines, derivation of streams from dem, etc.). To 50 | assign an exact position of the sites on the network they are moved to the 51 | closest stream segment (snapped) using the GRASS function 52 | \href{https://grass.osgeo.org/grass78/manuals/v.distance.html}{v.distance}. 53 | 54 | If \code{locid_col} and \code{pid_col} are not provided, 'pid' and 'locID' 55 | are identical, unique numbers. If they are provided, they are created based 56 | on these columns (as numbers, not as text). Note that measurements 57 | can be joined to the sites at a later step (including multiple parameters and 58 | multiple measurements) using \code{\link{merge_sites_measurements}}. 59 | Then, 'pid' is updated accordingly. 60 | 61 | 'upDist' is calculated using 62 | \href{https://grass.osgeo.org/grass78/manuals/v.distance.html}{v.distance} with 63 | upload = "to_along" which gives the distance along the stream segment to the next 64 | upstream node ('distalong'). 'upDist' is the difference between the 'upDist' 65 | of the edge the point lies on and 'distalong'. 66 | 67 | The unit for distances (= map units) can be found out using 68 | execGRASS("g.proj", flags = "p"). 69 | 70 | If prediction sites have been created outside of this package they can be 71 | processed here as well. They must have been imported with \code{\link{import_data}} 72 | before. Alternatively, prediction sites can be created using 73 | \code{\link{calc_prediction_sites}}. 74 | } 75 | \note{ 76 | \code{\link{import_data}}, \code{\link{derive_streams}} and 77 | \code{\link{calc_edges}} must be run before. 78 | } 79 | \examples{ 80 | \donttest{ 81 | # Initiate and setup GRASS 82 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 83 | if(.Platform$OS.type == "windows"){ 84 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 85 | } else { 86 | grass_program_path = "/usr/lib/grass78/" 87 | } 88 | 89 | setup_grass_environment(dem = dem_path, 90 | gisBase = grass_program_path, 91 | remove_GISRC = TRUE, 92 | override = TRUE 93 | ) 94 | gmeta() 95 | 96 | # Load files into GRASS 97 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 98 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 99 | import_data(dem = dem_path, sites = sites_path) 100 | 101 | # Derive streams from DEM 102 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 103 | 104 | # Check and correct complex confluences (there are no complex confluences in this 105 | # example date set; set accum_threshold in derive_streams to a smaller value 106 | # to create complex confluences) 107 | cj <- check_compl_confluences() 108 | if(cj){ 109 | correct_compl_confluences() 110 | } 111 | 112 | # Prepare edges 113 | calc_edges() 114 | 115 | # Prepare site 116 | calc_sites() 117 | 118 | # Plot data 119 | library(sp) 120 | dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 121 | edges <- readVECT('edges', ignore.stderr = TRUE) 122 | sites <- readVECT('sites', ignore.stderr = TRUE) 123 | sites_o <- readVECT('sites_o', ignore.stderr = TRUE) 124 | plot(dem, col = terrain.colors(20),axes = TRUE) 125 | lines(edges, col = 'blue') 126 | points(sites, pch = 4) 127 | points(sites_o, pch = 1) 128 | legend("topright", pch = c(1, 4), legend = c("original", "corrected")) 129 | } 130 | } 131 | \author{ 132 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net}, Eduard Szoecs, 133 | \email{eduardszoecs@gmail.com}, 134 | } 135 | -------------------------------------------------------------------------------- /man/check_compl_confluences.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/check_compl_confluences.R 3 | \name{check_compl_confluences} 4 | \alias{check_compl_confluences} 5 | \title{Check if there are more than two inflows to an outflow.} 6 | \usage{ 7 | check_compl_confluences() 8 | } 9 | \value{ 10 | TRUE if there are complex confluences. 11 | } 12 | \description{ 13 | It is checked, if more than two line segments flow into a node, i.e. 14 | if there are more than two inflows to an outflow. 15 | } 16 | \details{ 17 | It is checked, if there are columns named 'prev_str03', 'prev_str04' and 18 | 'prev_str05' in the attribute table of streams_v derived with \code{derive_streams} 19 | (i.e. based on the GRASS function 20 | \href{https://grass.osgeo.org/grass78/manuals/addons/r.stream.order.html}{r.stream.order}). 21 | } 22 | \note{ 23 | \code{\link{setup_grass_environment}}, \code{\link{import_data}} and 24 | \code{\link{derive_streams}} must be run before. 25 | } 26 | \examples{ 27 | \donttest{ 28 | # Initiate and setup GRASS 29 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 30 | if(.Platform$OS.type == "windows"){ 31 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 32 | } else { 33 | grass_program_path = "/usr/lib/grass78/" 34 | } 35 | 36 | setup_grass_environment(dem = dem_path, 37 | gisBase = grass_program_path, 38 | remove_GISRC = TRUE, 39 | override = TRUE 40 | ) 41 | gmeta() 42 | 43 | # Load files into GRASS 44 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 45 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 46 | import_data(dem = dem_path, sites = sites_path) 47 | 48 | # Derive streams from DEM 49 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 50 | 51 | check_compl_confluences() 52 | 53 | # Deriving streams with finer resolution will result in complext confluences 54 | derive_streams(burn = 0, accum_threshold = 150, condition = TRUE, clean = TRUE) 55 | check_compl_confluences() 56 | } 57 | } 58 | \author{ 59 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net} 60 | } 61 | -------------------------------------------------------------------------------- /man/check_projection.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/import_data.R 3 | \name{check_projection} 4 | \alias{check_projection} 5 | \title{Show the projection of raster data and compare to the current GRASS location.} 6 | \usage{ 7 | check_projection(path, format = "wkt") 8 | } 9 | \arguments{ 10 | \item{path}{character string vector; path raster data file(s)} 11 | 12 | \item{format}{character string; how to format the output (see details)} 13 | } 14 | \value{ 15 | Nothing. 16 | } 17 | \description{ 18 | Check if the projection of raster files matches the region of the 19 | current location 20 | } 21 | \details{ 22 | This is a wrapper for the GRASS function 23 | \href{https://grass.osgeo.org/grass78/manuals/g.proj.html}{g.proj}. 24 | It prints out the projection information of the current location 25 | and of the raster file(s). Based on this information it can be decided if the data 26 | can be read into GRASS (\code{\link{import_data}}) without prior processing, i.e. 27 | if all raster data are of the same projection. 28 | Different output options can be chosen: 29 | * \code{wkt} WKT format (default) 30 | * \code{grass} conventional GRASS format 31 | * \code{shell} shell script style 32 | * \code{proj4} PROJ.4 format (note that this format is deprecated) 33 | } 34 | -------------------------------------------------------------------------------- /man/check_ssn.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/check_ssn.R 3 | \name{check_ssn} 4 | \alias{check_ssn} 5 | \title{Checking 'SSN' object.} 6 | \usage{ 7 | check_ssn(path, predictions = NULL) 8 | } 9 | \arguments{ 10 | \item{path}{character; path to .ssn object.} 11 | 12 | \item{predictions}{name(s) of prediction map(s) (optional).} 13 | } 14 | \value{ 15 | TRUE or FALSE depending if checks pass. 16 | } 17 | \description{ 18 | This function roughly checks the 'SSN' object. It returns FALSE if some 19 | essential columns are missing or values have illegal values. 20 | } 21 | \examples{ 22 | \donttest{ 23 | # Initiate and setup GRASS 24 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 25 | if(.Platform$OS.type == "windows"){ 26 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 27 | } else { 28 | grass_program_path = "/usr/lib/grass78/" 29 | } 30 | 31 | setup_grass_environment(dem = dem_path, 32 | gisBase = grass_program_path, 33 | remove_GISRC = TRUE, 34 | override = TRUE 35 | ) 36 | gmeta() 37 | 38 | # Load files into GRASS 39 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 40 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 41 | import_data(dem = dem_path, sites = sites_path) 42 | 43 | # Derive streams from DEM 44 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 45 | 46 | # Check and correct complex confluences (there are no complex confluences in this 47 | # example date set; set accum_threshold in derive_streams to a smaller value 48 | # to create complex confluences) 49 | cj <- check_compl_confluences() 50 | if(cj){ 51 | correct_compl_confluences() 52 | } 53 | 54 | # Prepare edges 55 | calc_edges() 56 | 57 | # Prepare site 58 | calc_sites() 59 | # Calculate H2OArea 60 | calc_attributes_sites_exact() 61 | 62 | # Plot data 63 | library(sp) 64 | dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 65 | sites <- readVECT('sites', ignore.stderr = TRUE) 66 | sites_orig <- readVECT('sites_o', ignore.stderr = TRUE) 67 | edges <- readVECT('edges', ignore.stderr = TRUE) 68 | plot(dem, col = terrain.colors(20)) 69 | lines(edges, col = 'blue') 70 | points(sites_orig, pch = 4) 71 | cols <- colorRampPalette(c("blue", 'red'))(length(sites$H2OArea))[rank(sites$H2OArea)] 72 | points(sites, pch = 16, col = cols) 73 | 74 | # Write data to SSN Folder 75 | ssn_dir <- file.path(tempdir(), 'nc.ssn') 76 | export_ssn(ssn_dir, delete_directory = TRUE) 77 | 78 | # Check if all files are ok 79 | library(SSN) 80 | check_ssn(ssn_dir) 81 | } 82 | } 83 | \author{ 84 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net}, 85 | Eduard Szoecs, \email{eduardszoecs@gmail.com} 86 | } 87 | -------------------------------------------------------------------------------- /man/correct_compl_confluences.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/correct_compl_confluences.R 3 | \name{correct_compl_confluences} 4 | \alias{correct_compl_confluences} 5 | \title{Correct confluences with three or more inflows.} 6 | \usage{ 7 | correct_compl_confluences(clean = TRUE) 8 | } 9 | \arguments{ 10 | \item{clean}{logical; should intermediate files be removed from 'GRASS' 11 | session?} 12 | } 13 | \value{ 14 | Nothing. The function changes features in 'streams_v'. Changed features are 15 | marked in the new column 'changed'. 16 | } 17 | \description{ 18 | At complex confluences (when more than two line segments flow into a node, 19 | i.e. more than two inflows to an outflow), the end of one of the inflows is moved a 20 | tiny bit upstream to one of the other inflows to create a new confluence of 21 | two streams (see details below). 22 | } 23 | \details{ 24 | At complex confluences (when more than two line segments flow into a node, 25 | i.e. more than two inflows to an outflow), new confluences of only two streams 26 | are created: 27 | 1. complex confluences are found based on the fact that the outflow has more than 28 | two previous streams 29 | 2. the inflow with the shortest cumulative length from its source is found; 30 | the end of this segment will be moved 31 | 3. the inflow with the smallest angle to this inflow is found; 32 | this segment will be cut into tow segments close to the junction using the GRASS function 33 | \href{https://grass.osgeo.org/grass78/manuals/v.edit.html}{v.edit}(tool = 34 | break) creating a new confluence 35 | 4. the shortest inflow found in 2 is moved to the newly created confluence using 36 | \href{https://grass.osgeo.org/grass78/manuals/v.edit.html}{v.edit}(tool = 37 | vertexmove) 38 | 5. all lengths are updated (segment length, cumulative length, i.e. length of the stream 39 | from the source, distance to the outlet). 40 | The distance the shortest confluence is moved depends on the number of inflows. For three 41 | inflows, it is moved 1/12 time the DEM cellsize upstream, for seven (the extremely rare maximum) 42 | 5/12 * cellsize. 43 | } 44 | \note{ 45 | \code{\link{setup_grass_environment}}, \code{\link{import_data}} and 46 | \code{\link{derive_streams}} must be run before. 47 | } 48 | \examples{ 49 | \donttest{ 50 | # Initiate and setup GRASS 51 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 52 | if(.Platform$OS.type == "windows"){ 53 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 54 | } else { 55 | grass_program_path = "/usr/lib/grass78/" 56 | } 57 | 58 | setup_grass_environment(dem = dem_path, 59 | gisBase = grass_program_path, 60 | remove_GISRC = TRUE, 61 | override = TRUE 62 | ) 63 | gmeta() 64 | 65 | # Load files into GRASS 66 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 67 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 68 | streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 69 | import_data(dem = dem_path, sites = sites_path, streams = streams_path) 70 | 71 | # Derive streams from DEM 72 | derive_streams(burn = 10, accum_threshold = 100, condition = TRUE, clean = TRUE) 73 | 74 | # Check and correct complex confluences (there are complex confluences in the 75 | # example date set if the accumulation threshold is low) 76 | cj <- check_compl_confluences() 77 | if(cj){ 78 | correct_compl_confluences() 79 | } 80 | 81 | # plot 82 | library(sp) 83 | dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 84 | streams <- readVECT('streams_v', ignore.stderr = TRUE) 85 | streams_orig <- readVECT('streams_v_o3', ignore.stderr = TRUE) 86 | # zoom to a relevant part of the dem 87 | plot(dem, col = terrain.colors(20), axes = TRUE, 88 | xlim = c(640100,640150), ylim = c(219735,219785)) 89 | lines(streams_orig, col = 'red', lwd = 4) 90 | lines(streams, col = 'blue', lty = 2, lwd = 2) 91 | legend("bottomright", col = c("red", "blue"), lty = c(1,2), lwd = c(4,2), 92 | legend = c("original", "corrected")) 93 | 94 | plot(streams, col = c("blue", "red")[streams@data$changed+1], lty = 1, lwd = 2) 95 | } 96 | } 97 | \author{ 98 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net} 99 | } 100 | -------------------------------------------------------------------------------- /man/delete_lakes.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/delete_lakes.R 3 | \name{delete_lakes} 4 | \alias{delete_lakes} 5 | \title{Delete lakes from stream network} 6 | \usage{ 7 | delete_lakes(lakes, keep = TRUE) 8 | } 9 | \arguments{ 10 | \item{lakes}{character string or object; path to lake vector file (ESRI shape), 11 | name of vector map in the GRASS data base or sp or sf data object.} 12 | 13 | \item{keep}{boolean; should the original 'streams_v' be saved? Default is TRUE.} 14 | } 15 | \value{ 16 | Nothing. The function updates 'streams_v' and (if keep = TRUE) saves 17 | the original file to streams_v_prev_lakes. If \code{lakes} is a file path, the lakes 18 | are imported into GRASS as 'lakes'. 19 | } 20 | \description{ 21 | When the stream network is derived from a dem, the streams 22 | will just cross lakes or ponds. However, the flow is disconnected 23 | here and the relationship between sampling points upstream and 24 | downstream of a lake is not clear. For instance, chemicals could 25 | be retained and temperature altered in a lake. This function intersects 26 | the stream network with a given vector map of lakes; it deletes the stream 27 | segments in the lake, breaks those that cross its borders and 28 | assigns a new, updated topology. 29 | } 30 | \note{ 31 | The column 'out_dist' (flow length from the upstream node of the 32 | segment to the outlet of the network) is updated based on the new segment length. 33 | In contrast, 'cum_length' is not updated as it is no longer needed and will 34 | be deleted in calc_edges. 35 | 36 | #' 37 | } 38 | \examples{ 39 | \donttest{ 40 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 41 | if(.Platform$OS.type == "windows"){ 42 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 43 | } else { 44 | grass_program_path = "/usr/lib/grass78/" 45 | } 46 | 47 | setup_grass_environment(dem = dem_path, 48 | gisBase = grass_program_path, 49 | remove_GISRC = TRUE, 50 | override = TRUE 51 | ) 52 | gmeta() 53 | 54 | # Load files into GRASS 55 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 56 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 57 | import_data(dem = dem_path, sites = sites_path) 58 | 59 | # Derive streams from DEM 60 | derive_streams(burn = 0, accum_threshold = 100, condition = TRUE, clean = TRUE) 61 | 62 | # Check and correct complex confluences (there are no complex confluences in this 63 | # example date set; set accum_threshold in derive_streams to a smaller value 64 | # to create complex confluences) 65 | cj <- check_compl_confluences() 66 | if(cj){ 67 | correct_compl_confluences() 68 | } 69 | 70 | lakes_path <- system.file("extdata", "nc", "lakes.shp", package = "openSTARS") 71 | delete_lakes(lakes = lakes_path) 72 | 73 | # plot 74 | library(sp) 75 | dem <- readRAST('dem', ignore.stderr = TRUE, plugin = FALSE) 76 | streams <- readVECT('streams_v', ignore.stderr = TRUE) 77 | streams_with_lakes <- readVECT('streams_v_prev_lakes', ignore.stderr = TRUE) 78 | lakes <- readVECT('lakes', ignore.stderr = TRUE) 79 | 80 | plot(dem, col = terrain.colors(20), axes = TRUE) 81 | plot(lakes, add = TRUE, col = "grey") 82 | lines(streams_with_lakes, col = 'red', lty = 1, lwd = 2) 83 | lines(streams, col = 'blue', lty = 4, lwd = 2) 84 | legend("topright", col = c("red", "blue"), lty = c(1,4), lwd = c(2,2), 85 | legend = c("through lakes", "lakes cut out")) 86 | } 87 | } 88 | \author{ 89 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net} 90 | } 91 | -------------------------------------------------------------------------------- /man/derive_streams.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/derive_streams.R 3 | \name{derive_streams} 4 | \alias{derive_streams} 5 | \title{Derive stream network from DEM.} 6 | \usage{ 7 | derive_streams( 8 | burn = 0, 9 | accum_threshold = 700, 10 | condition = TRUE, 11 | min_stream_length = 0, 12 | dem_name = NULL, 13 | clean = TRUE, 14 | mem = FALSE 15 | ) 16 | } 17 | \arguments{ 18 | \item{burn}{numeric; how many meters should the streams be burned into the 19 | DEM? Only applicable if a mapped stream network is provided in \code{\link{import_data}}. 20 | Defaults to 0.} 21 | 22 | \item{accum_threshold}{integer; accumulation threshold to use (i.e. minimum 23 | flow accumulation value in cells that will initiate a new stream). A small value 24 | results in many small streams. Defaults to 700 but a reasonable value 25 | strongly depends on the raster resolution. See details below.} 26 | 27 | \item{condition}{logical; should the DEM be conditioned using the GRASS function 28 | \href{https://grass.osgeo.org/grass78/manuals/addons/r.hydrodem.html}{r.hydrodem}; 29 | default: TRUE.} 30 | 31 | \item{min_stream_length}{integer: minimum stream length in number of DEM 32 | raster cells; shorter first order stream segments are deleted. Defaults to 0 33 | but a reasonable value strongly depends on the raster resolution. See details below.} 34 | 35 | \item{dem_name}{character vector, optional; default: 'dem'; useful if 36 | conditioned and / or burnt in DEM raster from previous runs shall be used.} 37 | 38 | \item{clean}{logical; should intermediate raster layer of imported streams 39 | ('streams_or') be removed from the GRASS session? Defaults to TRUE.} 40 | 41 | \item{mem}{logical; should -m flag in the GRASS function 42 | \href{https://grass.osgeo.org/grass78/manuals/r.watershed.html}{r.watershed} 43 | be used (for data preparation)? Defaults to FALSE; if set to TRUE the calculation 44 | uses disk swap mode, i.e. it is not carried out in the RAM but also using disk space. 45 | Useful for large data sets but also slower.} 46 | } 47 | \value{ 48 | Nothing. The function produces the following maps: 49 | \itemize{ 50 | \item{'streams_v'} {derived streams with topology (vector)} 51 | \item{'dirs'} {flow directions (raster)} 52 | \item{'accums'} {accumulation values (raster)} 53 | \item{'dem_cond'} {conditioned dem (raster) if \code{condition} is TRUE} 54 | \item{'dem_[cond]_burn[X]'} {burnt in DEM (raster) if burn is > 0} 55 | } The original GRASS map 'dem' is not modified if \code{condition} is TRUE and / or \code{burn} > 0. 56 | } 57 | \description{ 58 | Streams are derived from a digital elevation model (DEM) using the GRASS function 59 | \href{https://grass.osgeo.org/grass78/manuals/r.stream.extract.html}{r.stream.extract}. 60 | If a stream network is available (see \code{\link{import_data}}) and burn > 0 61 | it will be first burnt into DEM. Stream topology is derived using the GRASS function 62 | \href{https://grass.osgeo.org/grass78/manuals/addons/r.stream.order.html}{r.stream.order}. 63 | } 64 | \details{ 65 | For details on \code{accum_threshold} and \code{min_stream_length} 66 | see the parameters 'threshold' and 'stream_length' at 67 | \href{https://grass.osgeo.org/grass78/manuals/r.stream.extract.html}{r.stream.extract}. 68 | It might be useful to not burn in the whole available stream network but only 69 | parts of it (e.g., larger streams with higher Strahler stream order only). 70 | For this, the stream network needs to be pre-processed (parts could be deleted) 71 | before loading it with \code{import_data}. 72 | } 73 | \note{ 74 | \code{\link{setup_grass_environment}} and \code{\link{import_data}} 75 | must be run before. 76 | } 77 | \examples{ 78 | \donttest{ 79 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 80 | if(.Platform$OS.type == "windows"){ 81 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 82 | } else { 83 | grass_program_path = "/usr/lib/grass78/" 84 | } 85 | 86 | setup_grass_environment(dem = dem_path, 87 | gisBase = grass_program_path, 88 | remove_GISRC = TRUE, 89 | override = TRUE 90 | ) 91 | gmeta() 92 | 93 | # Load files into GRASS 94 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 95 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 96 | streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 97 | import_data(dem = dem_path, sites = sites_path, streams = streams_path) 98 | 99 | # Derive streams from DEM 100 | derive_streams(burn = 10, accum_threshold = 700, condition = TRUE, clean = TRUE) 101 | 102 | # Plot 103 | library(sp) 104 | dem <- readRAST('dem', ignore.stderr = TRUE) 105 | sites <- readVECT('sites_o', ignore.stderr = TRUE) 106 | streams_o <- readVECT('streams_o', ignore.stderr = TRUE) 107 | streams <- readVECT('streams_v', ignore.stderr = TRUE) 108 | plot(dem, col = terrain.colors(20)) 109 | lines(streams, col = 'blue', lwd = 2) 110 | lines(streams_o, col = 'lightblue', lwd = 1) 111 | legend("topright", col = c("lightblue", "blue"), lwd = c(1,2), 112 | legend = c("read in streams for burn in", "derived streams"))#' } 113 | 114 | } 115 | \author{ 116 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net}, Eduard Szoecs, \email{eduardszoecs@gmail.com} 117 | } 118 | -------------------------------------------------------------------------------- /man/export_ssn.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/export_ssn.R 3 | \name{export_ssn} 4 | \alias{export_ssn} 5 | \title{Export 'SSN' object} 6 | \usage{ 7 | export_ssn(path, predictions = NULL, delete_directory = FALSE) 8 | } 9 | \arguments{ 10 | \item{path}{character; path to write .ssn object to.} 11 | 12 | \item{predictions}{name(s) of prediction map(s) (optional).} 13 | 14 | \item{delete_directory}{boolean; shall the ssn directory and all files be 15 | deleted before export in case it already exists? See details.} 16 | } 17 | \value{ 18 | Nothing. Files are written to the specified folder 19 | } 20 | \description{ 21 | This function exports the calculated sites, edges and binary IDs 22 | to a folder which then can be read using the 'SSN' package. 23 | } 24 | \details{ 25 | First it is checked if one of the column names is longer than 10 26 | characters (which cannot be exported to ESRI shape files as required by 'SSN'). 27 | 28 | \code{delete_directory = TRUE} is useful if the same directory name has been 29 | used before and the existing data shall be overwritten. 30 | } 31 | \examples{ 32 | \donttest{ 33 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 34 | if(.Platform$OS.type == "windows"){ 35 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 36 | } else { 37 | grass_program_path = "/usr/lib/grass78/" 38 | } 39 | 40 | setup_grass_environment(dem = dem_path, 41 | gisBase = grass_program_path, 42 | remove_GISRC = TRUE, 43 | override = TRUE 44 | ) 45 | gmeta() 46 | 47 | # Load files into GRASS 48 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 49 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 50 | import_data(dem = dem_path, sites = sites_path) 51 | 52 | # Derive streams from DEM 53 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 54 | 55 | # Check and correct complex confluences (there are no complex confluences in this 56 | # example date set; set accum_threshold in derive_streams to a smaller value 57 | # to create complex confluences) 58 | cj <- check_compl_confluences() 59 | if(cj){ 60 | correct_compl_confluences() 61 | } 62 | 63 | # Prepare edges 64 | calc_edges() 65 | 66 | # Prepare site 67 | calc_sites() 68 | 69 | # Write data to SSN Folder 70 | ssn_dir <- file.path(tempdir(), 'nc.ssn') 71 | export_ssn(ssn_dir, delete_directory = TRUE) 72 | list.files(ssn_dir) 73 | } 74 | 75 | } 76 | \author{ 77 | Mira Kattwinkel,\email{mira.kattwinkel@gmx.net}, 78 | Eduard Szoecs, \email{eduardszoecs@gmail.com} 79 | } 80 | -------------------------------------------------------------------------------- /man/get_all_raster_values.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_edges.R 3 | \name{get_all_raster_values} 4 | \alias{get_all_raster_values} 5 | \title{get_all_raster_values 6 | Returns all unique values in the raster} 7 | \usage{ 8 | get_all_raster_values(raster_name) 9 | } 10 | \arguments{ 11 | \item{raster_name}{name of the raster map} 12 | } 13 | \value{ 14 | a vector of all values in the raster 15 | } 16 | \description{ 17 | Returns the number of different values in the input raster. 18 | } 19 | \note{ 20 | This function is sensitive to MASKs, i.e. if a MASK is present, 21 | only the part or the raster is processed within the MASK; 22 | \href{https://grass.osgeo.org/grass78/manuals/r.mask.html}{r.mask}. 23 | } 24 | \keyword{internal} 25 | -------------------------------------------------------------------------------- /man/get_cats_edges_in_catchment.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_edges.R 3 | \name{get_cats_edges_in_catchment} 4 | \alias{get_cats_edges_in_catchment} 5 | \title{get_cats_edges_in_catchment 6 | Returns the cats of this and all upstream edges} 7 | \usage{ 8 | get_cats_edges_in_catchment(dt, str_id) 9 | } 10 | \arguments{ 11 | \item{dt}{data.table containing the attributes of the stream segments} 12 | 13 | \item{str_id}{integer giving the stream_id ('stream') of the starting edge} 14 | } 15 | \value{ 16 | vector of cat values of all upstream edges and the calling one. 17 | } 18 | \description{ 19 | Recursive function to get the stream_ids from one segment upstream. 20 | This function is used internally and is not intended to be called by the user. 21 | } 22 | \author{ 23 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 24 | } 25 | \keyword{internal} 26 | -------------------------------------------------------------------------------- /man/get_n_val_raster.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_attributes_edges.R 3 | \name{get_n_val_raster} 4 | \alias{get_n_val_raster} 5 | \title{get_n_val_raster 6 | Returns the number of different values in the raster.} 7 | \usage{ 8 | get_n_val_raster(raster_name) 9 | } 10 | \arguments{ 11 | \item{raster_name}{name of the raster map} 12 | } 13 | \value{ 14 | The range of values in the raster map. 15 | } 16 | \description{ 17 | Returns the number of different values in the input raster. 18 | } 19 | \note{ 20 | This function is sensitive to MASKs, i.e. if a MASK is present, 21 | only the part or the raster is processed within the MASK; 22 | \href{https://grass.osgeo.org/grass78/manuals/r.mask.html}{r.mask}. 23 | } 24 | \keyword{internal} 25 | -------------------------------------------------------------------------------- /man/get_streams_edges_in_catchment.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_edges.R 3 | \name{get_streams_edges_in_catchment} 4 | \alias{get_streams_edges_in_catchment} 5 | \title{get_streams_edges_in_catchment 6 | Returns the stream values of this and all upstream edges} 7 | \usage{ 8 | get_streams_edges_in_catchment(dt, str_id) 9 | } 10 | \arguments{ 11 | \item{dt}{data.table containing the attributes of the stream segments} 12 | 13 | \item{str_id}{integer giving the stream_id ('stream') of the starting edge} 14 | } 15 | \value{ 16 | vector of stream values of all upstream edges and the calling one. 17 | } 18 | \description{ 19 | Recursive function to get the stream from one segment upstream. 20 | This function is used internally and is not intended to be called by the user. 21 | } 22 | \author{ 23 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 24 | } 25 | \keyword{internal} 26 | -------------------------------------------------------------------------------- /man/grass_v.to.db.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/setup_grass_environment.R 3 | \name{grass_v.to.db} 4 | \alias{grass_v.to.db} 5 | \title{Update attribute table.} 6 | \usage{ 7 | grass_v.to.db(map, option, type = "line", columns, format) 8 | } 9 | \arguments{ 10 | \item{map}{character; name of the map where values should be uploaded.} 11 | 12 | \item{option}{character; what values should be uploaded} 13 | 14 | \item{type}{character; feature type (default = "line")} 15 | 16 | \item{columns}{character; name of the column top upload data to} 17 | 18 | \item{format}{character; data format of the new columns (if it must be created)} 19 | } 20 | \value{ 21 | Nothing. Uses \href{https://grass.osgeo.org/grass78/manuals/v.to.db.html}{v.to.db} 22 | to populate attribute values from vector features. 23 | } 24 | \description{ 25 | Wrapper for v.to.db to catch errors arising in some GRASS versions 26 | } 27 | \details{ 28 | Since different versions of GRASS handle v.to.db differently (older versions <= 7.4 need 29 | the column to exists. while newer ones create the column) different implementations are 30 | necessary 31 | } 32 | \author{ 33 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 34 | } 35 | -------------------------------------------------------------------------------- /man/import_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/import_data.R 3 | \name{import_data} 4 | \alias{import_data} 5 | \title{Import data into 'GRASS.'} 6 | \usage{ 7 | import_data( 8 | dem, 9 | band = 1, 10 | sites, 11 | streams = NULL, 12 | snap_streams = FALSE, 13 | pred_sites = NULL, 14 | predictor_raster = NULL, 15 | predictor_r_names = NULL, 16 | predictor_vector = NULL, 17 | predictor_v_names = NULL 18 | ) 19 | } 20 | \arguments{ 21 | \item{dem}{character; path to DEM (digital elevation model) raster file.} 22 | 23 | \item{band}{integer (optional); defines which band of the dem to use} 24 | 25 | \item{sites}{character string or object; path to sites vector file (ESRI shape) 26 | or sp or sf data object.} 27 | 28 | \item{streams}{character string or object (optional); path to network vector 29 | file (ESRI shape) or sp or sf data object. If available this can be burnt into the DEM 30 | in \code{\link{derive_streams}}} 31 | 32 | \item{snap_streams}{boolean (optional); snap line ends. 33 | If TRUE line ends of the streams are snapped to the next feature if they are 34 | unconnected with threshold of 10 m using 'GRASS' function v.clean.} 35 | 36 | \item{pred_sites}{character string vector or object(s) (optional); path(s) to prediction sites 37 | vector files (ESRI shape) or sp or sf data object. 38 | Different formats (i.e. path and objects) must not be mixed; more than one sf or sp 39 | object must be provided as a list, not concatenated with \code{c}.} 40 | 41 | \item{predictor_raster}{character vector (optional); paths to raster data to 42 | import as predictors.} 43 | 44 | \item{predictor_r_names}{character string vector (optional); names for potential predictor 45 | variables in raster format; if not provided \code{perdictor_raster} is used.} 46 | 47 | \item{predictor_vector}{character string vector of object(s) (optional); path(s) 48 | to vector data (ESRI shape) or sp or sf object names to import as predictors. 49 | Different formats (i.e. path and objects) must not be mixed; more than one sf or sp 50 | object must be provided as a list, not concatenated with \code{c}.} 51 | 52 | \item{predictor_v_names}{character vector (optional); names for potential predictor 53 | variables in vector format ; if not provided \code{perdictor_vector} is used.} 54 | } 55 | \value{ 56 | Nothing, the data is loaded into the 'GRASS' session (mapset PERMANENT). 57 | The DEM is stored as raster 'dem', sites as vector 'sites_o', prediction sites 58 | as vector using the original file names with an appended '_o' (without extension), 59 | streams as vector 'streams_o' in the 'GRASS' location. Additionally, predictor 60 | raster map(s) can be read in and are stored in 'GRASS' using either the 61 | original file names (without extension) or using the names provides in 62 | \code{predictor_r_names}. The latter option may be useful if ArcGIS grid data 63 | (typically stored as 'grid_name/w001001.adf') are used. Likewise, predictor 64 | vector maps can be read in from Esri Shape file (given as the full file path) 65 | or as sf or sp objects. Potential predictor data can also be read in later, e.g. 66 | using GRASS commands \href{https://grass.osgeo.org/grass78/manuals/v.import.html}{v.import} 67 | or \href{https://grass.osgeo.org/grass78/manuals/r.in.gdal.html}{r.in.gdal} 68 | (see examples below). 69 | } 70 | \description{ 71 | This function loads a DEM (digital elevation model) and sites data (both required) into the 'GRASS' session. 72 | Optionally, prediction sites and streams data can be loaded and the streams 73 | may be corrected by snapping to prevent lose ends. Likewise, potential predictor 74 | maps (raster or vector format) can be loaded. 75 | } 76 | \details{ 77 | All vector data (sites, streams and potential predictors) is imported 78 | into the current location using \href{https://grass.osgeo.org/grass78/manuals/v.import.html}{v.import}. 79 | Hence, if the projections does not match to the one of the DEM (which was used 80 | to specify the location in \code{\link{setup_grass_environment}}) the maps 81 | are projected and imported on the fly. 82 | All raster data are not transformed but it is assumed that they have the same 83 | projection as the current location. Hence, it is important to make sure that 84 | they all have indeed the same projection (and same cell size) and that the correct 85 | one is set in \code{\link{setup_grass_environment}}. If this condition is not met, 86 | the raster data should be preprocessed before importing. 87 | Use \code{\link{check_projection}} to compare the projection of a raster data set and 88 | the one of the current location (i.e the one of the dem). 89 | } 90 | \note{ 91 | A GRASS session must be initiated and setup before, see \code{\link{setup_grass_environment}}. 92 | 93 | If sites, pred_sites and / or streams are sp objects it is important that they 94 | have a datum defined otherwise the import will not work. Hence, it is e.g. 95 | better to use proj4string = CRS("+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0 +datum=potsdam +units=m +no_defs") 96 | instead of proj4string = CRS("+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0 +ellps=bessel +towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 +units=m +no_defs")) 97 | when defining sp objects. However, please note that proj4 is outdated 98 | (\code{link{https://www.r-spatial.org/r/2020/03/17/wkt.html}}) and will at least rise warnings. 99 | } 100 | \examples{ 101 | \donttest{ 102 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 103 | if(.Platform$OS.type == "windows"){ 104 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 105 | } else { 106 | grass_program_path = "/usr/lib/grass78/" 107 | } 108 | 109 | setup_grass_environment(dem = dem_path, 110 | gisBase = grass_program_path, 111 | remove_GISRC = TRUE, 112 | override = TRUE 113 | ) 114 | gmeta() 115 | 116 | # Load files into GRASS 117 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 118 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 119 | streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 120 | preds_v_path <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS") 121 | preds_r_path <- system.file("extdata", "nc", "landuse_r.tif", package = "openSTARS") 122 | 123 | import_data(dem = dem_path, sites = sites_path, streams = streams_path, 124 | predictor_vector = preds_v_path, predictor_raster = preds_r_path) 125 | 126 | # Plot data 127 | library(sp) 128 | dem <- readRAST("dem", ignore.stderr = TRUE, plugin = FALSE) 129 | sites_orig <- readVECT("sites_o", ignore.stderr = TRUE) 130 | lu <- readRAST("landuse_r", ignore.stderr = TRUE, plugin = FALSE) 131 | # import additional vector data 132 | fp <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS") 133 | execGRASS("v.import", flags = c("overwrite", "quiet"), 134 | parameters = list( 135 | input = fp, 136 | output = "psources", 137 | extent = "region"), # to import into current regien 138 | intern = TRUE, ignore.stderr = TRUE) 139 | 140 | #plot(dem, col = terrain.colors(20)) 141 | plot(dem, col = grey.colors(20)) 142 | points(sites_orig, pch = 4) 143 | ps <- readVECT("psources") 144 | points(ps, bg = "red", pch = 21, col = "grey", cex = 1.5) 145 | 146 | # plot landuse data 147 | library(raster) 148 | par(mfcol = c(1,1), mar = c(5,4,4,2)) 149 | op <- par() 150 | par(xpd = FALSE) 151 | plot(raster(lu), legend = FALSE, xaxt = "n", yaxt = "n", bty = "n", 152 | col = c("red", "goldenrod", "green", "forestgreen","darkgreen", "blue", "lightblue")) 153 | par(xpd = TRUE) 154 | legend("bottom", cex = 0.75, 155 | legend = c("developed", "agriculture", "herbaceous", 156 | "shrubland", "forest", "water", "sediment"), 157 | fill = c("red", "goldenrod", "green", 158 | "forestgreen","darkgreen", "blue", "lightblue"), 159 | horiz = TRUE, inset = -0.175) 160 | par <- op 161 | } 162 | } 163 | \author{ 164 | Eduard Szoecs, \email{eduardszoecs@gmial.com}, Mira Kattwinkel 165 | \email{mira.kattwinkel@gmx.net} 166 | } 167 | -------------------------------------------------------------------------------- /man/import_vector_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/import_data.R 3 | \name{import_vector_data} 4 | \alias{import_vector_data} 5 | \title{Import vector data into GRASS.} 6 | \usage{ 7 | import_vector_data(data, name, layer = NULL, proj_ref_obj = NULL, snap = -1) 8 | } 9 | \arguments{ 10 | \item{data}{character string or object; path to data vector file (shape), postgis 11 | data source name (dsn; see details), or sp or sf data object.} 12 | 13 | \item{name}{string giving the base name of the vector data within the GRASS environment (i.e. output)} 14 | 15 | \item{layer}{character string; default 1, particularly needed if data is a dsn for 16 | importing postgis data (see details)} 17 | 18 | \item{proj_ref_obj}{character; path to a georeferenced data file to be used as reference; only 19 | used if \code{data} is an sf of sp object, then, the two projections are compared to check if 20 | on-the-fly reprojection is needed for importing; typically the dem raster file used in this project.} 21 | 22 | \item{snap}{float; snapping threshold in map units. If != -1 (default) vertices are snapped to other vertices 23 | in this snapping distance during import. If used, the features are automatically cleaned afterwards 24 | (see GRASS tools \href{https://grass.osgeo.org/grass78/manuals/v.import.html}{v.import} 25 | and \href{https://grass.osgeo.org/grass78/manuals/v.clean.html}{v.clean} )} 26 | } 27 | \value{ 28 | Nothing. 29 | } 30 | \description{ 31 | Generic function to import vector data of various formats into the GRASS environment. 32 | } 33 | \details{ 34 | For importing data from Postgis, all data base credentials must be supplied 35 | in \code{data} and the correct \code{layer} and, if the table containing the polygons 36 | are in a specific schema also that one (see example) 37 | } 38 | \examples{ 39 | # import data from Postgis 40 | \dontrun{ 41 | import_vector_data(data = "PG: 'pgname=postgit_DB', 'host=123.45.67.890', 42 | 'port='1234', 'user=username', 'password=password'", 43 | name = "forest", layer = "landuse_schema.forest") 44 | } 45 | 46 | } 47 | \author{ 48 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 49 | } 50 | -------------------------------------------------------------------------------- /man/merge_sites_measurements.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/merge_sites_measurements.R 3 | \name{merge_sites_measurements} 4 | \alias{merge_sites_measurements} 5 | \title{Merge a table with measurements to the sites.} 6 | \usage{ 7 | merge_sites_measurements(measurements, site_id, all_sites = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{measurements}{character string, data.table or data.frame object; 11 | path to table data containing the data or a data.table or data.frame object} 12 | 13 | \item{site_id}{character string; columns name that gives the unique name of the 14 | site. Must be identical in both the sites vector object and the table of 15 | measurements} 16 | 17 | \item{all_sites}{logical; should sites without measurements be preserved (default FALSE)} 18 | 19 | \item{...}{additional arguments to read.table in case \code{measuremtes} is a file path 20 | to table data; see \code{\link{read.table}} for details.} 21 | } 22 | \description{ 23 | After all processing steps are done and before exporting as an SSN object 24 | measurements can be added to the site map. They can contain multiple parameters and 25 | repeated measurements at the same site. 26 | } 27 | \details{ 28 | Measurements are merged to the sites objects based on \code{site_id}. If 29 | there are repeated measurements, point features are duplicated and the 'pid' of the 30 | sites is updated accordingly to be unique. 31 | } 32 | \examples{ 33 | \donttest{ 34 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 35 | if(.Platform$OS.type == "windows"){ 36 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 37 | } else { 38 | grass_program_path = "/usr/lib/grass78/" 39 | } 40 | 41 | setup_grass_environment(dem = dem_path, 42 | gisBase = grass_program_path, 43 | remove_GISRC = TRUE, 44 | override = TRUE 45 | ) 46 | gmeta() 47 | 48 | # Load files into GRASS 49 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 50 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 51 | import_data(dem = dem_path, sites = sites_path) 52 | 53 | # Derive streams from DEM 54 | derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE) 55 | 56 | # Check and correct complex junctions (there are no complex confluences in this 57 | # example date set) 58 | cj <- check_compl_confluences() 59 | if(cj){ 60 | correct_compl_confluences() 61 | } 62 | 63 | # Prepare edges 64 | calc_edges() 65 | 66 | # Prepare site 67 | calc_sites() 68 | 69 | merge_sites_measurements(measurements = system.file( 70 | "extdata", "nc", "obs_data.csv", package = "openSTARS"), 71 | site_id = "site_id", sep = ",", dec = ".") 72 | # note the dublicated rows, and the new columns at the end 73 | sites <- readVECT("sites", ignore.stderr = TRUE) 74 | head(sites@data, n = 6) 75 | } 76 | } 77 | \author{ 78 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net} 79 | } 80 | -------------------------------------------------------------------------------- /man/openSTARS.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/openSTARS.R 3 | \docType{package} 4 | \name{openSTARS} 5 | \alias{openSTARS} 6 | \title{openSTARS: An Open Source Implementation of the 'ArcGIS' Toolbox 'STARS'.} 7 | \description{ 8 | openSTARS provides functions to prepare data so that it can be imported by 9 | the \code{\link[SSN]{SSN}} package for spatial modelling on stream networks. 10 | 'GRASS GIS 7.0' (or greater) with installed addons r.stream.basins, 11 | r.stream.distance, r.stream.order, and r.hydrodem is needed. 12 | } 13 | \section{obs_data.csv}{ 14 | 15 | Artificial observation data with arbitrary measurements. 16 | } 17 | 18 | \section{elev_ned30m.tif}{ 19 | 20 | South-West Wake county National Elevation Data 30m. 21 | } 22 | 23 | \section{sites_nc.shp}{ 24 | 25 | Arbitrary sites along rivers in North Carolina. 26 | } 27 | 28 | \section{streams.shp}{ 29 | 30 | Rivers in North Carolina. 31 | } 32 | 33 | \section{geology.shp}{ 34 | 35 | Geological data. 36 | } 37 | 38 | \section{landuse_r.tif}{ 39 | 40 | Land use date in North Carolina. 41 | } 42 | 43 | \section{lakes.shp}{ 44 | 45 | Artificial lakes (not at topologically correct locations) 46 | } 47 | 48 | \section{pointsources.shp}{ 49 | 50 | Artificial point sources. 51 | } 52 | 53 | \examples{ 54 | \donttest{ 55 | # Initiate and setup GRASS 56 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 57 | if(.Platform$OS.type == "windows"){ 58 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 59 | } else { 60 | grass_program_path = "/usr/lib/grass78/" 61 | } 62 | 63 | setup_grass_environment(dem = dem_path, 64 | gisBase = grass_program_path, 65 | remove_GISRC = TRUE, 66 | override = TRUE 67 | ) 68 | gmeta() 69 | 70 | # Load files into GRASS 71 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 72 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 73 | streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS") 74 | preds_v_path <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS") 75 | preds_r_path <- system.file("extdata", "nc", "landuse_r.tif", package = "openSTARS") 76 | 77 | import_data(dem = dem_path, sites = sites_path, streams = streams_path, 78 | predictor_vector = preds_v_path, predictor_raster = preds_r_path) 79 | 80 | # Derive streams from DEM 81 | # burn in 'streams' 10 meters 82 | derive_streams(burn = 10, accum_threshold = 700, condition = TRUE, clean = TRUE) 83 | 84 | # Check and correct complex confluences (there are no complex confluences in this 85 | # example date set; set accum_threshold in derive_streams to a smaller value 86 | # to create complex confluences) 87 | cj <- check_compl_confluences() 88 | if(cj){ 89 | correct_compl_confluences() 90 | } 91 | 92 | # calculate slope as potential predictor 93 | execGRASS("r.slope.aspect", flags = c("overwrite","quiet"), 94 | parameters = list( 95 | elevation = "dem", 96 | slope = "slope" 97 | )) 98 | 99 | # Prepare edges 100 | calc_edges() 101 | calc_attributes_edges(input_raster = c("slope", "landuse_r"), 102 | stat_rast = c("max", "percent"), 103 | attr_name_rast = c("maxSlo", "luse"), 104 | input_vector = "pointsources", stat_vect = "count", 105 | attr_name_vect = "psource") 106 | 107 | # Prepare site 108 | calc_sites() 109 | 110 | # Usually, only one of the following methods is needed. The exact one takes 111 | # longer to run 112 | # approximate potential predictor variables for each site based on edge values 113 | calc_attributes_sites_approx(input_attr_name = c("maxSlo", "lusep_1", "lusep_2", 114 | "lusep_3", "lusep_4", "lusep_5", 115 | "lusep_6", "lusep_7"), 116 | output_attr_name = c("maxSloA","luse1A", "luse2A", 117 | "luse_3A", "luse4A", "luse5A", 118 | "luse6A", "luse7A"), 119 | stat = c("max", rep("percent", 7))) 120 | 121 | # exact potential predictor variables for each site based on catchments 122 | calc_attributes_sites_exact(input_raster = c("slope", "landuse_r"), 123 | attr_name_rast = c("maxSloEx", "luseE"), 124 | stat_rast = c("max", "percent")) 125 | 126 | # Plot data 127 | library(sp) 128 | dem <- readRAST("dem", ignore.stderr = TRUE, plugin = FALSE) 129 | sites <- readVECT("sites", ignore.stderr = TRUE) 130 | sites_orig <- readVECT("sites_o", ignore.stderr = TRUE) 131 | edges <- readVECT("edges", ignore.stderr = TRUE) 132 | plot(dem, col = terrain.colors(20)) 133 | lines(edges, col = "blue") 134 | points(sites_orig, pch = 4) 135 | cols <- colorRampPalette(c("blue", "red"))(length(sites$H2OArea))[rank(sites$H2OArea)] 136 | points(sites, pch = 16, col = cols) 137 | 138 | # Write data to SSN Folder 139 | ssn_dir <- file.path(tempdir(), "nc.ssn") 140 | export_ssn(ssn_dir, delete_directory = TRUE) 141 | 142 | # Check if all files are ok 143 | library(SSN) 144 | check_ssn(ssn_dir) 145 | 146 | # Load into SSN-package 147 | ssn_obj <- importSSN(ssn_dir, o.write = TRUE) 148 | print(ssn_obj) 149 | } 150 | 151 | 152 | #Datasets shipped with openSTARS 153 | 154 | } 155 | -------------------------------------------------------------------------------- /man/prepare_sites.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calc_sites.R 3 | \name{prepare_sites} 4 | \alias{prepare_sites} 5 | \title{Snap sites to streams and calculate attributes} 6 | \usage{ 7 | prepare_sites(sites_map, locid_c = NULL, pid_c = NULL, maxdist = NULL) 8 | } 9 | \arguments{ 10 | \item{sites_map}{character; name of sites map (observation or prediction sites) 11 | as created by \code{import_data}.} 12 | 13 | \item{locid_c}{character (optional); column name in the sites attribute 14 | table giving a unique site identifier.} 15 | 16 | \item{pid_c}{character (optional); column name in the sites attribute table 17 | that distinguishes between repeated measurements at a sampling site.} 18 | 19 | \item{maxdist}{integer (optional); maximum snapping distance. Sites farther away 20 | from edges will be deleted.} 21 | } 22 | \description{ 23 | Snap sites to streams and calculate attributes 24 | } 25 | \details{ 26 | This function is called by \code{calc_sites} and should not be called directly. 27 | Sites are snapped to the streams and upstream distance is calculated. 28 | } 29 | -------------------------------------------------------------------------------- /man/restrict_network.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/restrict_network.R 3 | \name{restrict_network} 4 | \alias{restrict_network} 5 | \title{Restrict edges to certain stream networks} 6 | \usage{ 7 | restrict_network( 8 | sites = NULL, 9 | keep_netIDs = NULL, 10 | delete_netIDs = NULL, 11 | keep = TRUE, 12 | filename = "edges_o" 13 | ) 14 | } 15 | \arguments{ 16 | \item{sites}{name(s) of sites.} 17 | 18 | \item{keep_netIDs}{numeric (optional); vector of netIDs to keep} 19 | 20 | \item{delete_netIDs}{numeric (optional); vector of netIDs to delete} 21 | 22 | \item{keep}{boolean; should the original 'edges' be saved? Default is TRUE.} 23 | 24 | \item{filename}{character string; file name to save the original edges vector file; 25 | defaults to 'edges_o'.} 26 | } 27 | \value{ 28 | Nothing. The function updates 'edges' and (if keep = TRUE) saves 29 | the original file to the file name provided. 30 | } 31 | \description{ 32 | When the stream network is derived from a DEM, the network will cover the whole 33 | extent of the DEM input. However, the observation sites might be restricted 34 | to a certain area, i.e. to certain networks. This function deletes edges 35 | that belong to networks (based on their netID) without sites (observation or prediction). 36 | This can help to save computation time before calculating edge attributes. 37 | } 38 | \examples{ 39 | \donttest{ 40 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 41 | if(.Platform$OS.type == "windows"){ 42 | grass_program_path = "c:/Program Files/GRASS GIS 7.6" 43 | } else { 44 | grass_program_path = "/usr/lib/grass78/" 45 | } 46 | 47 | setup_grass_environment(dem = dem_path, 48 | gisBase = grass_program_path, 49 | remove_GISRC = TRUE, 50 | override = TRUE 51 | ) 52 | gmeta() 53 | 54 | # Load files into GRASS 55 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 56 | sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS") 57 | lakes_path <- system.file("extdata", "nc", "lakes.shp", package = "openSTARS") 58 | import_data(dem = dem_path, sites = sites_path) 59 | 60 | # Derive streams from DEM 61 | derive_streams(burn = 0, accum_threshold = 100, condition = TRUE, clean = TRUE) 62 | 63 | # Check and correct complex confluences (there are no complex confluences in this 64 | # example date set; set accum_threshold in derive_streams to a smaller value 65 | # to create complex confluences) 66 | cj <- check_compl_confluences() 67 | if(cj){ 68 | correct_compl_confluences() 69 | } 70 | 71 | calc_edges() 72 | calc_sites() 73 | restrict_network(sites = "sites", keep = TRUE, filename = "edges_o") 74 | 75 | # plot 76 | library(sp) 77 | edges <- readVECT('edges', ignore.stderr = TRUE) 78 | edges_o <- readVECT('edges_o', ignore.stderr = TRUE) 79 | sites <- readVECT('sites', ignore.stderr = TRUE) 80 | plot(edges_o, col = "lightblue", lwd = 2) 81 | lines(edges, col = "blue4", lty = 2) 82 | points(sites, pch = 16, col = "red") 83 | legend("topright", col = c("red", "lightblue", "blue4"), lty = c(NA, 1, 2), 84 | lwd = c(NA,2,1), pch = c(16,NA,NA), 85 | legend = c("sites", "edges original", "edges restricted")) 86 | } 87 | } 88 | \author{ 89 | Mira Kattwinkel \email{mira.kattwinkel@gmx.net} 90 | } 91 | -------------------------------------------------------------------------------- /man/setup_grass_environment.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/setup_grass_environment.R 3 | \name{setup_grass_environment} 4 | \alias{setup_grass_environment} 5 | \title{Setup 'GRASS' environment.} 6 | \usage{ 7 | setup_grass_environment(dem, gisBase, epsg = NULL, sites = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{dem}{character; path to DEM.} 11 | 12 | \item{gisBase}{character; the directory path to GRASS binaries and libraries, containing 13 | bin and lib subdirectories among others (see details).} 14 | 15 | \item{epsg}{integer (deprecated); not used any more. Only included for compatibility with previous version.} 16 | 17 | \item{sites}{(deprecated); not used any more. Only included for compatibility with previous version.} 18 | 19 | \item{...}{Optional arguments to be passed to \code{\link[rgrass7]{initGRASS}} (see details).} 20 | } 21 | \value{ 22 | Nothing. A GRASS session is initiated and the 'GRASS' mapset is set to PERMANENT. 23 | The geographical projection, geographical extension, number of columns and number of 24 | rows for the data and the resolution are defined by the dem. They are stored the DEFAULT_WIND file. 25 | } 26 | \description{ 27 | This function sets the 'GRASS' mapset to PERMANENT and sets its projection and extension. 28 | } 29 | \details{ 30 | A GRASS session is initiated using \code{\link[rgrass7]{initGRASS}}. The path to 31 | the GRASS program must be provided as \code{gisBase}. For Linux, this might look like 32 | "/usr/lib/grass78/" and for Windows "c:/Program Files/GRASS GIS 7.8". 33 | Optional arguments are for example 34 | * \code{gisDbase}: the GRASS GISBASE directory for this session; defaults to tempdir() 35 | * \code{location}: name of the location for this session; defaults to tempfile() 36 | * \code{override}: TRUE for allowing to override an existing location. 37 | } 38 | \note{ 39 | It is no longer required to initiate a GRASS session before using \code{\link[rgrass7]{initGRASS}}! 40 | } 41 | \examples{ 42 | \donttest{ 43 | # path to GRASS 44 | if(.Platform$OS.type == "windows"){ 45 | gisbase = "c:/Program Files/GRASS GIS 7.6" 46 | } else { 47 | gisbase = "/usr/lib/grass78/" 48 | } 49 | # path to the dem 50 | dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS") 51 | setup_grass_environment(dem = dem_path, 52 | gisBase = gisbase, 53 | location = "nc_example_location", 54 | override = TRUE) 55 | gmeta() 56 | } 57 | } 58 | \author{ 59 | Mira Kattwinkel, \email{mira.kattwinkel@gmx.net} 60 | } 61 | -------------------------------------------------------------------------------- /man/watershed_memory.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/derive_streams.R 3 | \name{watershed_memory} 4 | \alias{watershed_memory} 5 | \title{Calculate RAM needed for deriving the stream network from DEM} 6 | \usage{ 7 | watershed_memory(dem) 8 | } 9 | \arguments{ 10 | \item{dem}{character; path to DEM raster file.} 11 | } 12 | \value{ 13 | MB of RAM needed to derive the stream network with \code{mem = F} in 14 | \code{\link{derive_streams}}. 15 | } 16 | \description{ 17 | See GRASS function \href{https://grass.osgeo.org/grass78/manuals/r.watershed.html}{r.watershed}. 18 | } 19 | \keyword{internal} 20 | --------------------------------------------------------------------------------