mapme-initiative / mapme.biodiversity

Efficient analysis of spatial biodiversity datasets for global portfolios
https://mapme-initiative.github.io/mapme.biodiversity/dev
GNU General Public License v3.0
33 stars 7 forks source link

TEOW Biome Calculation fails in one specific area #210

Closed karpfen closed 10 months ago

karpfen commented 10 months ago

I'm trying to calculate the TEOW Biome coverage, but my script fails in some specific regions. One of them can be reproduced by this snippet:

library(sf)
library(mapme.biodiversity)

poly <- data.frame (
  id = 1,
  geom = "Polygon ((11.78575001197944339 -15.77651589055943404, 13.06044910857308849 -15.91525864937234758, 13.06044910857308849 -15.91525864937234758, 13.11247764312793151 -17.41541472903697851, 13.11247764312793151 -17.41541472903697851, 11.65567867559233584 -17.26800054779825899, 11.65567867559233584 -17.26800054779825899, 11.78575001197944339 -15.77651589055943404))"
)

poly <- st_as_sf(poly, wkt = "geom", crs = 4326)

pf_biome <- poly  %>%
  init_portfolio(
    2000:2023,
    verbose = TRUE) %>%
  get_resources("teow")
dat_biome <- calc_indicators(pf_biome, c("biome", "ecoregion"))

Upon calling calc_indicators, I receive this error message:

Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented,  : 
  Loop 0 is not valid: Edge 0 is degenerate (duplicate vertex)
Warning message:
In .compute(x[i, ], resources, fun, params, i) :
  Error occured at polygon 1 with the following error message: Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented,  : 
  Loop 0 is not valid: Edge 0 is degenerate (duplicate vertex)
. 
 Returning NAs.

I already checked if there are invalid geometries involved. The TEOW dataset does contain one, but it is not in the area of the polygon in question. I also fixed it for good measure, but the issue still exists.

A workaround is to set sf_use_s2(FALSE), but that's also not ideal.

output of sessionInfo():

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)

Matrix products: default

Random number generation:
 RNG:     L'Ecuyer-CMRG 
 Normal:  Inversion 
 Sample:  Rejection 

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] purrr_1.0.2              mapme.biodiversity_0.4.0 sf_1.0-14               

loaded via a namespace (and not attached):
 [1] dplyr_1.1.3        compiler_4.3.1     tidyselect_1.2.0   Rcpp_1.0.11        stringr_1.5.0     
 [6] parallel_4.3.1     tidyr_1.3.0        globals_0.16.2     here_1.0.1         R6_2.5.1          
[11] plyr_1.8.9         generics_0.1.3     curl_5.1.0         classInt_0.4-10    s2_1.1.4          
[16] future_1.33.0      tibble_3.2.1       logger_0.2.2       units_0.8-4        rprojroot_2.0.3   
[21] DBI_1.1.3          pillar_1.9.0       rlang_1.1.1        utf8_1.2.4         stringi_1.7.12    
[26] terra_1.7-55       cli_3.6.1          withr_2.5.2        magrittr_2.0.3     progressr_0.14.0  
[31] wk_0.9.0           class_7.3-22       digest_0.6.33      grid_4.3.1         rstudioapi_0.15.0 
[36] lifecycle_1.0.4    vctrs_0.6.3        KernSmooth_2.23-22 proxy_0.4-27       glue_1.6.2        
[41] listenv_0.9.0      furrr_0.3.1        codetools_0.2-19   parallelly_1.36.0  fansi_1.0.5       
[46] e1071_1.7-13       tools_4.3.1        pkgconfig_2.0.3   
goergen95 commented 10 months ago

I think the issue is the with the polygon you created, which is invalid. The second and third vertex are identical. If you fix it the indicator calculation works.

karpfen commented 10 months ago

With the latest changes, this now also works for the polygons I sent you via email, thanks :)