Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated path to new NHD S3 bucket #9

Merged
merged 4 commits into from
Dec 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions 1_crosswalk_fetch/src/fetch_nhdhr.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


create_nhd_HR_download_plan <- function(states, min_size, d_tolerance, remove_IDs = NULL, keep_IDs = NULL){
base_url <- 'ftp://rockyftp.cr.usgs.gov/vdelivery/Datasets/Staged/Hydrography/NHD/State/HighResolution/GDB/NHD_H_%s_State_GDB.zip'
base_url <- 'https://prd-tnm.s3.amazonaws.com/StagedProducts/Hydrography/NHD/State/GDB/NHD_H_%s_State_GDB.zip'

fetch_as_sf_step <- create_task_step(
step_name = 'fetch_NHD_as_sf',
Expand Down Expand Up @@ -41,15 +41,19 @@ fetch_NHD_as_sf <- function(url,min_size, d_tol){
unlink(unzip_dir, recursive = TRUE)
unlink(dl_dest)
})


if(getOption('timeout') < 120){
options(timeout = 120)
}
download.file(url, destfile = dl_dest, quiet = TRUE, method='curl')
unzip(dl_dest, exdir = unzip_dir)

sf::read_sf(file.path(unzip_dir,paste0(tools::file_path_sans_ext(basename(url)), '.gdb')), layer = 'NHDWaterbody') %>%
filter(FType %in% c(390, 436, 361)) %>% #select only lakes/ponds/reservoirs. This drops things like swamp/marsh
mutate(area_m2 = AreaSqKm * 1000000) %>%
filter(ftype %in% c(390, 436, 361)) %>% #select only lakes/ponds/reservoirs. This drops things like swamp/marsh
mutate(area_m2 = areasqkm * 1000000) %>%
filter(area_m2 > min_size) %>%
mutate(site_id = paste0('nhdhr_', Permanent_Identifier)) %>% dplyr::select(site_id, GNIS_Name, Elevation, FType, FCode) %>% #geometry selected automatically
mutate(site_id = paste0('nhdhr_', permanent_identifier)) %>%
dplyr::select(site_id, gnis_name, elevation, ftype, fcode) %>% #geometry selected automatically
# bad Idaho site!
filter(site_id != 'nhdhr_{5BEDE13F-C94B-4501-B979-E00C29EA374B}') %>%
sf::st_transform(crs = 4326) %>%
Expand All @@ -64,7 +68,7 @@ GNIS_Name_xwalk <- function(ind_file, ...){

sf_lakes <- rbind(...)
deduped_sf_lakes <- st_drop_geometry(sf_lakes[!duplicated(sf_lakes$site_id), ]) %>%
dplyr::select(site_id, GNIS_Name)
dplyr::select(site_id, gnis_name)

saveRDS(deduped_sf_lakes, data_file)
gd_put(ind_file, data_file)
Expand All @@ -75,7 +79,7 @@ combine_nhd_sfs <- function(ind_file, ...){

sf_lakes <- rbind(...)
deduped_sf_lakes <- sf_lakes[!duplicated(sf_lakes$site_id), ] %>%
dplyr::select(site_id, Elevation, FType, FCode)
dplyr::select(site_id, elevation, ftype, fcode)

saveRDS(deduped_sf_lakes, data_file)
gd_put(ind_file, data_file)
Expand Down