Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
reference-hydrofabric
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Water Mission Area
nhgf
reference-hydrofabric
Commits
2c89d0b1
Commit
2c89d0b1
authored
5 years ago
by
Bock, Andy
Browse files
Options
Downloads
Patches
Plain Diff
First commit
parent
ad2bd0a8
No related branches found
No related tags found
1 merge request
!4
Removed "setwd"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
NHD_navigate.R
+111
-0
111 additions, 0 deletions
NHD_navigate.R
with
111 additions
and
0 deletions
NHD_navigate.R
0 → 100644
+
111
−
0
View file @
2c89d0b1
##################################################
## Project: GFv2.0
## Script purpose: Generate Segments and POIS from HUC12 outlets
## Date: 12/4/2019
## Author: abock@usgs.gov
## Notes: First cut, includes both HUC12 outlets as POIS and c
## connecting confluences; Run over Hydrologic Region 06
## NEEDED MODS
# 0 - a) create setup script for downloading and formatting data (commented out sections below)
# b) downloading data from SB into the proper formats
# c) ensuring the proper libraries are installed and loaded.
# 1 - Functionalize script with input arguments to cover workspace, results, etc.
# 2 - Add Gages to workflow
# 3 - Add NID/Waterbodies COMIDs to workflow
# 3 - Collapse Points close together
# 4 - Spit out segment length, travel time
# 5 - identify incremental catchments associated with each downstream POI
##################################################
# Network navigation for upstream/downstream from a COMID of interest
NetworkNav
<-
function
(
inCom
,
nhdDF
,
navType
){
if
(
navType
==
"up"
){
Seg
<-
get_UM
(
nhdDF
,
inCom
,
include
=
T
)
}
else
{
Seg
<-
get_DM
(
nhdDF
,
inCom
,
distance
=
1050
,
include
=
T
)
}
return
(
Seg
)
}
library
(
nhdplusTools
)
library
(
tidyverse
)
library
(
sf
)
library
(
rgdal
)
#https://cran.r-project.org/web/packages/nhdplusTools/nhdplusTools.pdf
setwd
(
"D:/abock/NHM_FY20/gfv2.0"
)
#######################
# Read Region 06 HUC12s
HUC12
<-
readOGR
(
"data/hu_points.gpkg"
,
"hu_points"
)
HUC12
@
data
$
Region
<-
substr
(
HUC12
@
data
$
HUC12
,
1
,
2
)
comIDs
<-
HUC12
@
data
%>%
filter
(
Region
==
"06"
)
%>%
select
(
COMID
)
# Set path to national NHDPlus v2 seamless data;
Print
(
"This will take awhile, go take a walk"
)
if
(
!
file.exists
(
"data/nhdplus_flowline_attributes.rds"
)){
download_nhdplusv2
(
"data"
)
nhdplus_path
(
"data/NHDPlusNationalData/NHDPlusV21_National_Seamless_Flattened_Lower48.gdb"
)
stage_national_data
(
include
=
c
(
"attribute"
,
"flowline"
),
output_path
=
"data"
)
# Get catchments later?
}
else
{
print
(
"NHDPlusv2 data downloaded and staged"
)
}
# Read flowline attribute data frames
nhdDF
<-
readRDS
(
"data/nhdplus_flowline_attributes.rds"
)
# %>% select(COMID, Pathlength, LENGTHKM, LevelPathI, UpHydroseq, Hydroseq, DnHydroseq)
# Read flowline simple features
nhd
<-
readRDS
(
"data/nhdplus_flowline.rds"
)
# Return flowlinest that are POIs
huc12POIsegs
<-
nhd
%>%
filter
(
COMID
%in%
comIDs
[,
1
])
# 15 missing?
#########################
# break segs it to points
Segpts
<-
st_geometry
(
huc12POIsegs
)
%>%
lapply
(
.
,
function
(
x
)
{
st_sfc
(
x
)
%>%
st_cast
(
.
,
'POINT'
)})
# subset the last point from each geometry, make a POINT sf object
Seg_ends
<-
sapply
(
Segpts
,
function
(
p
)
{
p
[
c
(
length
(
p
))]})
%>%
st_sfc
()
%>%
st_sf
(
'geom'
=
.
)
# Write out shapefile representing POIs for given theme
st_write
(
Seg_ends
,
dsn
=
"workspace/huc12POIs.shp"
,
delete_dsn
=
T
)
write_rds
(
Seg_ends
,
path
=
"workspace/huc12POIS.rds"
)
##########################
# Navigate upstream along the mainstems
upNet
<-
unique
(
unlist
(
lapply
(
comIDs
,
NetworkNav
,
nhdDF
,
"up"
)))
# Subset by dangling segments that need downstream navigation to be connected
upNet_DF
<-
nhd
%>%
filter
(
COMID
%in%
upNet
)
%>%
filter
(
!
DnHydroseq
%in%
Hydroseq
)
# Nagivate downstream along the mainstems
downNet
<-
unique
(
unlist
(
lapply
(
upNet_DF
$
COMID
,
NetworkNav
,
nhdDF
,
"down"
)))
# Combine upstream/downstream navigation results
huc12Segs
<-
unique
(
c
(
upNet
,
downNet
))
# Subset nhd segs to navigation results and write to shapefile
huc12Segs
<-
nhd
%>%
filter
(
COMID
%in%
huc12Segs
)
st_write
(
huc12Segs
,
dsn
=
"workspace/huc12segs.shp"
,
delete_dsn
=
T
)
write_rds
(
Seg_ends
,
path
=
"workspace/huc12segs.rds"
)
# Derive confluence POIs where they are absent
nhdConf
<-
nhd
%>%
filter
(
COMID
%in%
huc12Segs
$
COMID
)
confluences
<-
nhdConf
%>%
group_by
(
DnHydroseq
)
%>%
filter
(
n
()
>
1
)
%>%
filter
(
!
COMID
%in%
huc12POIsegs
$
COMID
)
confpts
<-
st_geometry
(
confluences
)
%>%
lapply
(
.
,
function
(
x
)
{
st_sfc
(
x
)
%>%
st_cast
(
.
,
'POINT'
)})
# subset the last point from each geometry, make a POINT sf object
conf_ends
<-
sapply
(
confpts
,
function
(
p
)
{
p
[
c
(
length
(
p
))]})
%>%
st_sfc
()
%>%
st_sf
(
'geom'
=
.
)
# Write out shapefile representing POIs for given theme
st_write
(
conf_ends
,
dsn
=
"results/confPOIs.shp"
,
delete_dsn
=
T
)
write_rds
(
conf_ends
,
path
=
"workspace/confPOIs.rds"
)
##########################################
##############DEPRECATED SECTION##################
# # retrieve POI from seg (this is deprecated)
# retrievePOI<-function(nhdPOI){
# #print (iter)
# #linepnts<-st_sfc(nhdPOI)
# linepnts<-st_cast(nhdPOI,"POINT")
# print(linepnts)
# endPoint<-linepnts[length(linepnts)]
# st_cast(endPoint,'POINT')
# }
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment