Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dblodgett-usgs/ef36f5e4208548048b2d8d7b8fedef81 to your computer and use it in GitHub Desktop.
Save dblodgett-usgs/ef36f5e4208548048b2d8d7b8fedef81 to your computer and use it in GitHub Desktop.
basic catchment splitting
reprex::reprex({
#' @example
#' p <- sf::st_point(x = c(-73.82705, 43.29139), dim = "XY")
#' make_json_input(p)
#'
make_json_input <- function(p) {
jsonlite::toJSON(list(inputs = list(list(id = "lat",
type = "text/plain",
value = p[2]),
list(id = "lng",
type = "text/plain",
value = p[1]))),
pretty = TRUE, auto_unbox = TRUE)
}
p <- sf::st_point(x = c(-73.82705, 43.29139), dim = "XY")
url <- "https://labs.dev-wma.chs.usgs.gov/pygeoapi/processes/nldi-delineate/jobs?response=document"
out <- httr::POST(url, httr::accept_json(), httr::content_type_json(), body = make_json_input(p))
json <- jsonlite::fromJSON(rawToChar(out$content), simplifyVector = FALSE)
nms <- names(json[[1]][[1]]$value)
geoms <- lapply(json[[1]][[1]]$value, function (x) sf::read_sf(jsonlite::toJSON(x, auto_unbox = TRUE)))
names(geoms) <- nms
plot(sf::st_geometry(geoms$upstreamBasin), col = NA)
plot(sf::st_sfc(p, crs = 4326), add = TRUE, pch = 17, col = "red")
plot(sf::st_geometry(geoms$catchment), col = NA)
plot(sf::st_geometry(geoms$splitCatchment), col = NA, add = TRUE)
plot(sf::st_geometry(geoms$mergedCatchment), col = NA, add = TRUE)
plot(sf::st_sfc(p, crs = 4326), add = TRUE, pch = 17, col = "red")
})
@dblodgett-usgs
Copy link
Author

make_json_input <- function(p) {
  jsonlite::toJSON(list(inputs = list(list(id = "lat",
                          type = "text/plain",
                          value = p[2]),
                     list(id = "lng",
                          type = "text/plain",
                          value = p[1]))),
                   pretty = TRUE, auto_unbox = TRUE)
}

p <- sf::st_point(x = c(-73.82705, 43.29139), dim = "XY")

url <- "https://labs.dev-wma.chs.usgs.gov/pygeoapi/processes/nldi-delineate/jobs?response=document"

out <- httr::POST(url, httr::accept_json(), httr::content_type_json(), body = make_json_input(p))

json <- jsonlite::fromJSON(rawToChar(out$content), simplifyVector = FALSE)

nms <- names(json[[1]][[1]]$value)

geoms <- lapply(json[[1]][[1]]$value, function (x) sf::read_sf(jsonlite::toJSON(x, auto_unbox = TRUE)))

names(geoms) <- nms

plot(sf::st_geometry(geoms$upstreamBasin), col = NA)
plot(sf::st_sfc(p, crs = 4326), add = TRUE, pch = 17, col = "red")

plot(sf::st_geometry(geoms$catchment), col = NA)
plot(sf::st_geometry(geoms$splitCatchment), col = NA, add = TRUE)
plot(sf::st_geometry(geoms$mergedCatchment), col = NA, add = TRUE)
plot(sf::st_sfc(p, crs = 4326), add = TRUE, pch = 17, col = "red")

Created on 2021-05-11 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment