Skip to content

Commit

Permalink
fix #223 (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
smnorris authored Dec 16, 2023
1 parent 336bc2c commit 19cdfcd
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ column,description
``crossings_dnstr``,List of aggregated_crossings_id values for all crossings downstream
``barriers_anthropogenic_dnstr``,List of aggregated_crossings_id values for all anthropogenic barriers downstream
``barriers_anthropogenic_upstr``,List of aggregated_crossings_id values for all anthropogenic barriers upstream
``barriers_anthropogenic_upstr_ch_cm_co_pk_sk``,List of aggregated_crossings_id values for anthropogenic barriers upstream and modelled as potentially accessible to Pacific salmon
``barriers_anthropogenic_upstr_st``,List of aggregated_crossings_id values for all anthropogenic barriers upstream and modelled as potentially accessible to Steelhead
``barriers_anthropogenic_dnstr_count``,Count of anthropogenic barriers downstream
``barriers_anthropogenic_upstr_count``,Count of anthropogenic barriers upstream
``barriers_anthropogenic_upstr_ch_cm_co_pk_sk``,Count of anthropogenic barriers upstream and modelled as potentially accessible to Pacific salmon
``barriers_anthropogenic_upstr_st``,Count of anthropogenic barriers upstream and modelled as potentially accessible to Steelhead
``gradient``,Stream slope at point
``total_network_km``,Total length of stream network upstream of point
``total_stream_km``,Total length of streams and rivers upstream of point (does not include network connectors in lakes etc)
Expand Down
13 changes: 7 additions & 6 deletions model/01_access/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \
# for crossings table, barriers_anthropogenic, note what barriers are upstream/downstream of each other
.make/index_crossings: .make/barriers_anthropogenic
# note all crossings downstream of a crossing
$(PSQL) -c "drop table if exists bcfishpass.crossings_dnstr_crossings"
$(PSQL) -c "drop table if exists bcfishpass.crossings_dnstr_crossings cascade"
$(PSQL) -c "create table bcfishpass.crossings_dnstr_crossings (aggregated_crossings_id text primary key, features_dnstr text[])"
parallel --no-run-if-empty \
"echo \"select bcfishpass.load_dnstr( \
Expand All @@ -169,7 +169,7 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \
$(PSQL) -v wsg={1}" ::: $(WSG)

# note all anthropogenic barriers downstream of a crossing
$(PSQL) -c "drop table if exists bcfishpass.crossings_dnstr_barriers_anthropogenic"
$(PSQL) -c "drop table if exists bcfishpass.crossings_dnstr_barriers_anthropogenic cascade"
$(PSQL) -c "create table bcfishpass.crossings_dnstr_barriers_anthropogenic (aggregated_crossings_id text primary key, features_dnstr text[])"
parallel --no-run-if-empty \
"echo \"select bcfishpass.load_dnstr( \
Expand All @@ -184,7 +184,7 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \
$(PSQL) -v wsg={1}" ::: $(WSG)

# note all anthropogenic barriers upstream of a crossing
$(PSQL) -c "drop table if exists bcfishpass.crossings_upstr_barriers_anthropogenic"
$(PSQL) -c "drop table if exists bcfishpass.crossings_upstr_barriers_anthropogenic cascade"
$(PSQL) -c "create table bcfishpass.crossings_upstr_barriers_anthropogenic (aggregated_crossings_id text primary key, features_upstr text[])"
parallel --no-run-if-empty \
"echo \"select bcfishpass.load_upstr( \
Expand All @@ -199,7 +199,7 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \
$(PSQL) -v wsg={1}" ::: $(WSG)

# note all anthropogenic barriers downstream of an anthropogenic barrier
$(PSQL) -c "drop table if exists bcfishpass.barriers_anthropogenic_dnstr_barriers_anthropogenic"
$(PSQL) -c "drop table if exists bcfishpass.barriers_anthropogenic_dnstr_barriers_anthropogenic cascade"
$(PSQL) -c "create table bcfishpass.barriers_anthropogenic_dnstr_barriers_anthropogenic (barriers_anthropogenic_id text primary key, features_dnstr text[])"
parallel --no-run-if-empty \
"echo \"select bcfishpass.load_dnstr( \
Expand All @@ -213,8 +213,9 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \
:'wsg');\" | \
$(PSQL) -v wsg={1}" ::: $(WSG)

# load data from _dnstr _upstr lookups back into the main tables, drop the lookups
$(PSQL) -f sql/index_crossings.sql
# note upstream anthropogenic barriers per model
$(PSQL) -f sql/crossings_upstr_barriers_anthropogenic_model.sql

touch $@

# -------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
drop table if exists bcfishpass.crossings_upstr_barriers_anthropogenic_model cascade;

create table bcfishpass.crossings_upstr_barriers_anthropogenic_model (
aggregated_crossings_id text primary key,
barriers_anthropogenic_upstr_bt text[],
barriers_anthropogenic_upstr_ch_cm_co_pk_sk text[] ,
barriers_anthropogenic_upstr_ct_dv_rb text[] ,
barriers_anthropogenic_upstr_st text[] ,
barriers_anthropogenic_upstr_wct text[]
);


