Skip to content

Commit

Permalink
make geoserver downloads more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Aug 6, 2022
1 parent e4d98a5 commit 5f21e37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions R/geoserver_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ query_usgs_geoserver <- function(AOI = NULL, ids = NULL,
return(NULL)
})

use_s2 <- sf::sf_use_s2()
sf::sf_use_s2(FALSE)

on.exit(sf::sf_use_s2(use_s2), add = TRUE)

out <- tryCatch({check_valid(sf::st_zm(sf::read_sf(resp)), out_prj = t_srs)},
error = function(e){ return(NULL) })

Expand Down
22 changes: 20 additions & 2 deletions R/subset_nhdplus.R
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ check_valid <- function(x, out_prj = sf::st_crs(x)) {

if(is.null(x)){return(NULL)}

return_now <- FALSE

x <- sf::st_zm(x)

if (!all(sf::st_is_valid(x))) {
Expand All @@ -629,8 +631,17 @@ check_valid <- function(x, out_prj = sf::st_crs(x)) {

orig_type <- orig_type[grepl("POLY|LINE", orig_type)]

try({
x <- sf::st_make_valid(x)
x <- tryCatch({
sf::st_make_valid(x)
}, error = function(e) {
warning("Error trying to make geometry valid. Returning invalid geometry.")
return_now <<- TRUE
x
})

if(return_now) return(x)

tryCatch({

if(!all(sf::st_geometry_type(x) == orig_type)) {
if(any(grepl("^GEOMETRY", sf::st_geometry_type(x)))) {
Expand All @@ -645,7 +656,14 @@ check_valid <- function(x, out_prj = sf::st_crs(x)) {

}
}
}, error = function(e) {
warning("Error while trying to unify geometry type. \nReturning geometry as is.")
return_now <<- TRUE
x
})

if(return_now) return(x)

}

if (any(grepl("POLYGON", class(sf::st_geometry(x))))) {
Expand Down

0 comments on commit 5f21e37

Please sign in to comment.