Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BT update #542

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 118 additions & 55 deletions model/01_access/sql/model_access_bt.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
with all_barriers as
with barriers as
(
select
barriers_gradient_id as barrier_id,
barrier_type,
barrier_name,
linear_feature_id,
Expand All @@ -17,6 +18,7 @@ with all_barriers as
union all

select
barriers_falls_id as barrier_id,
barrier_type,
barrier_name,
linear_feature_id,
Expand All @@ -32,6 +34,7 @@ with all_barriers as
union all

select
barriers_subsurfaceflow_id as barrier_id,
barrier_type,
barrier_name,
linear_feature_id,
Expand All @@ -45,29 +48,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 +72,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,
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 or o.n_obs < 1) and
h.species_codes is null
)
)

insert into bcfishpass.barriers_bt
Expand All @@ -112,24 +179,20 @@ insert into bcfishpass.barriers_bt
watershed_group_code,
geom
)
-- 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_type,
barrier_name,
linear_feature_id,
blue_line_key,
downstream_route_measure,
wscode_ltree,
localcode_ltree,
watershed_group_code,
geom
from barriers b
where watershed_group_code = any(
array(
select watershed_group_code
from bcfishpass.wsg_species_presence
where bt is true
)
)
select * from barriers_filtered
union all
-- include *all* user added features, even those below observations
select
barriers_user_definite_id 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
Loading