Skip to content

Commit

Permalink
Merge pull request #21 from JoFrhwld/18-capture-errors-in-creating-po…
Browse files Browse the repository at this point in the history
…lygons

18 capture errors in creating polygons
  • Loading branch information
JoFrhwld authored Sep 27, 2023
2 parents 99ad586 + 4aa011d commit 4a4ad78
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

export(density_area)
export(density_polygons)
import(rlang)
import(sf)
importFrom(dplyr,.data)
30 changes: 28 additions & 2 deletions R/density_area.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @import rlang
get_isolines<- function(x,
y,
probs = 0.5,
Expand All @@ -6,12 +7,16 @@ get_isolines<- function(x,
...) {

dots <- rlang::dots_list(...)
hdr_args <- rlang::fn_fmls(ggdensity::get_hdr)
use_args <- dots[names(dots) %in% names(hdr_args)]

tibble::tibble(x = x,
y = y) |>
ggdensity::get_hdr(probs = probs,
rangex = rangex,
rangey = rangey,
...) ->
!!!use_args) |>
rlang::inject() ->
density_estimate

density_estimate$df_est$z <- density_estimate$df_est$fhat
Expand Down Expand Up @@ -51,6 +56,23 @@ get_isolines_safely <- function(...){
return(iso_result$result)
}


sf_polygon_safely <- function(...){

purrr::safely(sfheaders::sf_polygon,
quiet = TRUE)(...)->
poly_result

if(!is.null(poly_result$error)){
cli::cli_warn(
c("There was a problem creating a polygon with {.fun sfheaders::sf_polygon}",
"i" = "Try examining the results of {.fun densityarea::density_polygons} with {.arg as_sf} set to {.val {FALSE}}.")
)
return(NULL)
}
return(poly_result$result)
}

isolines_to_df <- function(isolines, probs, nameswap){
isolines |>
dplyr::mutate(
Expand Down Expand Up @@ -88,13 +110,17 @@ iso_df_to_sf <- function(iso_poly_df, xname, yname){
) -> df_prepared

df_prepared |>
sfheaders::sf_polygon(
sf_polygon_safely(
x = xname,
y = yname,
polygon_id = "polygon_id",
keep = T
) -> iso_poly_pieces

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

iso_poly_pieces |>
dplyr::select(-"polygon_id") |>
sf::st_sf() ->
Expand Down

0 comments on commit 4a4ad78

Please sign in to comment.