
Build per-segment per-species mapping_code tokens from schema tables
Source:R/lnk_mapping_code.R
lnk_mapping_code.RdSchema-aware portable wrapper around lnk_pipeline_mapping_code().
Queries table_access, table_habitat (long form), and
table_streams.feature_code, assembles the inputs (pivot habitat
long → wide, build feature_code lookup), calls
lnk_pipeline_mapping_code() for the per-segment token compute, and
optionally writes the result to table_to.
Arguments
- conn
A DBI::DBIConnection to the local pipeline DB.
- table_access
Character. Schema-qualified name of the
streams_accesstable (e.g."working_pars.streams_access"or"fresh_default.streams_access").- table_habitat
Character. Schema-qualified name of a long-form habitat source — either the working-schema
streams_habitattable or the persiststreams_habitat_long_vwview. Must have columnsid_segment,watershed_group_code,species_code,spawning,rearing.- table_streams
Character. Schema-qualified name of the
streamstable, queried forid_segment+feature_code.- aoi
Character. Watershed group code (e.g.
"PARS") — filters all input queries to one WSG.- table_to
Character or
NULL. Optional schema-qualified destination table for the result. When non-NULL,lnk_pipeline_mapping_code()writes the tibble viadbWriteTable(overwrite = TRUE). DefaultNULL— returns-only.- presence
Named logical vector or
NULL. Per-species presence flag foraoi. WhenNULLthe function derives presence from the data: a species is present iff it has at least one habitat row withspawning = TRUEorrearing = TRUE. Pass explicit values to override (e.g. force-include a species for QGIS symbology even when no segments are accessible).- species_resident
Character. Species using the resident flavor of
mapping_code_barrier. Defaultc("bt", "wct"). Pass-through tolnk_pipeline_mapping_code()'sresident_speciesarg.- species_anadromous
Character. Species using the anadromous flavor. Default
c("ch", "cm", "co", "pk", "sk", "st"). Pass-through tolnk_pipeline_mapping_code()'sanadromous_speciesarg.- species_spawn_only
Character. Species without rearing semantics. Default
c("cm", "pk"). Pass-through tolnk_pipeline_mapping_code()'sspawn_only_speciesarg.
Value
Invisibly, the per-segment per-species mapping_code tibble
keyed by id_segment with one mapping_code_<sp> text column per
species in union(species_resident, species_anadromous).
Details
Decouples the mapping_code build from any specific schema layout.
Caller passes explicit table names — the function works against
working-schema tables (mid-pipeline) or persist-schema tables
(ad-hoc rebuild) without modification. The companion view
<persist_schema>.streams_habitat_long_vw (created by
lnk_persist_init()) presents the per-species split as a long-form
shape so table_habitat can point at either layout.
This function replaces the inline assembly previously buried inside
lnk_compare_wsg(). The compare wrapper now goes through this
function via lnk_pipeline_run(..., mapping_code = TRUE). Operators
can also call this directly against persist schema with the tunnel
down — the build is tunnel-independent (the diff vs reference is
separate, see .lnk_compare_wsg_mapping_code_diff).
Tracks link#187 (tunnel decouple + portable build).
See also
Other compare:
lnk_compare_rollup(),
lnk_compare_wsg(),
lnk_parity_annotate()
Examples
if (FALSE) { # \dontrun{
conn <- DBI::dbConnect(
RPostgres::Postgres(),
host = "localhost", port = 5432, dbname = "fwapg",
user = "postgres", password = "postgres")
# Working-schema build during a pipeline run:
lnk_mapping_code(
conn,
table_access = "working_pars.streams_access",
table_habitat = "working_pars.streams_habitat",
table_streams = "working_pars.streams",
aoi = "PARS",
table_to = "working_pars.streams_mapping_code")
# Ad-hoc rebuild against persist (tunnel-free) for QGIS symbology:
lnk_mapping_code(
conn,
table_access = "fresh_default.streams_access",
table_habitat = "fresh_default.streams_habitat_long_vw",
table_streams = "fresh_default.streams",
aoi = "PARS",
table_to = "fresh_default.streams_mapping_code")
} # }