-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Stream profile | ||
|
||
Extracting a stream profile and/or gradient from FWA geometries is a straightforward, but often repeated task. | ||
Run the required query once and materialize the result in table `whse_basemapping.fwa_stream_profiles` for re-use. | ||
|
||
## Process | ||
|
||
./process.sh | ||
|
||
## Output | ||
|
||
Table "whse_basemapping.fwa_stream_profiles" | ||
Column | Type | Collation | Nullable | Default | ||
--------------------------+------------------+-----------+----------+--------- | ||
blue_line_key | integer | | | | ||
downstream_route_measure | double precision | | | | ||
upstream_route_measure | double precision | | | | ||
downstream_elevation | double precision | | | | ||
upstream_elevation | double precision | | | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
set -euxo pipefail | ||
|
||
PSQL="psql $DATABASE_URL -v ON_ERROR_STOP=1" | ||
WSGS=$($PSQL -AXt -c "SELECT watershed_group_code FROM whse_basemapping.fwa_watershed_groups_poly") | ||
|
||
$PSQL -c "drop table if exists whse_basemapping.fwa_stream_profiles" | ||
$PSQL -c "create table whse_basemapping.fwa_stream_profiles ( | ||
blue_line_key integer, | ||
downstream_route_measure double precision, | ||
upstream_route_measure double precision, | ||
downstream_elevation double precision, | ||
upstream_elevation double precision | ||
); | ||
" | ||
|
||
parallel $PSQL -f sql/fwa_stream_profiles.sql -v wsg={1} ::: $WSGS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
-- extract coordinates from streams | ||
insert into whse_basemapping.fwa_stream_profiles ( | ||
blue_line_key, | ||
downstream_route_measure, | ||
upstream_route_measure, | ||
downstream_elevation, | ||
upstream_elevation | ||
) | ||
|
||
with coordinates as ( | ||
select | ||
blue_line_key, | ||
downstream_route_measure, | ||
linear_feature_id, | ||
edge_type, | ||
(st_dumppoints(geom)).path[1] as node_id, | ||
(st_dumppoints(geom)).geom as geom | ||
from whse_basemapping.fwa_stream_networks_sp | ||
where watershed_group_code = :'wsg' | ||
), | ||
|
||
-- extract elevation of each point and determine distances between each point | ||
lengths as ( | ||
select | ||
blue_line_key, | ||
downstream_route_measure, | ||
linear_feature_id, | ||
edge_type, | ||
node_id, | ||
st_z(geom) as elevation_from, | ||
st_z(lead(geom) OVER(ORDER BY blue_line_key, downstream_route_measure, node_id)) as elevation_to, | ||
st_distance(geom, lead(geom) OVER(ORDER BY blue_line_key, downstream_route_measure, node_id)) as length | ||
from coordinates | ||
order by blue_line_key, downstream_route_measure, node_id | ||
), | ||
|
||
-- derive measure of at upstream point | ||
-- drop duplicates at geometry endpoints (where length is 0) | ||
-- drop the final vertex (where length is null) | ||
tidy as ( | ||
select | ||
row_number() over() as id, | ||
blue_line_key, | ||
linear_feature_id, | ||
edge_type, | ||
round((downstream_route_measure + sum(length) over (order by blue_line_key, downstream_route_measure, node_id))::numeric, 2) as upstream_route_measure, | ||
round(elevation_from::numeric, 2) as elevation_from, | ||
round(elevation_to::numeric, 2) as elevation_to | ||
from lengths | ||
where length !=0 and length is not null | ||
order by blue_line_key, downstream_route_measure, node_id | ||
) | ||
|
||
select | ||
blue_line_key, | ||
lag(upstream_route_measure, 1, 0) over (order by id) as downstream_route_measure, | ||
upstream_route_measure, | ||
elevation_from as downstream_elevation, | ||
elevation_to as upstream_elevation | ||
from tidy; |