| Title: | Classify Fluvial Mesohabitats from Depth and Velocity |
|---|---|
| Description: | Classifies hydraulic conditions into nominal fluvial mesohabitat categories using water depth and velocity. It implements the eight-class scheme in the preprint by Cordero and Harris <doi:10.2139/ssrn.7100727>, supports validated rectangular custom schemes, and works with tabular and spatial data through 'terra'. Outputs describe hydraulic classes and do not by themselves establish biological habitat quality or species occurrence. |
| Authors: | Elvin Cordero [aut, cre] (ORCID: <https://orcid.org/0009-0003-8025-283X>) |
| Maintainer: | Elvin Cordero <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-09-27 09:48:27 UTC |
| Source: | https://github.com/el-cordero/hydromeso |
Main dispatcher for numeric values, tables, spatial vectors, and rasters. All forms use the same lower-inclusive, upper-exclusive rule engine.
classify_mesohabitat( x, velocity = NULL, depth_col = NULL, velocity_col = NULL, ... )classify_mesohabitat( x, velocity = NULL, depth_col = NULL, velocity_col = NULL, ... )
x |
Numeric depth values, a table, a |
velocity |
Numeric velocity values or a velocity raster/path. |
depth_col, velocity_col
|
Table/vector column selectors. |
... |
Arguments passed to the type-specific classifier. |
The corresponding classified data frame, SpatVector, or
categorical SpatRaster.
classify_mesohabitat_values(), classify_mesohabitat_table(),
classify_mesohabitat_vector(), classify_mesohabitat_raster()
classify_mesohabitat(c(0.2, 0.8), c(0.1, 0.4))classify_mesohabitat(c(0.2, 0.8), c(0.1, 0.4))
Supports separate rasters or a selected pair in one multilayer raster. Depth is always the geometry template. Continuous velocity is aligned only when explicitly requested and always with bilinear interpolation.
classify_mesohabitat_raster( depth, velocity = NULL, scheme = meso_scheme_default(), depth_layer = NULL, velocity_layer = NULL, align = c("error", "velocity_to_depth"), aoi = NULL, mask_aoi = TRUE, assume_crs = NULL, dry_threshold = NULL, invalid = c("error", "NA"), filename = "", overwrite = FALSE, ... )classify_mesohabitat_raster( depth, velocity = NULL, scheme = meso_scheme_default(), depth_layer = NULL, velocity_layer = NULL, align = c("error", "velocity_to_depth"), aoi = NULL, mask_aoi = TRUE, assume_crs = NULL, dry_threshold = NULL, invalid = c("error", "NA"), filename = "", overwrite = FALSE, ... )
depth |
A |
velocity |
A separate |
scheme |
A validated |
depth_layer, velocity_layer
|
Optional layer names or positions. |
align |
|
aoi |
Optional |
mask_aoi |
If |
assume_crs |
Optional CRS assigned only to inputs missing a CRS. This does not transform coordinates and emits a message. |
dry_threshold, invalid
|
Classification controls. |
filename |
Optional output filename. |
overwrite |
Permit overwriting |
... |
Additional |
A categorical integer SpatRaster on the (possibly AOI-cropped)
depth geometry, with a complete class table.
classify_mesohabitat_series(), write_mesohabitat()
d <- terra::rast(nrows = 2, ncols = 4, xmin = 0, xmax = 4, ymin = 0, ymax = 2, crs = "EPSG:32615", vals = c(0.2, 0.8, 1.5, 0.2, 0.2, 0.8, 0.8, 1.5)) v <- d terra::values(v) <- c(0.1, 0.1, 0.1, 0.4, 0.8, 0.4, 0.8, 0.8) classify_mesohabitat_raster(d, v)d <- terra::rast(nrows = 2, ncols = 4, xmin = 0, xmax = 4, ymin = 0, ymax = 2, crs = "EPSG:32615", vals = c(0.2, 0.8, 1.5, 0.2, 0.2, 0.8, 0.8, 1.5)) v <- d terra::values(v) <- c(0.1, 0.1, 0.1, 0.4, 0.8, 0.4, 0.8, 0.8) classify_mesohabitat_raster(d, v)
Named scenarios are paired by unique names. Unnamed inputs are rejected unless positional pairing is explicitly requested.
classify_mesohabitat_series( depth, velocity, scenario_names = NULL, positional = FALSE, scheme = meso_scheme_default(), ... )classify_mesohabitat_series( depth, velocity, scenario_names = NULL, positional = FALSE, scheme = meso_scheme_default(), ... )
depth, velocity
|
Multilayer |
scenario_names |
Optional unique names that explicitly define both lists. |
positional |
Permit explicit position-based pairing when names are absent. |
scheme |
A |
... |
Passed to |
A categorical multilayer SpatRaster, one layer per scenario.
mesohabitat_from_median_hydraulics(), modal_mesohabitat()
d <- terra::rast(nrows = 1, ncols = 2, vals = c(0.2, 0.8), crs = "EPSG:32615") v <- terra::rast(d); terra::values(v) <- c(0.1, 0.4) classify_mesohabitat_series(list(low = d), list(low = v))d <- terra::rast(nrows = 1, ncols = 2, vals = c(0.2, 0.8), crs = "EPSG:32615") v <- terra::rast(d); terra::values(v) <- c(0.1, 0.4) classify_mesohabitat_series(list(low = d), list(low = v))
Classify a table of depth and velocity observations
classify_mesohabitat_table( x, depth_col, velocity_col, scheme = meso_scheme_default(), class_col = "mesohabitat_class", label_col = "mesohabitat", keep_input = TRUE, overwrite = FALSE, dry_threshold = NULL, invalid = c("error", "NA"), ... )classify_mesohabitat_table( x, depth_col, velocity_col, scheme = meso_scheme_default(), class_col = "mesohabitat_class", label_col = "mesohabitat", keep_input = TRUE, overwrite = FALSE, dry_threshold = NULL, invalid = c("error", "NA"), ... )
x |
A data frame, matrix, or path to a CSV file. |
depth_col, velocity_col
|
Column names or one-based positions. |
scheme |
A validated |
class_col, label_col
|
Names for appended output fields. |
keep_input |
Keep all input columns; if |
overwrite |
Permit replacing existing output columns. |
dry_threshold, invalid
|
Passed to |
... |
Additional arguments passed to |
A data frame in the original row order.
classify_mesohabitat_vector(), write_mesohabitat()
x <- data.frame(site = letters[1:3], d = c(0.2, 0.8, 2), v = c(0.1, 0.4, 0.7)) classify_mesohabitat_table(x, "d", "v")x <- data.frame(site = letters[1:3], d = c(0.2, 0.8, 2), v = c(0.1, 0.4, 0.7)) classify_mesohabitat_table(x, "d", "v")
Applies one authoritative rule-table engine. Lower bounds are inclusive, upper bounds are exclusive, missing inputs remain missing, and zero depth and velocity are classified mathematically (Class 1 under the default scheme).
classify_mesohabitat_values( depth, velocity, scheme = meso_scheme_default(), dry_threshold = NULL, invalid = c("error", "NA") )classify_mesohabitat_values( depth, velocity, scheme = meso_scheme_default(), dry_threshold = NULL, invalid = c("error", "NA") )
depth, velocity
|
Numeric vectors in the units declared by |
scheme |
A validated |
dry_threshold |
|
invalid |
Either |
A data frame containing depth, velocity, integer
mesohabitat_class, and unordered factor mesohabitat.
Cordero, E. and Harris, A. (Preprint). SSRN. doi:10.2139/ssrn.7100727.
classify_mesohabitat(), classify_mesohabitat_table(),
convert_hydraulic_units()
classify_mesohabitat_values(c(0.2, 0.61, 1.37), c(0.1, 0.61, 0.3))classify_mesohabitat_values(c(0.2, 0.61, 1.37), c(0.1, 0.61, 0.3))
Geometry, CRS, feature order, and existing attributes are retained. Points, lines, and polygons are accepted because classification uses attributes.
classify_mesohabitat_vector( x, depth_col, velocity_col, scheme = meso_scheme_default(), class_col = "mesohabitat_class", label_col = "mesohabitat", overwrite = FALSE, dry_threshold = NULL, invalid = c("error", "NA"), layer = NULL, ... )classify_mesohabitat_vector( x, depth_col, velocity_col, scheme = meso_scheme_default(), class_col = "mesohabitat_class", label_col = "mesohabitat", overwrite = FALSE, dry_threshold = NULL, invalid = c("error", "NA"), layer = NULL, ... )
x |
A |
depth_col, velocity_col
|
Attribute names or positions. |
scheme, class_col, label_col, overwrite, dry_threshold, invalid
|
As in
|
layer |
Optional layer name when reading a multi-layer vector dataset. |
... |
Additional arguments passed to |
A terra::SpatVector with integer class and label attributes.
classify_mesohabitat_raster(), write_mesohabitat()
x <- data.frame(x = 1:3, y = 1:3, depth = c(0.2, 0.8, 2), velocity = c(0.1, 0.4, 0.7)) v <- terra::vect(x, geom = c("x", "y"), crs = "EPSG:32615") classify_mesohabitat_vector(v, "depth", "velocity")x <- data.frame(x = 1:3, y = 1:3, depth = c(0.2, 0.8, 2), velocity = c(0.1, 0.4, 0.7)) v <- terra::vect(x, geom = c("x", "y"), crs = "EPSG:32615") classify_mesohabitat_vector(v, "depth", "velocity")
Reports nominal transitions, gains and losses by class, unchanged area, and percentage changed. Numeric differences between IDs are never interpreted.
compare_mesohabitat(before, after, align = c("error", "after_to_before"))compare_mesohabitat(before, after, align = c("error", "after_to_before"))
before, after
|
Single-layer classified |
align |
|
A list of ordinary data frames and scalar area metrics.
Explicitly converts values before classification. The default scheme remains defined at exactly 0.61 and 1.37 m and 0.30 and 0.61 m/s; converted imperial values can differ slightly at rounded published boundaries.
convert_hydraulic_units( depth = NULL, velocity = NULL, depth_from = "m", depth_to = "m", velocity_from = "m/s", velocity_to = "m/s" )convert_hydraulic_units( depth = NULL, velocity = NULL, depth_from = "m", depth_to = "m", velocity_from = "m/s", velocity_to = "m/s" )
depth, velocity
|
Optional numeric values. |
depth_from, depth_to
|
One of |
velocity_from, velocity_to
|
One of |
A list with converted depth and velocity.
convert_hydraulic_units(depth = 2, velocity = 1, depth_from = "ft", velocity_from = "ft/s")convert_hydraulic_units(depth = 2, velocity = 1, depth_from = "ft", velocity_from = "ft/s")
A small redistributable data frame with every default mesohabitat class, exact and adjacent threshold cases, coordinates, and missing values. Depth is in metres and velocity is in metres per second. Coordinates are synthetic UTM Zone 15N metres (EPSG:32615).
hydromeso_examplehydromeso_example
A data frame with 18 rows and four variables:
Synthetic easting.
Synthetic northing.
Water depth in metres.
Velocity in metres per second.
Created synthetically for this package.
hydromeso_example classify_mesohabitat_table(hydromeso_example, "depth", "velocity")hydromeso_example classify_mesohabitat_table(hydromeso_example, "depth", "velocity")
Creates and validates a rectangular classification in depth-velocity space. Every lower bound is inclusive and every upper bound is exclusive. Class IDs are nominal identifiers, not ecological ranks.
meso_scheme( rules, name = "Custom mesohabitat scheme", description = NULL, depth_units = "m", velocity_units = "m/s", citation = NULL, source_doi = NULL, allow_gaps = FALSE, allow_negative = FALSE )meso_scheme( rules, name = "Custom mesohabitat scheme", description = NULL, depth_units = "m", velocity_units = "m/s", citation = NULL, source_doi = NULL, allow_gaps = FALSE, allow_negative = FALSE )
rules |
A data frame with |
name |
Nonempty scheme name. |
description |
Optional description. |
depth_units, velocity_units
|
Nonempty unit labels. |
citation |
Optional source citation. |
source_doi |
Optional source DOI. |
allow_gaps |
Whether uncovered regions at nonnegative depth and velocity are permitted. |
allow_negative |
Whether negative rule minima are permitted. |
An object of class meso_scheme.
validate_meso_scheme(), meso_scheme_default(),
classify_mesohabitat_values()
rules <- data.frame( class_id = c(1L, 2L), label = c("Slow", "Fast"), depth_min = c(0, 0), depth_max = c(Inf, Inf), velocity_min = c(0, 0.5), velocity_max = c(0.5, Inf) ) meso_scheme(rules, name = "Two velocity classes")rules <- data.frame( class_id = c(1L, 2L), label = c("Slow", "Fast"), depth_min = c(0, 0), depth_max = c(Inf, Inf), velocity_min = c(0, 0.5), velocity_max = c(0.5, Inf) ) meso_scheme(rules, name = "Two velocity classes")
Returns the exact depth-velocity classification used by Cordero and Harris (Preprint). The broader depth-velocity framework is informed by Aadland (1993), whose paper describes six habitat types and should not be read as the source of the two added high-velocity class names. Inputs must be metres and metres per second unless explicitly converted before classification.
meso_scheme_default()meso_scheme_default()
A validated meso_scheme with eight nominal classes.
Cordero, E. and Harris, A. (Preprint). Semi-Supervised and Supervised Machine Learning Approaches to Predicting Fluvial Mesohabitats from Satellite Data. SSRN. doi:10.2139/ssrn.7100727.
Aadland, L. P. (1993). Stream Habitat Types: Their Fish Assemblages and Relationship to Flow. North American Journal of Fisheries Management, 13, 790-806. doi:10.1577/1548-8675(1993)013<0790:SHTTFA>2.3.CO;2.
meso_scheme(), plot_meso_scheme()
meso_scheme_default()meso_scheme_default()
Extract the rules from a mesohabitat scheme
meso_scheme_rules(x)meso_scheme_rules(x)
x |
A |
A plain data frame containing the rule table.
meso_scheme_rules(meso_scheme_default())meso_scheme_rules(meso_scheme_default())
Loads a compact pair of depth and velocity GeoTIFFs derived from the 31 May 2022 HEC-RAS model output used by Cordero and Harris (Preprint). The sample shows the Big Blue-Kansas Rivers confluence near Manhattan, Kansas. Source cells were cropped to the manuscript's detailed visualization extent, aggregated from 3-foot to 18-foot cells, and converted to metres and metres per second. The raster geometry remains in the source project's documented NAD 1983 (CORS96) StatePlane Kansas North coordinate system.
mesohabitat_example_rasters(paths = FALSE)mesohabitat_example_rasters(paths = FALSE)
paths |
Logical. If |
A named list with depth and velocity rasters or file paths.
Cordero, E. and Harris, A. (Preprint). Semi-Supervised and Supervised Machine Learning Approaches to Predicting Fluvial Mesohabitats from Satellite Data. SSRN. doi:10.2139/ssrn.7100727.
x <- mesohabitat_example_rasters() classify_mesohabitat_raster(x$depth, x$velocity)x <- mesohabitat_example_rasters() classify_mesohabitat_raster(x$depth, x$velocity)
Create a synthetic example spatial vector
mesohabitat_example_vector()mesohabitat_example_vector()
A point terra::SpatVector in EPSG:32615 made from
hydromeso_example.
mesohabitat_example_vector()mesohabitat_example_vector()
Calculates cellwise median depth and median velocity, then classifies those hydraulic surfaces. This is not a numeric median of nominal class IDs and should not be called "median mesohabitat".
mesohabitat_from_median_hydraulics( depth, velocity, na.rm = TRUE, scheme = meso_scheme_default(), ... )mesohabitat_from_median_hydraulics( depth, velocity, na.rm = TRUE, scheme = meso_scheme_default(), ... )
depth, velocity
|
Multilayer |
na.rm |
Remove missing values in the cellwise medians. |
scheme |
A |
... |
Passed to |
A named list containing median_depth, median_velocity, and
mesohabitat_from_median_hydraulics.
classify_mesohabitat_series(), modal_mesohabitat()
Stable mesohabitat class colors
mesohabitat_palette(scheme = meso_scheme_default())mesohabitat_palette(scheme = meso_scheme_default())
scheme |
A |
A named character vector keyed by class label.
Computes the most frequent nominal class per cell. It never calculates a numeric median of class IDs.
modal_mesohabitat(x, ties = c("NA", "lowest_class"), na.rm = TRUE)modal_mesohabitat(x, ties = c("NA", "lowest_class"), na.rm = TRUE)
x |
A classified multilayer |
ties |
|
na.rm |
Ignore missing scenarios. |
A single categorical SpatRaster.
Plot a mesohabitat classification scheme
plot_meso_scheme( scheme = meso_scheme_default(), xlim = NULL, ylim = NULL, reverse_depth_axis = FALSE, palette = mesohabitat_palette(scheme), ... )plot_meso_scheme( scheme = meso_scheme_default(), xlim = NULL, ylim = NULL, reverse_depth_axis = FALSE, palette = mesohabitat_palette(scheme), ... )
scheme |
A |
xlim, ylim
|
Optional finite velocity and depth limits. |
reverse_depth_axis |
Reverse the displayed depth axis only when |
palette |
Named class colors. |
... |
Additional arguments passed to |
The scheme, invisibly.
Plot classified mesohabitat output
plot_mesohabitat( x, depth_col = "depth", velocity_col = "velocity", class_col = "mesohabitat_class", label_col = "mesohabitat", scheme = .scheme_from_object(x), palette = mesohabitat_palette(scheme), legend = TRUE, legend_ncol = 4, legend_cex = 0.75, ... )plot_mesohabitat( x, depth_col = "depth", velocity_col = "velocity", class_col = "mesohabitat_class", label_col = "mesohabitat", scheme = .scheme_from_object(x), palette = mesohabitat_palette(scheme), legend = TRUE, legend_ncol = 4, legend_cex = 0.75, ... )
x |
A classified table, |
depth_col, velocity_col
|
Table columns for depth and velocity. |
class_col, label_col
|
Classification columns. |
scheme |
A |
palette |
Named colors. |
legend |
Show one shared legend below the plot or plot panels. |
legend_ncol |
Number of columns in the shared legend. |
legend_cex |
Legend text size multiplier. |
... |
Additional plotting arguments. |
x, invisibly.
Summarizes classified tables, vectors, rasters, and raster scenarios. Raster
areas use terra::cellSize() and are valid for projected or geographic grids.
summarize_mesohabitat( x, class_col = "mesohabitat_class", label_col = "mesohabitat", denominator = c("classified", "non_na", "mask"), analysis_mask = NULL )summarize_mesohabitat( x, class_col = "mesohabitat_class", label_col = "mesohabitat", denominator = c("classified", "non_na", "mask"), analysis_mask = NULL )
x |
A classified data frame, |
class_col, label_col
|
Classification field names for tables/vectors. |
denominator |
Raster percentage denominator: |
analysis_mask |
Optional raster mask. |
An ordinary data frame.
Performs deterministic breakpoint-based checks of rule structure, metadata, overlaps, gaps, and reachability. The nonnegative depth-velocity plane is the required domain unless gaps are allowed. No random sampling is used.
validate_meso_scheme(x)validate_meso_scheme(x)
x |
A |
The validated object, invisibly.
validate_meso_scheme(meso_scheme_default())validate_meso_scheme(meso_scheme_default())
Dispatches to a categorical GeoTIFF or other raster format, a terra vector
format, or CSV. Shapefile output uses short safe class field names to avoid
silent truncation.
write_mesohabitat( x, filename, overwrite = FALSE, sidecar = TRUE, create_dir = FALSE, scheme = .scheme_from_object(x), ... )write_mesohabitat( x, filename, overwrite = FALSE, sidecar = TRUE, create_dir = FALSE, scheme = .scheme_from_object(x), ... )
x |
A classified data frame, |
filename |
Output path. |
overwrite |
Permit replacement. |
sidecar |
Write a CSV class table beside the output. |
create_dir |
Create missing parent directories only when |
scheme |
A |
... |
Format-specific writer arguments. |
The normalized output path, invisibly.