diff --git a/R/get_data.R b/R/get_data.R index 496974b..6b402b0 100644 --- a/R/get_data.R +++ b/R/get_data.R @@ -8,5 +8,5 @@ get_nwis_sites <- function() { } get_wbd_gdb <- function(wbd_dir) { - nhdplusTools::download_wbd(outdir = wbd_dir) + nhdplusTools::download_wbd(outdir = wbd_dir, url = "https://prd-tnm.s3.amazonaws.com/StagedProducts/Hydrography/WBD/National/GDB/WBD_National_GDB.zip") } \ No newline at end of file diff --git a/R/reference_units.R b/R/reference_units.R index 60de2c7..9655fa3 100644 --- a/R/reference_units.R +++ b/R/reference_units.R @@ -1,5 +1,5 @@ get_hu <- function(wbd_gdb, hu_layer, id_attribute, gnis_base, pid_base, - out_geojson, landing_base, csv_out, description, creator = "dblodgett@usgs.gov") { + out, landing_base, csv_out, description, creator = "dblodgett@usgs.gov") { hu <- sf::read_sf(wbd_gdb, hu_layer) hu <- rmapshaper::ms_simplify(hu, sys = TRUE) @@ -17,27 +17,40 @@ get_hu <- function(wbd_gdb, hu_layer, id_attribute, gnis_base, pid_base, hu <- dplyr::select(hu, uri, NAME, gnis_url, GNIS_ID, temp_id, LOADDATE) - unlink(out_geojson, force = TRUE) + hu_level <- nchar(hu$temp_id[1]) - out <- dplyr::tibble(id = hu$uri, + if(hu_level > 2) { + replace <- substr(pid_base, 28, 29) + replacement <- paste0(0, (as.numeric(replace) - 2)) + + new_pid_base <- gsub(replace, replacement, pid_base) + + hu$containingCatchment <- paste0(new_pid_base, substr(hu$temp_id, 1, (hu_level - 2))) + } + + unlink(out, force = TRUE) + + out_pid <- dplyr::tibble(id = hu$uri, target = paste0(landing_base, hu$temp_id), creator = creator, description = description, c1_type = "QueryString", - c1_match = "f?=.*", + c1_match = "?f=.*", c1_value = paste0(landing_base, hu$temp_id, "?f=${C:f:1}")) names(hu)[names(hu) == "temp_id"] <- id_attribute - sf::write_sf(hu, out_geojson) + sf::write_sf(hu, out) - readr::write_csv(out, path = csv_out) + readr::write_csv(out_pid, path = csv_out) + + hu } -write_nat_aq <- function(nat_aq, pid_base, landing_base, out_geojson, out_csv) { +write_nat_aq <- function(nat_aq, pid_base, landing_base, out, out_csv) { nat_aq <- rmapshaper::ms_simplify(nat_aq) nat_aq$LINK[nat_aq$AQ_CODE == 610] <- "https://water.usgs.gov/ogw/aquiferbasics/pacnorbr.html" @@ -52,9 +65,9 @@ write_nat_aq <- function(nat_aq, pid_base, landing_base, out_geojson, out_csv) { nat_aq <- dplyr::filter(nat_aq, !is.na(nat_aq$NAT_AQFR_CD)) %>% select(uri, LINK, NAT_AQFR_CD, AQ_NAME, AQ_CODE, ROCK_NAME, ROCK_TYPE) - unlink(out_geojson, force = TRUE) + unlink(out, force = TRUE) - sf::write_sf(nat_aq, out_geojson) + sf::write_sf(nat_aq, out) nat_aq$uri <- paste0(pid_base, nat_aq$NAT_AQFR_CD) @@ -64,7 +77,7 @@ write_nat_aq <- function(nat_aq, pid_base, landing_base, out_geojson, out_csv) { creator = "dblodgett@usgs.gov", description = "National Aquifer Reference", c1_type = "QueryString", - c1_match = "f?=.*", + c1_match = "?f=.*", c1_value = paste0(landing_base, nat_aq$NAT_AQFR_CD, "?f=${C:f:1}")) diff --git a/runner.R b/runner.R index 54707eb..91cc9d7 100644 --- a/runner.R +++ b/runner.R @@ -17,7 +17,7 @@ plan <- drake_plan(nwis_sites = get_nwis_sites(), id_attribute = "HUC2", gnis_base = "https://geonames.usgs.gov/apex/f?p=gnispq:3:::NO::P3_FID:", pid_base = "https://geoconnex.us/ref/hu02/", - out_geojson = file_out("out/hu02.geojson"), + out = file_out("out/hu02.gpkg"), landing_base = "https://info.geoconnex.us/collections/hu02/items/", csv_out = file_out("out/hu02.csv"), description = "two digit hydrologic units reference", @@ -27,7 +27,7 @@ plan <- drake_plan(nwis_sites = get_nwis_sites(), id_attribute = "HUC4", gnis_base = "https://geonames.usgs.gov/apex/f?p=gnispq:3:::NO::P3_FID:", pid_base = "https://geoconnex.us/ref/hu04/", - out_geojson = file_out("out/hu04.geojson"), + out = file_out("out/hu04.gpkg"), landing_base = "https://info.geoconnex.us/collections/hu04/items/", csv_out = file_out("out/hu04.csv"), description = "four digit hydrologic units reference", @@ -37,7 +37,7 @@ plan <- drake_plan(nwis_sites = get_nwis_sites(), id_attribute = "HUC6", gnis_base = "https://geonames.usgs.gov/apex/f?p=gnispq:3:::NO::P3_FID:", pid_base = "https://geoconnex.us/ref/hu06/", - out_geojson = file_out("out/hu06.geojson"), + out = file_out("out/hu06.gpkg"), landing_base = "https://info.geoconnex.us/collections/hu06/items/", csv_out = file_out("out/hu06.csv"), description = "six digit hydrologic units reference", @@ -47,7 +47,7 @@ plan <- drake_plan(nwis_sites = get_nwis_sites(), id_attribute = "HUC8", gnis_base = "https://geonames.usgs.gov/apex/f?p=gnispq:3:::NO::P3_FID:", pid_base = "https://geoconnex.us/ref/hu08/", - out_geojson = file_out("out/hu08.geojson"), + out = file_out("out/hu08.gpkg"), landing_base = "https://info.geoconnex.us/collections/hu08/items/", csv_out = file_out("out/hu08.csv"), description = "eight digit hydrologic units reference", @@ -57,7 +57,7 @@ plan <- drake_plan(nwis_sites = get_nwis_sites(), id_attribute = "HUC10", gnis_base = "https://geonames.usgs.gov/apex/f?p=gnispq:3:::NO::P3_FID:", pid_base = "https://geoconnex.us/ref/hu10/", - out_geojson = file_out("out/hu10.geojson"), + out = file_out("out/hu10.gpkg"), landing_base = "https://info.geoconnex.us/collections/hu10/items/", csv_out = file_out("out/hu10.csv"), description = "ten digit hydrologic units reference", @@ -67,7 +67,7 @@ plan <- drake_plan(nwis_sites = get_nwis_sites(), nat_aq_out = write_nat_aq(nat_aq, pid_base = "https://geoconnex.us/ref/nat_aq/", landing_base = "https://info.geoconnex.us/collections/nat_aq/items/", - out_geojson = file_out("out/nat_aq.geojson"), + out = file_out("out/nat_aq.gpkg"), out_csv = file_out("out/nat_aq.csv")), wade_sites = sf::read_sf("https://www.hydroshare.org/resource/5f665b7b82d74476930712f7e423a0d2/data/contents/wade_sites.geojson"), wade_nldi = make_wade_nldi(wade_sites, file_out("out/wade.geojson")))