diff --git a/db/tables/crossings.sql b/db/tables/crossings.sql index 46ed418e..0d7848a7 100644 --- a/db/tables/crossings.sql +++ b/db/tables/crossings.sql @@ -27,7 +27,7 @@ create table bcfishpass.crossings user_barrier_anthropogenic_id bigint unique, modelled_crossing_id integer unique, crossing_source text, -- pscis/dam/model, can be inferred from above ids - crossing_feature_type text, -- general type of crossing (rail/road/trail/dam/weir) + --crossing_feature_type text, -- general type of crossing (rail/road/trail/dam/weir) -- basic crossing status/info pscis_status text, -- ASSESSED/HABITAT CONFIRMATION etc @@ -76,7 +76,7 @@ create table bcfishpass.crossings utm_northing integer, -- map tile for pdfs - dbm_mof_50k_grid text, + --dbm_mof_50k_grid text, -- basic FWA info linear_feature_id integer, @@ -95,8 +95,8 @@ create table bcfishpass.crossings -- watershed_upstr_ha double precision DEFAULT 0, -- distinct species upstream/downstream, derived from bcfishobs - observedspp_dnstr text[], - observedspp_upstr text[], + --observedspp_dnstr text[], + --observedspp_upstr text[], geom geometry(PointZM, 3005), @@ -111,7 +111,7 @@ comment on column bcfishpass.crossings.dam_id IS 'BC Dams unique identifier'; comment on column bcfishpass.crossings.user_barrier_anthropogenic_id IS 'User added misc anthropogenic barriers unique identifier'; comment on column bcfishpass.crossings.modelled_crossing_id IS 'Modelled crossing unique identifier'; comment on column bcfishpass.crossings.crossing_source IS 'Data source for the crossing, one of: {PSCIS,MODELLED CROSSINGS,CABD,MISC BARRIERS}'; -comment on column bcfishpass.crossings.crossing_feature_type IS 'The general type of feature crossing the stream, valid feature types are {DAM,RAIL,"ROAD, DEMOGRAPHIC","ROAD, RESOURCE/OTHER",TRAIL,WEIR}'; +--comment on column bcfishpass.crossings.crossing_feature_type IS 'The general type of feature crossing the stream, valid feature types are {DAM,RAIL,"ROAD, DEMOGRAPHIC","ROAD, RESOURCE/OTHER",TRAIL,WEIR}'; comment on column bcfishpass.crossings.pscis_status IS 'From PSCIS, the current_pscis_status of the crossing, one of: {ASSESSED,HABITAT CONFIRMATION,DESIGN,REMEDIATED}'; comment on column bcfishpass.crossings.crossing_type_code IS 'Defines the type of crossing present at the location of the stream crossing. Acceptable types are: OBS = Open Bottom Structure CBS = Closed Bottom Structure OTHER = Crossing structure does not fit into the above categories. Eg: ford, wier'; comment on column bcfishpass.crossings.crossing_subtype_code IS 'Further definition of the type of crossing, one of {BRIDGE,CRTBOX,DAM,FORD,OVAL,PIPEARCH,ROUND,WEIR,WOODBOX,NULL}'; @@ -143,7 +143,7 @@ comment on column bcfishpass.crossings.dam_operating_status IS 'See CABD dams co comment on column bcfishpass.crossings.utm_zone IS 'UTM ZONE is a segment of the Earths surface 6 degrees of longitude in width. The zones are numbered eastward starting at the meridian 180 degrees from the prime meridian at Greenwich. There are five zones numbered 7 through 11 that cover British Columbia, e.g., Zone 10 with a central meridian at -123 degrees.'; comment on column bcfishpass.crossings.utm_easting IS 'UTM EASTING is the distance in meters eastward to or from the central meridian of a UTM zone with a false easting of 500000 meters. e.g., 440698'; comment on column bcfishpass.crossings.utm_northing IS 'UTM NORTHING is the distance in meters northward from the equator. e.g., 6197826'; -comment on column bcfishpass.crossings.dbm_mof_50k_grid IS 'WHSE_BASEMAPPING.DBM_MOF_50K_GRID map_tile_display_name, used for generating planning map pdfs'; +--comment on column bcfishpass.crossings.dbm_mof_50k_grid IS 'WHSE_BASEMAPPING.DBM_MOF_50K_GRID map_tile_display_name, used for generating planning map pdfs'; comment on column bcfishpass.crossings.linear_feature_id IS 'From BC FWA, the unique identifier for a stream segment (flow network arc)'; comment on column bcfishpass.crossings.blue_line_key IS 'From BC FWA, uniquely identifies a single flow line such that a main channel and a secondary channel with the same watershed code would have different blue line keys (the Fraser River and all side channels have different blue line keys).'; comment on column bcfishpass.crossings.watershed_key IS 'From BC FWA, a key that identifies a stream system. There is a 1:1 match between a watershed key and watershed code. The watershed key will match the blue line key for the mainstem.'; @@ -155,8 +155,8 @@ comment on column bcfishpass.crossings.gnis_stream_name IS 'The BCGNIS (BC Geogr comment on column bcfishpass.crossings.stream_order IS 'Order of FWA stream at point'; comment on column bcfishpass.crossings.stream_magnitude IS 'Magnitude of FWA stream at point'; --comment on column bcfishpass.crossings.watershed_upstr_ha IS 'Total watershed area upstream of point (approximate, does not include area of the fundamental watershed in which the point lies)'; -comment on column bcfishpass.crossings.observedspp_dnstr IS 'Fish species observed downstream of point *within the same watershed group*'; -comment on column bcfishpass.crossings.observedspp_upstr IS 'Fish species observed upstream of point *within the same watershed group*'; +--comment on column bcfishpass.crossings.observedspp_dnstr IS 'Fish species observed downstream of point *within the same watershed group*'; +--comment on column bcfishpass.crossings.observedspp_upstr IS 'Fish species observed upstream of point *within the same watershed group*'; comment on column bcfishpass.crossings.geom IS 'The point geometry associated with the feature'; -- index for speed diff --git a/db/views/01_crossings_feature_type_vw.sql b/db/views/01_crossings_feature_type_vw.sql new file mode 100644 index 00000000..4a0b0ec2 --- /dev/null +++ b/db/views/01_crossings_feature_type_vw.sql @@ -0,0 +1,81 @@ +-- crossing feature type +drop view if exists bcfishpass.crossings_feature_type_vw; +create view bcfishpass.crossings_feature_type_vw as +select + aggregated_crossings_id, + case + when crossing_subtype_code = 'WEIR' then 'WEIR' + when crossing_subtype_code = 'DAM' then 'DAM' + -- RAIL, pscis crossings within 50m of rail line and with RAIL in the road name + when + rail_owner_name is not null + and aggregated_crossings_id in ( + select + aggregated_crossings_id + from bcfishpass.crossings c + inner join whse_basemapping.gba_railway_tracks_sp r + on st_dwithin(c.geom, r.geom, 50) + where crossing_source = 'PSCIS' + and upper(pscis_road_name) like '%RAIL%' and upper(pscis_road_name) not like '%TRAIL%' + ) + then 'RAIL' + -- RAIL, pscis crossings within 10m of rail line + when + rail_owner_name is not null + and aggregated_crossings_id in ( + select + aggregated_crossings_id + from bcfishpass.crossings c + inner join whse_basemapping.gba_railway_tracks_sp r + on st_dwithin(c.geom, r.geom, 10) + where crossing_source = 'PSCIS' + ) + then 'RAIL' + -- modelled rail crossings + when + rail_owner_name is not null + and crossing_source = 'MODELLED CROSSINGS' + then 'RAIL' + -- tenured roads + when + ften_forest_file_id is not NULL OR + ogc_proponent is not NULL + then 'ROAD, RESOURCE/OTHER' + -- demographic roads + when + transport_line_type_description IN ( + 'Road alleyway', + 'Road arterial major', + 'Road arterial minor', + 'Road collector major', + 'Road collector minor', + 'Road freeway', + 'Road highway major', + 'Road highway minor', + 'Road lane', + 'Road local', + 'Private driveway demographic', + 'Road pedestrian mall', + 'Road runway non-demographic', + 'Road recreation demographic', + 'Road ramp', + 'Road restricted', + 'Road strata', + 'Road service', + 'Road yield lane' + ) + then 'ROAD, DEMOGRAPHIC' + -- trails + when + upper(transport_line_type_description) LIKE 'TRAIL%' + then 'TRAIL' + -- everything else (DRA) + when + transport_line_type_description is not NULL + then 'ROAD, RESOURCE/OTHER' + -- in the absence of any of the above info, assume a PSCIS crossing is on a resource/other road + when + stream_crossing_id is not NULL + then 'ROAD, RESOURCE/OTHER' + end as crossing_feature_type +from bcfishpass.crossings; \ No newline at end of file diff --git a/db/views/02_crossings_admin.sql b/db/views/02_crossings_admin.sql new file mode 100644 index 00000000..8784b9bc --- /dev/null +++ b/db/views/02_crossings_admin.sql @@ -0,0 +1,31 @@ +-- below datasets are not guaranteed to exist + +--drop materialized view if exists bcfishpass.crossings_admin; +--create materialized view bcfishpass.crossings_admin; +--SELECT DISTINCT ON (c.aggregated_crossings_id) -- some of the admin areas are not clean/distinct, make sure to select just one +-- c.aggregated_crossings_id, +-- rd.admin_area_abbreviation as abms_regional_district, +-- muni.admin_area_abbreviation as abms_municipality, +-- ir.english_name as clab_indian_reserve_name, +-- ir.band_name as clab_indian_reserve_band_name, +-- np.english_name as clab_national_park_name, +-- pp.protected_lands_name as bc_protected_lands_name, +-- pmbc.owner_type as pmbc_owner_type, +-- nr.region_org_unit_name as adm_nr_region, +-- nr.district_name as adm_nr_district +--FROM bcfishpass.crossings c +--LEFT OUTER JOIN whse_legal_admin_boundaries.abms_regional_districts_sp rd +--ON ST_Intersects(c.geom, rd.geom) +--LEFT OUTER JOIN whse_legal_admin_boundaries.abms_municipalities_sp muni +--ON ST_Intersects(c.geom, muni.geom) +--LEFT OUTER JOIN whse_admin_boundaries.adm_indian_reserves_bands_sp ir +--ON ST_Intersects(c.geom, ir.geom) +--LEFT OUTER JOIN whse_admin_boundaries.clab_national_parks np +--ON ST_Intersects(c.geom, np.geom) +--LEFT OUTER JOIN whse_tantalis.ta_park_ecores_pa_svw pp +--ON ST_Intersects(c.geom, pp.geom) +--LEFT OUTER JOIN whse_cadastre.pmbc_parcel_fabric_poly_svw pmbc +--ON ST_Intersects(c.geom, pmbc.geom) +--LEFT OUTER JOIN whse_admin_boundaries.adm_nr_districts_sp nr +--ON ST_Intersects(c.geom, pmbc.geom) +--ORDER BY c.aggregated_crossings_id, rd.admin_area_abbreviation, muni.admin_area_abbreviation, ir.english_name, pp.protected_lands_name, pmbc.owner_type, nr.district_name; \ No newline at end of file diff --git a/db/views/02_crossings_upstr_dnstr_observations.sql b/db/views/02_crossings_upstr_dnstr_observations.sql new file mode 100644 index 00000000..a0d3d309 --- /dev/null +++ b/db/views/02_crossings_upstr_dnstr_observations.sql @@ -0,0 +1,62 @@ +-- downstream observations ***within the same watershed group*** +drop materialized view if exists bcfishpass.crossings_dnstr_observations; + +create materialized view bcfishpass.crossings_dnstr_observations as +select + aggregated_crossings_id, + array_agg(species_code) as observedspp_dnstr +FROM + ( + select distinct + a.aggregated_crossings_id, + unnest(species_codes) as species_code + from bcfishpass.crossings a + left outer join bcfishobs.fiss_fish_obsrvtn_events fo + on FWA_Downstream( + a.blue_line_key, + a.downstream_route_measure, + a.wscode_ltree, + a.localcode_ltree, + fo.blue_line_key, + fo.downstream_route_measure, + fo.wscode_ltree, + fo.localcode_ltree + ) + and a.watershed_group_code = fo.watershed_group_code + order by a.aggregated_crossings_id, species_code + ) AS f +group by aggregated_crossings_id; + +create index on bcfishpass.crossings_dnstr_observations (aggregated_crossings_id); + + +-- upstream observations ***within the same watershed group*** +drop materialized view if exists bcfishpass.crossings_upstr_observations; + +create materialized view bcfishpass.crossings_upstr_observations as +select + aggregated_crossings_id, + array_agg(species_code) as observedspp_upstr +FROM + ( + select distinct + a.aggregated_crossings_id, + unnest(species_codes) as species_code + from bcfishpass.crossings a + left outer join bcfishobs.fiss_fish_obsrvtn_events fo + on FWA_Upstream( + a.blue_line_key, + a.downstream_route_measure, + a.wscode_ltree, + a.localcode_ltree, + fo.blue_line_key, + fo.downstream_route_measure, + fo.wscode_ltree, + fo.localcode_ltree + ) + and a.watershed_group_code = fo.watershed_group_code + order by a.aggregated_crossings_id, species_code + ) AS f +group by aggregated_crossings_id; + +create index on bcfishpass.crossings_upstr_observations (aggregated_crossings_id); \ No newline at end of file diff --git a/db/views/03_crossings_vw.sql b/db/views/03_crossings_vw.sql index e8fe9a02..a7ef348c 100644 --- a/db/views/03_crossings_vw.sql +++ b/db/views/03_crossings_vw.sql @@ -14,7 +14,7 @@ select c.user_barrier_anthropogenic_id, c.modelled_crossing_id, c.crossing_source, - c.crossing_feature_type, + cft.crossing_feature_type, c.pscis_status, c.crossing_type_code, c.crossing_subtype_code, @@ -45,7 +45,7 @@ select c.utm_zone, c.utm_easting, c.utm_northing, - c.dbm_mof_50k_grid, + t.map_tile_display_name as dbm_mof_50k_grid, c.linear_feature_id, c.blue_line_key, c.watershed_key, @@ -62,8 +62,8 @@ select s.map_upstream, s.channel_width, s.mad_m3s, - array_to_string(c.observedspp_dnstr, ';') as observedspp_dnstr, - array_to_string(c.observedspp_upstr, ';') as observedspp_upstr, + array_to_string(cdo.observedspp_dnstr, ';') as observedspp_dnstr, + array_to_string(cuo.observedspp_upstr, ';') as observedspp_upstr, array_to_string(cd.features_dnstr, ';') as crossings_dnstr, array_to_string(ad.features_dnstr, ';') as barriers_anthropogenic_dnstr, coalesce(array_length(ad.features_dnstr, 1), 0) as barriers_anthropogenic_dnstr_count, @@ -233,6 +233,9 @@ select h.wct_rearing_belowupstrbarriers_km, c.geom from bcfishpass.crossings c +inner join bcfishpass.crossings_feature_type_vw cft on c.aggregated_crossings_id = cft.aggregated_crossings_id +left outer join bcfishpass.crossings_dnstr_observations cdo on c.aggregated_crossings_id = cdo.aggregated_crossings_id +left outer join bcfishpass.crossings_upstr_observations cuo on c.aggregated_crossings_id = cuo.aggregated_crossings_id 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 @@ -240,6 +243,7 @@ left outer join bcfishpass.crossings_upstr_barriers_per_model_vw aum on c.aggreg 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 +left outer join whse_basemapping.dbm_mof_50k_grid t ON ST_Intersects(c.geom, t.geom) order by c.aggregated_crossings_id, s.downstream_route_measure; create unique index on bcfishpass.crossings_vw (aggregated_crossings_id); diff --git a/db/views/wsg_crossing_summary.sql b/db/views/wsg_crossing_summary.sql index b44d3929..3d8bf505 100644 --- a/db/views/wsg_crossing_summary.sql +++ b/db/views/wsg_crossing_summary.sql @@ -1,7 +1,7 @@ drop view if exists bcfishpass.wsg_crossing_summary_current cascade; create view bcfishpass.wsg_crossing_summary_current as -select distinct on (watershed_group_code) +select s.model_run_id , s.watershed_group_code , s.crossing_feature_type , @@ -52,7 +52,8 @@ select distinct on (watershed_group_code) from bcfishpass.wsg_crossing_summary s inner join bcfishpass.log l on s.model_run_id = l.model_run_id -order by s.watershed_group_code, l.date_completed desc; +where l.date_completed = (select date_completed from bcfishpass.log order by date_completed desc limit 1) +order by s.watershed_group_code, s.crossing_feature_type; drop view if exists bcfishpass.wsg_crossing_summary_previous; @@ -110,4 +111,59 @@ from bcfishpass.wsg_crossing_summary s inner join bcfishpass.log l on s.model_run_id = l.model_run_id where l.date_completed = (select date_completed from bcfishpass.log order by date_completed desc offset 1 limit 1) -order by watershed_group_code; \ No newline at end of file +order by watershed_group_code; + + +create view bcfishpass.wsg_crossing_summary_diff as +select + a.watershed_group_code , + a.crossing_feature_type , + a.n_crossings_total - b.n_crossings_total as n_crossings_total, + a.n_passable_total - b.n_passable_total as n_passable_total, + a.n_barriers_total - b.n_barriers_total as n_barriers_total, + a.n_potential_total - b.n_potential_total as n_potential_total, + a.n_unknown_total - b.n_unknown_total as n_unknown_total, + a.n_barriers_accessible_bt - b.n_barriers_accessible_bt as n_barriers_accessible_bt, + a.n_potential_accessible_bt - b.n_potential_accessible_bt as n_potential_accessible_bt, + a.n_unknown_accessible_bt - b.n_unknown_accessible_bt as n_unknown_accessible_bt, + a.n_barriers_accessible_ch_cm_co_pk_sk - b.n_barriers_accessible_ch_cm_co_pk_sk as n_barriers_accessible_ch_cm_co_pk_sk, + a.n_potential_accessible_ch_cm_co_pk_sk - b.n_potential_accessible_ch_cm_co_pk_sk as n_potential_accessible_ch_cm_co_pk_sk, + a.n_unknown_accessible_ch_cm_co_pk_sk - b.n_unknown_accessible_ch_cm_co_pk_sk as n_unknown_accessible_ch_cm_co_pk_sk, + a.n_barriers_accessible_st - b.n_barriers_accessible_st as n_barriers_accessible_st, + a.n_potential_accessible_st - b.n_potential_accessible_st as n_potential_accessible_st, + a.n_unknown_accessible_st - b.n_unknown_accessible_st as n_unknown_accessible_st, + a.n_barriers_accessible_wct - b.n_barriers_accessible_wct as n_barriers_accessible_wct, + a.n_potential_accessible_wct - b.n_potential_accessible_wct as n_potential_accessible_wct, + a.n_unknown_accessible_wct - b.n_unknown_accessible_wct as n_unknown_accessible_wct, + a.n_barriers_habitat_bt - b.n_barriers_habitat_bt as n_barriers_habitat_bt, + a.n_potential_habitat_bt - b.n_potential_habitat_bt as n_potential_habitat_bt, + a.n_unknown_habitat_bt - b.n_unknown_habitat_bt as n_unknown_habitat_bt, + a.n_barriers_habitat_ch - b.n_barriers_habitat_ch as n_barriers_habitat_ch, + a.n_potential_habitat_ch - b.n_potential_habitat_ch as n_potential_habitat_ch, + a.n_unknown_habitat_ch - b.n_unknown_habitat_ch as n_unknown_habitat_ch, + a.n_barriers_habitat_cm - b.n_barriers_habitat_cm as n_barriers_habitat_cm, + a.n_potential_habitat_cm - b.n_potential_habitat_cm as n_potential_habitat_cm, + a.n_unknown_habitat_cm - b.n_unknown_habitat_cm as n_unknown_habitat_cm, + a.n_barriers_habitat_co - b.n_barriers_habitat_co as n_barriers_habitat_co, + a.n_potential_habitat_co - b.n_potential_habitat_co as n_potential_habitat_co, + a.n_unknown_habitat_co - b.n_unknown_habitat_co as n_unknown_habitat_co, + a.n_barriers_habitat_pk - b.n_barriers_habitat_pk as n_barriers_habitat_pk, + a.n_potential_habitat_pk - b.n_potential_habitat_pk as n_potential_habitat_pk, + a.n_unknown_habitat_pk - b.n_unknown_habitat_pk as n_unknown_habitat_pk, + a.n_barriers_habitat_sk - b.n_barriers_habitat_sk as n_barriers_habitat_sk, + a.n_potential_habitat_sk - b.n_potential_habitat_sk as n_potential_habitat_sk, + a.n_unknown_habitat_sk - b.n_unknown_habitat_sk as n_unknown_habitat_sk, + a.n_barriers_habitat_salmon - b.n_barriers_habitat_salmon as n_barriers_habitat_salmon, + a.n_potential_habitat_salmon - b.n_potential_habitat_salmon as n_potential_habitat_salmon, + a.n_unknown_habitat_salmon - b.n_unknown_habitat_salmon as n_unknown_habitat_salmon, + a.n_barriers_habitat_st - b.n_barriers_habitat_st as n_barriers_habitat_st, + a.n_potential_habitat_st - b.n_potential_habitat_st as n_potential_habitat_st, + a.n_unknown_habitat_st - b.n_unknown_habitat_st as n_unknown_habitat_st, + a.n_barriers_habitat_wct - b.n_barriers_habitat_wct as n_barriers_habitat_wct, + a.n_potential_habitat_wct - b.n_potential_habitat_wct as n_potential_habitat_wct, + a.n_unknown_habitat_wct - b.n_unknown_habitat_wct as n_unknown_habitat_wct +from bcfishpass.wsg_crossing_summary_current a +inner join bcfishpass.wsg_crossing_summary_previous b + on a.watershed_group_code = b.watershed_group_code + and a.crossing_feature_type = b.crossing_feature_type +order by a.watershed_group_code; \ No newline at end of file diff --git a/model/01_access/Makefile b/model/01_access/Makefile index 308d14c3..36b43eb0 100644 --- a/model/01_access/Makefile +++ b/model/01_access/Makefile @@ -94,7 +94,9 @@ clean: .make/pscis \ .make/dbm_mof_50k_grid \ sql/load_crossings.sql - $(PSQL) -f sql/load_crossings.sql + $(PSQL) -c "truncate bcfishpass.crossings" + parallel $(PSQL) -f sql/load_crossings.sql -v wsg={1} ::: $(WSGS) + # compute upstr/downstr observations here (as previous)? touch $@ # ----- diff --git a/model/01_access/sql/barriers_anthropogenic.sql b/model/01_access/sql/barriers_anthropogenic.sql index c1976732..40bea8e2 100644 --- a/model/01_access/sql/barriers_anthropogenic.sql +++ b/model/01_access/sql/barriers_anthropogenic.sql @@ -17,18 +17,19 @@ INSERT INTO bcfishpass.barriers_anthropogenic geom ) SELECT - aggregated_crossings_id, - crossing_feature_type as barrier_type, + c.aggregated_crossings_id, + cft.crossing_feature_type as barrier_type, NULL as barrier_name, - linear_feature_id, - blue_line_key, - watershed_key, - downstream_route_measure, - wscode_ltree, - localcode_ltree, - watershed_group_code as watershed_group_code, - st_force2d(geom) as geom -FROM bcfishpass.crossings + 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, + st_force2d(c.geom) as geom +FROM bcfishpass.crossings c +INNER JOIN bcfishpass.crossings_feature_type_vw cft on c.aggregated_crossings_id = cft.aggregated_crossings_id WHERE barrier_status IN ('BARRIER', 'POTENTIAL') AND blue_line_key = watershed_key AND -- do not include side channel features as barriers diff --git a/model/01_access/sql/barriers_dams.sql b/model/01_access/sql/barriers_dams.sql index a5293ff8..e91d7e55 100644 --- a/model/01_access/sql/barriers_dams.sql +++ b/model/01_access/sql/barriers_dams.sql @@ -14,18 +14,19 @@ INSERT INTO bcfishpass.barriers_dams ) SELECT - aggregated_crossings_id, - crossing_feature_type, - dam_name as barrier_name, - linear_feature_id, - blue_line_key, - watershed_key, - downstream_route_measure, - wscode_ltree, - localcode_ltree, - watershed_group_code as watershed_group_code, - st_force2d(geom) as geom -FROM bcfishpass.crossings + c.aggregated_crossings_id, + cft.crossing_feature_type, + c.dam_name as barrier_name, + 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, + st_force2d(c.geom) as geom +FROM bcfishpass.crossings c +INNER JOIN bcfishpass.crossings_feature_type_vw cft on c.aggregated_crossings_id = cft.aggregated_crossings_id WHERE dam_id IS NOT NULL AND barrier_status IN ('BARRIER', 'POTENTIAL') AND diff --git a/model/01_access/sql/barriers_dams_hydro.sql b/model/01_access/sql/barriers_dams_hydro.sql index 05f8c152..1344b433 100644 --- a/model/01_access/sql/barriers_dams_hydro.sql +++ b/model/01_access/sql/barriers_dams_hydro.sql @@ -14,18 +14,19 @@ INSERT INTO bcfishpass.barriers_dams_hydro ) SELECT - aggregated_crossings_id, - crossing_feature_type, - dam_name as barrier_name, - linear_feature_id, - blue_line_key, - watershed_key, - downstream_route_measure, - wscode_ltree, - localcode_ltree, - watershed_group_code as watershed_group_code, - st_force2d(geom) as geom -FROM bcfishpass.crossings + c.aggregated_crossings_id, + cft.crossing_feature_type, + c.dam_name as barrier_name, + 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, + st_force2d(c.geom) as geom +FROM bcfishpass.crossings c +INNER JOIN bcfishpass.crossings_feature_type_vw cft on c.aggregated_crossings_id = cft.aggregated_crossings_id WHERE dam_id IS NOT NULL AND dam_use = 'Hydroelectricity' AND diff --git a/model/01_access/sql/barriers_pscis.sql b/model/01_access/sql/barriers_pscis.sql index 4e18ea2a..c494d545 100644 --- a/model/01_access/sql/barriers_pscis.sql +++ b/model/01_access/sql/barriers_pscis.sql @@ -14,18 +14,19 @@ INSERT INTO bcfishpass.barriers_pscis ) SELECT - aggregated_crossings_id, - crossing_feature_type, - crossing_source as barrier_name, - linear_feature_id, - blue_line_key, - watershed_key, - downstream_route_measure, - wscode_ltree, - localcode_ltree, - watershed_group_code as watershed_group_code, - st_force2d(geom) as geom -FROM bcfishpass.crossings + c.aggregated_crossings_id, + cft.crossing_feature_type, + c.crossing_source as barrier_name, + 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, + st_force2d(c.geom) as geom +FROM bcfishpass.crossings c +INNER JOIN bcfishpass.crossings_feature_type_vw cft on c.aggregated_crossings_id = cft.aggregated_crossings_id WHERE stream_crossing_id IS NOT NULL AND barrier_status IN ('BARRIER', 'POTENTIAL') AND diff --git a/model/01_access/sql/crossings_admin.sql b/model/01_access/sql/crossings_admin.sql deleted file mode 100644 index 8bc5c601..00000000 --- a/model/01_access/sql/crossings_admin.sql +++ /dev/null @@ -1,55 +0,0 @@ --- this takes a while to run, only do it when cutting a release - - -ALTER TABLE bcfishpass.crossings ADD COLUMN abms_regional_district text; -ALTER TABLE bcfishpass.crossings ADD COLUMN abms_municipality text; -ALTER TABLE bcfishpass.crossings ADD COLUMN clab_indian_reserve_name text; -ALTER TABLE bcfishpass.crossings ADD COLUMN clab_indian_reserve_band_name text; -ALTER TABLE bcfishpass.crossings ADD COLUMN clab_national_park text; -ALTER TABLE bcfishpass.crossings ADD COLUMN tantalis_park_ecores_pa text; -ALTER TABLE bcfishpass.crossings ADD COLUMN pmbc_private text; -ALTER TABLE bcfishpass.crossings ADD COLUMN adm_nr_region text; -ALTER TABLE bcfishpass.crossings ADD COLUMN adm_nr_district text; - -WITH overlay AS -(SELECT DISTINCT ON (c.aggregated_crossings_id) -- some of the admin areas are not clean/distinct, make sure to select just one - c.aggregated_crossings_id, - rd.admin_area_abbreviation as abms_regional_district, - muni.admin_area_abbreviation as abms_municipality, - ir.english_name as clab_indian_reserve_name, - ir.band_name as clab_indian_reserve_band_name, - np.english_name as clab_national_park_name, - pp.protected_lands_name as bc_protected_lands_name, - pmbc.owner_type as pmbc_owner_type, - nr.region_org_unit_name as adm_nr_region, - nr.district_name as adm_nr_district -FROM bcfishpass.crossings c -LEFT OUTER JOIN whse_legal_admin_boundaries.abms_regional_districts_sp rd -ON ST_Intersects(c.geom, rd.geom) -LEFT OUTER JOIN whse_legal_admin_boundaries.abms_municipalities_sp muni -ON ST_Intersects(c.geom, muni.geom) -LEFT OUTER JOIN whse_admin_boundaries.adm_indian_reserves_bands_sp ir -ON ST_Intersects(c.geom, ir.geom) -LEFT OUTER JOIN whse_admin_boundaries.clab_national_parks np -ON ST_Intersects(c.geom, np.geom) -LEFT OUTER JOIN whse_tantalis.ta_park_ecores_pa_svw pp -ON ST_Intersects(c.geom, pp.geom) -LEFT OUTER JOIN whse_cadastre.pmbc_parcel_fabric_poly_svw pmbc -ON ST_Intersects(c.geom, pmbc.geom) -LEFT OUTER JOIN whse_admin_boundaries.adm_nr_districts_sp nr -ON ST_Intersects(c.geom, pmbc.geom) -ORDER BY c.aggregated_crossings_id, rd.admin_area_abbreviation, muni.admin_area_abbreviation, ir.english_name, pp.protected_lands_name, pmbc.owner_type, nr.district_name) - -UPDATE bcfishpass.crossings c -SET - abms_regional_district = o.abms_regional_district, - abms_municipality = o.abms_municipality, - clab_indian_reserve_name = o.clab_indian_reserve_name, - clab_indian_reserve_band_name = o.clab_indian_reserve_band_name, - clab_national_park_name = o.clab_national_park_name, - bc_protected_lands_name = o.bc_protected_lands_name, - pmbc_owner_type = o.pmbc_owner_type, - adm_nr_region = o.adm_nr_region, - adm_nr_distric = o.adm_nr_district -FROM overlay o -WHERE c.aggregated_crossings_id = o.aggregated_crossings_id; \ No newline at end of file diff --git a/model/01_access/sql/load_crossings.sql b/model/01_access/sql/load_crossings.sql index b99430e9..566ed089 100644 --- a/model/01_access/sql/load_crossings.sql +++ b/model/01_access/sql/load_crossings.sql @@ -3,8 +3,6 @@ -- insert PSCIS crossings first, they take precedence -- PSCIS on modelled crossings first, to get the road tenure info from model -- -------------------------------- -truncate bcfishpass.crossings; - insert into bcfishpass.crossings ( aggregated_crossings_id, @@ -128,6 +126,7 @@ on dra.transport_line_surface_code = drasurface.transport_line_surface_code inner join whse_basemapping.fwa_stream_networks_sp s ON e.linear_feature_id = s.linear_feature_id where e.modelled_crossing_id is not NULL -- only PSCIS crossings that have been linked to a modelled crossing +and e.watershed_group_code = :'wsg' order by e.stream_crossing_id on conflict do nothing; @@ -338,6 +337,7 @@ on e.stream_crossing_id = f.stream_crossing_id inner join whse_basemapping.fwa_stream_networks_sp s ON e.linear_feature_id = s.linear_feature_id where e.modelled_crossing_id IS NULL +and e.watershed_group_code = :'wsg' order by e.stream_crossing_id, distance_to_road asc on conflict do nothing; @@ -411,6 +411,7 @@ from bcfishpass.dams d inner join whse_basemapping.fwa_stream_networks_sp s on d.linear_feature_id = s.linear_feature_id inner join cabd.dams cabd on d.dam_id = cabd.cabd_id::text +where d.watershed_group_code = :'wsg' order by dam_id on conflict do nothing; @@ -465,6 +466,8 @@ select (st_dump(st_locatealong(s.geom, d.downstream_route_measure))).geom as geom from bcfishpass.dams d inner join whse_basemapping.fwa_stream_networks_sp s on d.linear_feature_id = s.linear_feature_id +-- note lack of watershed group code query (as these are outside of bc) +-- just run this insert every time, the conflicts are ignored order by dam_id on conflict do nothing; @@ -544,8 +547,8 @@ select b.stream_magnitude, (st_dump(st_locatealong(s.geom, b.downstream_route_measure))).geom as geom from misc_barriers b -inner join whse_basemapping.fwa_stream_networks_sp s -on b.linear_feature_id = s.linear_feature_id +inner join whse_basemapping.fwa_stream_networks_sp s on b.linear_feature_id = s.linear_feature_id +where b.watershed_group_code = :'wsg' order by user_barrier_anthropogenic_id on conflict do nothing; @@ -660,207 +663,6 @@ on dra.transport_line_surface_code = drasurface.transport_line_surface_code -- where b.blue_line_key = s.watershed_key where (f.structure IS NULL OR coalesce(f.structure, 'CBS') = 'OBS') -- don't include crossings that have been determined to be non-existent (f.structure = 'NONE') and p.stream_crossing_id IS NULL -- don't include PSCIS crossings +and b.watershed_group_code = :'wsg' order by modelled_crossing_id -on conflict do nothing; - - --- -------------------------------- --- populate crossing_feature_type column --- -------------------------------- -update bcfishpass.crossings -set crossing_feature_type = 'WEIR' -where crossing_subtype_code = 'WEIR'; - -update bcfishpass.crossings -set crossing_feature_type = 'DAM' -where crossing_subtype_code = 'DAM'; - --- railway --- pscis crossings within 50m of rail line and with RAIL in the road name -update bcfishpass.crossings -set crossing_feature_type = 'RAIL' -where rail_owner_name is not null -and aggregated_crossings_id in -( - select aggregated_crossings_id - from bcfishpass.crossings c - inner join whse_basemapping.gba_railway_tracks_sp r - on st_dwithin(c.geom, r.geom, 50) - where crossing_source = 'PSCIS' - and upper(pscis_road_name) like '%RAIL%' and upper(pscis_road_name) not like '%TRAIL%' -); - --- pscis crossings within 10m of rail line -update bcfishpass.crossings -set crossing_feature_type = 'RAIL' -where rail_owner_name is not null -and aggregated_crossings_id in -( - select aggregated_crossings_id - from bcfishpass.crossings c - inner join whse_basemapping.gba_railway_tracks_sp r - on st_dwithin(c.geom, r.geom, 10) - where crossing_source = 'PSCIS' -); - --- modelled rail crossings -update bcfishpass.crossings -set crossing_feature_type = 'RAIL' -where rail_owner_name is not null -and crossing_source = 'MODELLED CROSSINGS'; - --- tenured roads -update bcfishpass.crossings -set crossing_feature_type = 'ROAD, RESOURCE/OTHER' -where - ften_forest_file_id is not NULL OR - ogc_proponent is not NULL; - --- demographic roads -update bcfishpass.crossings -set crossing_feature_type = 'ROAD, DEMOGRAPHIC' -where - crossing_feature_type IS NULL AND - transport_line_type_description IN ( - 'Road alleyway', - 'Road arterial major', - 'Road arterial minor', - 'Road collector major', - 'Road collector minor', - 'Road freeway', - 'Road highway major', - 'Road highway minor', - 'Road lane', - 'Road local', - 'Private driveway demographic', - 'Road pedestrian mall', - 'Road runway non-demographic', - 'Road recreation demographic', - 'Road ramp', - 'Road restricted', - 'Road strata', - 'Road service', - 'Road yield lane' -); - -update bcfishpass.crossings -set crossing_feature_type = 'TRAIL' -where - crossing_feature_type IS NULL AND - upper(transport_line_type_description) LIKE 'TRAIL%'; - --- everything else from DRA -update bcfishpass.crossings -set crossing_feature_type = 'ROAD, RESOURCE/OTHER' -where - crossing_feature_type IS NULL AND - transport_line_type_description is not NULL; - --- in the absence of any of the above info, assume a PSCIS crossing is on a resource/other road -update bcfishpass.crossings -set crossing_feature_type = 'ROAD, RESOURCE/OTHER' -where - stream_crossing_id is not NULL AND - crossing_feature_type IS NULL; - - --- populate map tile column -WITH tile AS -( - select - a.aggregated_crossings_id, - b.map_tile_display_name - from bcfishpass.crossings a - inner join whse_basemapping.dbm_mof_50k_grid b - ON ST_Intersects(a.geom, b.geom) -) - -update bcfishpass.crossings p -set dbm_mof_50k_grid = t.map_tile_display_name -from tile t -where p.aggregated_crossings_id = t.aggregated_crossings_id; - - --- downstream observations ***within the same watershed group*** -WITH spp_downstream AS -( - select - aggregated_crossings_id, - array_agg(species_code) as species_codes - FROM - ( - select distinct - a.aggregated_crossings_id, - unnest(species_codes) as species_code - from bcfishpass.crossings a - left outer join bcfishobs.fiss_fish_obsrvtn_events fo - on FWA_Downstream( - a.blue_line_key, - a.downstream_route_measure, - a.wscode_ltree, - a.localcode_ltree, - fo.blue_line_key, - fo.downstream_route_measure, - fo.wscode_ltree, - fo.localcode_ltree - ) - and a.watershed_group_code = fo.watershed_group_code - order by a.aggregated_crossings_id, species_code - ) AS f - group by aggregated_crossings_id -) - -update bcfishpass.crossings c -set observedspp_dnstr = d.species_codes -from spp_downstream d -where c.aggregated_crossings_id = d.aggregated_crossings_id; - --- upstream observations ***within the same watershed group*** -WITH spp_upstream AS -( - select - aggregated_crossings_id, - array_agg(species_code) as species_codes - FROM - ( - select distinct - a.aggregated_crossings_id, - unnest(species_codes) as species_code - from bcfishpass.crossings a - left outer join bcfishobs.fiss_fish_obsrvtn_events fo - on FWA_Upstream( - a.blue_line_key, - a.downstream_route_measure, - a.wscode_ltree, - a.localcode_ltree, - fo.blue_line_key, - fo.downstream_route_measure, - fo.wscode_ltree, - fo.localcode_ltree - ) - and a.watershed_group_code = fo.watershed_group_code - order by a.aggregated_crossings_id, species_code - ) AS f - group by aggregated_crossings_id -) -update bcfishpass.crossings c -set observedspp_upstr = u.species_codes -from spp_upstream u -where c.aggregated_crossings_id = u.aggregated_crossings_id; - --- upstream area --- with upstr_ha as --- ( --- select --- c.aggregated_crossings_id, --- ua.upstream_area_ha --- from bcfishpass.crossings c --- inner join whse_basemapping.fwa_streams_watersheds_lut l --- on c.linear_feature_id = l.linear_feature_id --- inner join whse_basemapping.fwa_watersheds_upstream_area ua --- on l.watershed_feature_id = ua.watershed_feature_id --- ) --- update bcfishpass.crossings c --- set watershed_upstr_ha = upstr_ha.upstream_area_ha --- from upstr_ha --- where c.aggregated_crossings_id = upstr_ha.aggregated_crossings_id; \ No newline at end of file +on conflict do nothing; \ No newline at end of file diff --git a/model/02_habitat_linear/habitat_linear.sh b/model/02_habitat_linear/habitat_linear.sh index 185f34de..c6c08d58 100755 --- a/model/02_habitat_linear/habitat_linear.sh +++ b/model/02_habitat_linear/habitat_linear.sh @@ -30,6 +30,8 @@ done # with linear model processing complete, refresh materialized views $PSQL -c "refresh materialized view bcfishpass.crossings_upstr_barriers_per_model_vw" +$PSQL -c "refresh materialized view bcfishpass.crossings_upstr_observations" +$PSQL -c "refresh materialized view bcfishpass.crossings_dnstr_observations" $PSQL -c "refresh materialized view bcfishpass.crossings_vw" # Finished processing!