Skip to content

Commit

Permalink
Merge pull request #408 from DOI-USGS/splinters
Browse files Browse the repository at this point in the history
clean up splinters from split catchment fixes #406
  • Loading branch information
dblodgett-usgs authored Aug 2, 2024
2 parents e2eae62 + 42e383a commit 4051bf6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
40 changes: 37 additions & 3 deletions R/get_oaproc.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ get_raindrop_trace <- function(point, direction = "down") {
#' most users of this function will want to use \link{get_raindrop_trace} prior
#' to calls to this function.
#'
#' An attempt is made to eliminate polygon shards if they exist in the output.
#' However, there is a chance that this function will return a multipolygon
#' data.frame.
#'
#' @export
#' @examples
#' \donttest{
Expand Down Expand Up @@ -115,10 +119,40 @@ get_split_catchment <- function(point, upstream = TRUE) {

url <- paste0(url_base, "nldi-splitcatchment/execution")

return(sf_post(url, make_json_input_split(point, upstream),
out <- sf_post(url, make_json_input_split(point, upstream),
err_mess = paste("Ensure that the point you submitted is within\n the",
"coterminous US and consider trying get_raindrop_trace\ to ensure",
"your point is not too close to a catchment boundary.")))
"coterminous US and consider trying get_raindrop_trace\ to ensure",
"your point is not too close to a catchment boundary."))

try({
if(!is.null(out)) {
sf::st_geometry(out) <- sf::st_sfc(lapply(sf::st_geometry(out), remove_shards),
crs = sf::st_crs(out))

if(sf::st_geometry_type(out, by_geometry = FALSE) == "GEOMETRY") {
sf::st_geometry(out) <- sf::st_sfc(lapply(sf::st_geometry(out),
sf::st_cast, to = "MULTIPOLYGON"),
crs = sf::st_crs(out))
}
}
}, silent = TRUE)

return(out)
}

remove_shards <- function(g, thresh = 0.01) {

if(length(g) == 1) return(sf::st_polygon(g[[1]]))

p <- sf::st_cast(sf::st_sfc(g), "POLYGON")

a <- sf::st_area(p)

p <- p[a > max(a) * thresh]

if(length(p) > 1) return(sf::st_multipolygon(p))

sf::st_polygon(p[[1]])
}

#' Get Cross Section From Point (experimental)
Expand Down
4 changes: 4 additions & 0 deletions man/get_split_catchment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4051bf6

Please sign in to comment.