Skip to content

Commit

Permalink
update BT query and add PARS to testing db
Browse files Browse the repository at this point in the history
  • Loading branch information
smnorris committed Jul 10, 2024
1 parent cece7e2 commit af605da
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 36 deletions.
149 changes: 114 additions & 35 deletions model/01_access/sql/model_access_bt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,21 @@ with all_barriers as
where watershed_group_code = :'wsg'
),

-- BT observations, plus salmon/steelhead as any features passable by salmon/steehead
-- should also be passable by BT
obs as
-- BT observations, plus salmon/steelhead
-- (any features passable by salmon/steehead should also be passable by BT)
obs_upstr as
(
select *
from bcfishpass.observations_vw
where species_code in ('BT','CH','CM','CO','PK','SK','ST')
),

barriers as
(
select distinct
select
b.barrier_id,
b.barrier_type,
b.barrier_name,
b.linear_feature_id,
b.blue_line_key,
b.downstream_route_measure,
b.wscode_ltree,
b.localcode_ltree,
b.watershed_group_code,
b.geom
from all_barriers b
left outer join obs o
o.species_code as spp,
o.fish_observation_point_id as obs,
o.observation_date as obs_dt
from barriers b
inner join bcfishpass.observations_vw o
on fwa_upstream(
b.blue_line_key,
b.downstream_route_measure,
Expand All @@ -77,26 +69,98 @@ barriers as
o.downstream_route_measure,
o.wscode_ltree,
o.localcode_ltree,
False,
1
false,
20 -- a large tolerance to discard observations at more or less the same location as the barrier (within 20m)
)
where o.species_code is null
-- do not bother counting observations upstream of barriers that have been noted as barriers in the user control table
left outer join bcfishpass.user_barriers_definite_control bc
on b.blue_line_key = bc.blue_line_key and abs(b.downstream_route_measure - bc.downstream_route_measure) < 1
where o.species_code in ('BT','CH','CM','CO','PK','SK','ST')
and bc.barrier_ind is null
),

union all
obs_upstr_n as
(
select
o.barrier_id,
count(o.obs) as n_obs
from obs_upstr o
where o.spp in ('BT','CH','CM','CO','PK','SK','ST')
group by o.barrier_id
),

-- include *all* user added features, even those below observations
-- exclude barriers belown known spawning/rearing habitat
habitat as (
select
barrier_type,
barrier_name,
linear_feature_id,
blue_line_key,
downstream_route_measure,
wscode_ltree,
localcode_ltree,
watershed_group_code,
geom
from bcfishpass.barriers_user_definite
where watershed_group_code = :'wsg'
h.blue_line_key,
h.upstream_route_measure,
s.wscode_ltree,
s.localcode_ltree,
h.watershed_group_code,
h.species_code
from bcfishpass.user_habitat_classification h
inner join whse_basemapping.fwa_stream_networks_sp s
ON s.blue_line_key = h.blue_line_key
and round(h.upstream_route_measure::numeric) >= round(s.downstream_route_measure::numeric)
and round(h.upstream_route_measure::numeric) <= round(s.upstream_route_measure::numeric)
where h.habitat_ind is true
and h.species_code in ('BT','CH','CM','CO','PK','SK','ST')
and s.watershed_group_code = :'wsg'
),

hab_upstr as
(
select
b.barrier_id,
array_agg(species_code) as species_codes
from barriers b
inner join habitat h
on fwa_upstream(
b.blue_line_key,
b.downstream_route_measure,
b.wscode_ltree,
b.localcode_ltree,
h.blue_line_key,
h.upstream_route_measure,
h.wscode_ltree,
h.localcode_ltree,
false,
20 -- a large tolerance to discard habitat that ends at more or less the same location as the barrier (within 20m)
)
group by b.barrier_id
),


barriers_filtered as (
select
b.barrier_id as barrier_load_id,
b.barrier_type,
b.barrier_name,
b.linear_feature_id,
b.blue_line_key,
b.downstream_route_measure,
b.wscode_ltree,
b.localcode_ltree,
b.watershed_group_code,
b.geom
from barriers b
left outer join obs_upstr_n as o on b.barrier_id = o.barrier_id
left outer join hab_upstr h on b.barrier_id = h.barrier_id
where watershed_group_code = any(
array(
select watershed_group_code
from bcfishpass.wsg_species_presence
where bt is true
)
)
-- do not include barriers with
-- - any BT (or salmon/steelhead) observation upstream
-- - confirmed BT (or salmon/steelhead) habitat upstream
and
(
o.n_obs is null and
h.species_codes is null
)
)

insert into bcfishpass.barriers_bt
Expand All @@ -114,7 +178,7 @@ insert into bcfishpass.barriers_bt
)
-- add a primary key guaranteed to be unique provincially (presuming unique blkey/measure values within 1m)
select
(((blue_line_key::bigint + 1) - 354087611) * 10000000) + round(downstream_route_measure::bigint) as barrier_load_id,
barrier_load_id,
barrier_type,
barrier_name,
linear_feature_id,
Expand All @@ -132,4 +196,19 @@ where watershed_group_code = any(
where bt is true
)
)
union all
-- include *all* user added features, even those below observations
select
(((blue_line_key::bigint + 1) - 354087611) * 10000000) + round(downstream_route_measure::bigint) as barrier_load_id,
barrier_type,
barrier_name,
linear_feature_id,
blue_line_key,
downstream_route_measure,
wscode_ltree,
localcode_ltree,
watershed_group_code,
geom
from bcfishpass.barriers_user_definite
where watershed_group_code = :'wsg'
on conflict do nothing;
1 change: 1 addition & 0 deletions parameters/example_testing/parameters_habitat_method.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ COWN,cw
ELKR,cw
HORS,mad
LNIC,mad
PARS,cw
SANJ,cw
VICT,cw
2 changes: 1 addition & 1 deletion test/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -euxo pipefail

DATABASE_URL=postgresql://postgres@localhost:5432/bcfishpass_test
PSQL="psql $DATABASE_URL -v ON_ERROR_STOP=1"
WSGS="BELA\nBULK\nCOWN\nELKR\nHORS\nLNIC\nSANJ\nVICT" # edit here to adjust testing watersheds (could pull from parameters/example_testing/parameters_habitat_method)
WSGS="BELA\nBULK\nCOWN\nELKR\nHORS\nLNIC\nPARS\nSANJ\nVICT" # edit here to adjust testing watersheds (could pull from parameters/example_testing/parameters_habitat_method)

createdb bcfishpass_test

Expand Down

0 comments on commit af605da

Please sign in to comment.