Skip to contents

The sibling of zone_exclude_sql() for the v11 "gate + weight" tier (D11): where a species is kept in a zone but recorded well below its rate elsewhere, encounter_weight() gives a multiplier w in (0, 1) instead of a drop. This resolves the (model, zone, w) rows to (model, cell, w) over the model's cells in the zone, by the cell's majority zone, with critical habitat carved out (never downweighted) exactly as zone_exclude_sql() never excludes it. A weight of 0 is the gate itself, so zone_adjust_sql() can take one relation holding both.

Usage

zone_weight_sql(
  weights,
  src = "model_cell",
  cell_zone = "cell_zone",
  ch_src = NULL,
  id_col = "mdl_seq"
)

Arguments

weights

data frame with id_col, zone_key, w (numeric in 0, 1).

src

character; name of the model-cell relation holding (id_col, cell_id, ...), default "model_cell".

cell_zone

character; name of the relation (cell_id, zone_key) giving each cell's majority zone, typically a temporary table built from cell_zone_major_sql().

ch_src

character or NULL; name of a relation (id_col, cell_id) of critical-habitat cells per model. Rows listed there are never selected. NULL (default) = no guard.

id_col

character; the model id column, "mdl_seq" (v1-v7, default) or "mdl_key" (v8+).

Value

SQL string selecting distinct (id_col, cell_id, w), ordered by id_col, cell_id.

Examples

zone_weight_sql(data.frame(mdl_key = "ms_merge|WORMS:159023", zone_key = "EGOA", w = 0.14),
                id_col = "mdl_key")
#> WITH ex(mdl_key, zone_key, w) AS (
#>   VALUES
#>   ('ms_merge|WORMS:159023', 'EGOA', 0.14)
#> )
#> SELECT DISTINCT s.mdl_key, s.cell_id, ex.w
#> FROM model_cell s
#> JOIN cell_zone cz ON cz.cell_id = s.cell_id
#> JOIN ex ON ex.mdl_key = s.mdl_key AND ex.zone_key = cz.zone_key
#> ORDER BY s.mdl_key, s.cell_id