Skip to content

Commit

Permalink
gpkg and querystring update
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Jun 17, 2020
1 parent 6cd5e3f commit 5987ac8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
33 changes: 23 additions & 10 deletions R/reference_units.R
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]") {
out, landing_base, csv_out, description, creator = "[email protected]") {
hu <- sf::read_sf(wbd_gdb, hu_layer)

hu <- rmapshaper::ms_simplify(hu, sys = TRUE)
Expand All @@ -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"
Expand All @@ -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)

Expand All @@ -64,7 +77,7 @@ write_nat_aq <- function(nat_aq, pid_base, landing_base, out_geojson, out_csv) {
creator = "[email protected]",
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}"))
Expand Down
12 changes: 6 additions & 6 deletions runner.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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")))
Expand Down

0 comments on commit 5987ac8

Please sign in to comment.