insert into bcfishpass.crossings_upstr_barriers_anthropogenic_model (
aggregated_crossings_id ,
barriers_anthropogenic_upstr_bt ,
barriers_anthropogenic_upstr_ch_cm_co_pk_sk ,
barriers_anthropogenic_upstr_ct_dv_rb ,
barriers_anthropogenic_upstr_st ,
barriers_anthropogenic_upstr_wct
)

with access_models as (
select
a.aggregated_crossings_id,
s.barriers_bt_dnstr,
s.barriers_ch_cm_co_pk_sk_dnstr,
s.barriers_ct_dv_rb_dnstr,
s.barriers_st_dnstr,
s.barriers_wct_dnstr
from bcfishpass.crossings a
left outer join bcfishpass.streams s
ON s.blue_line_key = a.blue_line_key
AND round(s.downstream_route_measure::numeric, 4) <= round(a.downstream_route_measure::numeric, 4)
AND round(s.upstream_route_measure::numeric, 4) > round(a.downstream_route_measure::numeric, 4)
AND s.watershed_group_code = a.watershed_group_code
),

barriers_unnested as (
select
a.aggregated_crossings_id,
unnest(a.features_upstr) as barrier_upstr
from bcfishpass.crossings_upstr_barriers_anthropogenic a
),

barriers_upstr as (
select
b.aggregated_crossings_id,
b.barrier_upstr,
m.barriers_bt_dnstr,
m.barriers_ch_cm_co_pk_sk_dnstr,
m.barriers_ct_dv_rb_dnstr,
m.barriers_st_dnstr,
m.barriers_wct_dnstr
from barriers_unnested b
left outer join access_models m on b.barrier_upstr = m.aggregated_crossings_id
),

barriers_upstr_per_model as (
select
c.aggregated_crossings_id,
array_agg(barrier_upstr) filter (where u.barriers_bt_dnstr = array[]::text[]) as barriers_upstr_bt,
array_agg(barrier_upstr) filter (where u.barriers_ch_cm_co_pk_sk_dnstr = array[]::text[]) as barriers_upstr_ch_cm_co_pk_sk,
array_agg(barrier_upstr) filter (where u.barriers_ct_dv_rb_dnstr = array[]::text[]) as barriers_upstr_ct_dv_rb,
array_agg(barrier_upstr) filter (where u.barriers_st_dnstr = array[]::text[]) as barriers_upstr_st,
array_agg(barrier_upstr) filter (where u.barriers_wct_dnstr = array[]::text[]) as barriers_upstr_wct
from bcfishpass.crossings c
inner join barriers_upstr u on c.aggregated_crossings_id = u.aggregated_crossings_id
group by c.aggregated_crossings_id
)

select
c.aggregated_crossings_id ,
bpm.barriers_upstr_bt ,
bpm.barriers_upstr_ch_cm_co_pk_sk ,
bpm.barriers_upstr_ct_dv_rb ,
bpm.barriers_upstr_st ,
bpm.barriers_upstr_wct
from bcfishpass.crossings c
left outer join barriers_upstr_per_model bpm
on c.aggregated_crossings_id = bpm.aggregated_crossings_id;
178 changes: 0 additions & 178 deletions model/01_access/sql/index_crossings.sql

This file was deleted.

26 changes: 21 additions & 5 deletions model/02_habitat_linear/sql/views/crossings_vw.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ select distinct on (c.aggregated_crossings_id) -- joining to streams based on m
s.mad_m3s,
array_to_string(c.observedspp_dnstr, ';') as observedspp_dnstr,
array_to_string(c.observedspp_upstr, ';') as observedspp_upstr,
array_to_string(c.crossings_dnstr, ';') as crossings_dnstr,
array_to_string(c.barriers_anthropogenic_dnstr, ';') as barriers_anthropogenic_dnstr,
array_to_string(c.barriers_anthropogenic_upstr, ';') as barriers_anthropogenic_upstr,
coalesce(array_length(c.barriers_anthropogenic_dnstr, 1), 0) as barriers_anthropogenic_dnstr_count,
coalesce(array_length(c.barriers_anthropogenic_upstr, 1), 0) as barriers_anthropogenic_upstr_count,
array_to_string(cd.features_dnstr, ';') as crossings_dnstr,
array_to_string(ad.features_dnstr, ';') as barriers_anthropogenic_dnstr,
array_to_string(au.features_upstr, ';') as barriers_anthropogenic_upstr,
array_to_string(aum.barriers_anthropogenic_upstr_bt, ';') as barriers_anthropogenic_upstr_bt,
array_to_string(aum.barriers_anthropogenic_upstr_ch_cm_co_pk_sk, ';') as barriers_anthropogenic_upstr_ch_cm_co_pk_sk,
array_to_string(aum.barriers_anthropogenic_upstr_st, ';') as barriers_anthropogenic_upstr_st,
array_to_string(aum.barriers_anthropogenic_upstr_wct, ';') as barriers_anthropogenic_upstr_wct,
coalesce(array_length(ad.features_dnstr, 1), 0) as barriers_anthropogenic_dnstr_count,
coalesce(array_length(au.features_upstr, 1), 0) as barriers_anthropogenic_upstr_count,
coalesce(array_length(aum.barriers_anthropogenic_upstr_bt, 1), 0) as barriers_anthropogenic_upstr_count_bt,
coalesce(array_length(aum.barriers_anthropogenic_upstr_ch_cm_co_pk_sk, 1), 0) as barriers_anthropogenic_upstr_count_ch_cm_co_pk_sk,
coalesce(array_length(aum.barriers_anthropogenic_upstr_st, 1), 0) as barriers_anthropogenic_upstr_count_st,
coalesce(array_length(aum.barriers_anthropogenic_upstr_wct, 1), 0) as barriers_anthropogenic_upstr_count_wct,
a.gradient,
a.total_network_km,
a.total_stream_km,
Expand Down Expand Up @@ -221,6 +229,14 @@ select distinct on (c.aggregated_crossings_id) -- joining to streams based on m
h.wct_rearing_belowupstrbarriers_km,
c.geom
from bcfishpass.crossings c
left outer join bcfishpass.crossings_dnstr_crossings cd
on c.aggregated_crossings_id = cd.aggregated_crossings_id
left outer join bcfishpass.crossings_dnstr_barriers_anthropogenic ad
on c.aggregated_crossings_id = ad.aggregated_crossings_id
left outer join bcfishpass.crossings_upstr_barriers_anthropogenic au
on c.aggregated_crossings_id = au.aggregated_crossings_id
left outer join bcfishpass.crossings_upstr_barriers_anthropogenic_model aum
on c.aggregated_crossings_id = aum.aggregated_crossings_id
left outer join bcfishpass.crossings_upstream_access a on c.aggregated_crossings_id = a.aggregated_crossings_id
left outer join bcfishpass.crossings_upstream_habitat h on c.aggregated_crossings_id = h.aggregated_crossings_id
left outer join bcfishpass.streams s on c.linear_feature_id = s.linear_feature_id
Expand Down
23 changes: 18 additions & 5 deletions release/sql/freshwater_fish_habitat_accessibility_model.sql
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,15 @@ select
c.stream_magnitude,
array_to_string(c.observedspp_dnstr, ';') as observedspp_dnstr,
array_to_string(c.observedspp_upstr, ';') as observedspp_upstr,
array_to_string(c.crossings_dnstr, ';') as crossings_dnstr,
array_to_string(c.barriers_anthropogenic_dnstr, ';') as barriers_anthropogenic_dnstr,
array_to_string(c.barriers_anthropogenic_upstr, ';') as barriers_anthropogenic_upstr,
coalesce(array_length(barriers_anthropogenic_dnstr, 1), 0) as barriers_anthropogenic_dnstr_count,
coalesce(array_length(barriers_anthropogenic_upstr, 1), 0) as barriers_anthropogenic_upstr_count,
array_to_string(cd.features_dnstr, ';') as crossings_dnstr,
array_to_string(ad.features_dnstr, ';') as barriers_anthropogenic_dnstr,
array_to_string(au.features_upstr, ';') as barriers_anthropogenic_upstr,
array_to_string(aum.barriers_anthropogenic_upstr_ch_cm_co_pk_sk, ';') as barriers_anthropogenic_upstr_ch_cm_co_pk_sk,
array_to_string(aum.barriers_anthropogenic_upstr_st, ';') as barriers_anthropogenic_upstr_st,
coalesce(array_length(ad.features_dnstr, 1), 0) as barriers_anthropogenic_dnstr_count,
coalesce(array_length(au.features_upstr, 1), 0) as barriers_anthropogenic_upstr_count,
coalesce(array_length(aum.barriers_anthropogenic_upstr_ch_cm_co_pk_sk, 1), 0) as barriers_anthropogenic_upstr_count_ch_cm_co_pk_sk,
coalesce(array_length(aum.barriers_anthropogenic_upstr_st, 1), 0) as barriers_anthropogenic_upstr_count_st,
r.gradient,
r.total_network_km,
r.total_stream_km,
Expand Down Expand Up @@ -276,6 +280,15 @@ select
r.st_belowupstrbarriers_slopeclass30_km,
c.geom
from bcfishpass.crossings c
left outer join bcfishpass.crossings_dnstr_crossings cd
on c.aggregated_crossings_id = cd.aggregated_crossings_id
left outer join bcfishpass.crossings_dnstr_barriers_anthropogenic ad
on c.aggregated_crossings_id = ad.aggregated_crossings_id
left outer join bcfishpass.crossings_upstr_barriers_anthropogenic au
on c.aggregated_crossings_id = au.aggregated_crossings_id
left outer join bcfishpass.crossings_upstr_barriers_anthropogenic_model aum
on c.aggregated_crossings_id = aum.aggregated_crossings_id
left outer join bcfishpass.crossings_upstream_access r
on c.aggregated_crossings_id = r.aggregated_crossings_id;

create index on bcfishpass.freshwater_fish_habitat_accessibility_model_crossings_vw using gist (geom);

0 comments on commit 19cdfcd

Please sign in to comment.