Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEOW Biome Calculation fails in one specific area #210

Closed
karpfen opened this issue Nov 29, 2023 · 2 comments
Closed

TEOW Biome Calculation fails in one specific area #210

karpfen opened this issue Nov 29, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@karpfen
Copy link
Collaborator

karpfen commented Nov 29, 2023

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   
@karpfen karpfen added the bug Something isn't working label Nov 29, 2023
@goergen95
Copy link
Member

goergen95 commented Nov 29, 2023

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
Copy link
Collaborator Author

karpfen commented Nov 30, 2023

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

@karpfen karpfen closed this as completed Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants