Skip to content

Commit

Permalink
Revert "fix #223 (#431)"
Browse files Browse the repository at this point in the history
This reverts commit 19cdfcd.
  • Loading branch information
smnorris committed Dec 17, 2023
1 parent 19cdfcd commit 8734ff8
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ 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: 6 additions & 7 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 cascade"
$(PSQL) -c "drop table if exists bcfishpass.crossings_dnstr_crossings"
$(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 cascade"
$(PSQL) -c "drop table if exists bcfishpass.crossings_dnstr_barriers_anthropogenic"
$(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 cascade"
$(PSQL) -c "drop table if exists bcfishpass.crossings_upstr_barriers_anthropogenic"
$(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 cascade"
$(PSQL) -c "drop table if exists bcfishpass.barriers_anthropogenic_dnstr_barriers_anthropogenic"
$(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,9 +213,8 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \
:'wsg');\" | \
$(PSQL) -v wsg={1}" ::: $(WSG)

# note upstream anthropogenic barriers per model
$(PSQL) -f sql/crossings_upstr_barriers_anthropogenic_model.sql

# load data from _dnstr _upstr lookups back into the main tables, drop the lookups
$(PSQL) -f sql/index_crossings.sql
touch $@

# -------------------------------------------------------------------------------------
Expand Down

This file was deleted.

178 changes: 178 additions & 0 deletions model/01_access/sql/index_crossings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
alter table bcfishpass.crossings add column if not exists crossings_dnstr text[];
alter table bcfishpass.crossings add column if not exists barriers_anthropogenic_dnstr text[];
alter table bcfishpass.crossings add column if not exists barriers_anthropogenic_upstr text[];

drop table if exists bcfishpass.crossings_index_tmp;
create table bcfishpass.crossings_index_tmp (like bcfishpass.crossings including all);

insert into bcfishpass.crossings_index_tmp (
aggregated_crossings_id ,
stream_crossing_id ,
dam_id ,
user_barrier_anthropogenic_id ,
modelled_crossing_id ,
crossing_source ,
crossing_feature_type ,
pscis_status ,
crossing_type_code ,
crossing_subtype_code ,
modelled_crossing_type_source ,
barrier_status ,
pscis_road_name ,
pscis_stream_name ,
pscis_assessment_comment ,
pscis_assessment_date ,
pscis_final_score ,
transport_line_structured_name_1 ,
transport_line_type_description ,
transport_line_surface_description ,
ften_forest_file_id ,
ften_file_type_description ,
ften_client_number ,
ften_client_name ,
ften_life_cycle_status_code ,
rail_track_name ,
rail_owner_name ,
rail_operator_english_name ,
ogc_proponent ,
dam_name ,
dam_height ,
dam_owner ,
dam_use ,
dam_operating_status ,
utm_zone ,
utm_easting ,
utm_northing ,
dbm_mof_50k_grid ,
linear_feature_id ,
blue_line_key ,
watershed_key ,
downstream_route_measure ,
wscode_ltree ,
localcode_ltree ,
watershed_group_code ,
gnis_stream_name ,
stream_order ,
stream_magnitude ,
observedspp_dnstr ,
observedspp_upstr ,
geom ,
crossings_dnstr ,
barriers_anthropogenic_dnstr ,
barriers_anthropogenic_upstr
)

select
c.aggregated_crossings_id ,
c.stream_crossing_id ,
c.dam_id ,
c.user_barrier_anthropogenic_id ,
c.modelled_crossing_id ,
c.crossing_source ,
c.crossing_feature_type ,
c.pscis_status ,
c.crossing_type_code ,
c.crossing_subtype_code ,
c.modelled_crossing_type_source ,
c.barrier_status ,
c.pscis_road_name ,
c.pscis_stream_name ,
c.pscis_assessment_comment ,
c.pscis_assessment_date ,
c.pscis_final_score ,
c.transport_line_structured_name_1 ,
c.transport_line_type_description ,
c.transport_line_surface_description ,
c.ften_forest_file_id ,
c.ften_file_type_description ,
c.ften_client_number ,
c.ften_client_name ,
c.ften_life_cycle_status_code ,
c.rail_track_name ,
c.rail_owner_name ,
c.rail_operator_english_name ,
c.ogc_proponent ,
c.dam_name ,
c.dam_height ,
c.dam_owner ,
c.dam_use ,
c.dam_operating_status ,
c.utm_zone ,
c.utm_easting ,
c.utm_northing ,
c.dbm_mof_50k_grid ,
c.linear_feature_id ,
c.blue_line_key ,
c.watershed_key ,
c.downstream_route_measure ,
c.wscode_ltree ,
c.localcode_ltree ,
c.watershed_group_code ,
c.gnis_stream_name ,
c.stream_order ,
c.stream_magnitude ,
c.observedspp_dnstr ,
c.observedspp_upstr ,
c.geom ,
cd.features_dnstr ,
ad.features_dnstr ,
au.features_upstr
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;

-- do the switch
drop table bcfishpass.crossings cascade;
alter table bcfishpass.crossings_index_tmp rename to crossings;

-- drop lookups
drop table bcfishpass.crossings_dnstr_crossings;
drop table bcfishpass.crossings_dnstr_barriers_anthropogenic;
drop table bcfishpass.crossings_upstr_barriers_anthropogenic;


-- do same for barriers anthropogenic
alter table bcfishpass.barriers_anthropogenic add column if not exists barriers_anthropogenic_dnstr text[];
drop table if exists bcfishpass.barriers_anthropogenic_index_tmp;
create table bcfishpass.barriers_anthropogenic_index_tmp (like bcfishpass.barriers_anthropogenic including all);

insert into bcfishpass.barriers_anthropogenic_index_tmp (
barriers_anthropogenic_id,
barrier_type,
barrier_name,
linear_feature_id,
blue_line_key,
watershed_key,
downstream_route_measure,
wscode_ltree,
localcode_ltree,
watershed_group_code,
geom,
barriers_anthropogenic_dnstr
)
select
a.barriers_anthropogenic_id,
a.barrier_type,
a.barrier_name,
a.linear_feature_id,
a.blue_line_key,
a.watershed_key,
a.downstream_route_measure,
a.wscode_ltree,
a.localcode_ltree,
a.watershed_group_code,
geom,
d.features_dnstr as barriers_anthropogenic_dnstr
from bcfishpass.barriers_anthropogenic a
left outer join bcfishpass.barriers_anthropogenic_dnstr_barriers_anthropogenic d
on a.barriers_anthropogenic_id = d.barriers_anthropogenic_id;

-- do the switch
drop table bcfishpass.barriers_anthropogenic;
alter table bcfishpass.barriers_anthropogenic_index_tmp rename to barriers_anthropogenic;

drop table bcfishpass.barriers_anthropogenic_dnstr_barriers_anthropogenic;
26 changes: 5 additions & 21 deletions model/02_habitat_linear/sql/views/crossings_vw.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,11 @@ 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(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,
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,
a.gradient,
a.total_network_km,
a.total_stream_km,
Expand Down Expand Up @@ -229,14 +221,6 @@ 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: 5 additions & 18 deletions release/sql/freshwater_fish_habitat_accessibility_model.sql
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,11 @@ 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(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,
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,
r.gradient,
r.total_network_km,
r.total_stream_km,
Expand Down Expand Up @@ -280,15 +276,6 @@ 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 8734ff8

Please sign in to comment.