concaveman icon indicating copy to clipboard operation
concaveman copied to clipboard

Error: Shape contains invalid polygons

Open tungttnguyen opened this issue 4 years ago • 1 comments

Hello,

I tried running the example but got the following error:

> Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

The code still did not work (was frozen) after setting the tmap_options(check.and.fix = TRUE)

Can you check what went wrong? Thank you!

library(concaveman)
library(dplyr)
library(purrr)
library(sf)
library(tmap)

data(points)

polygons <- map(unique(points$k),
                ~ concaveman(points[points$k %in% .,])
) %>% 
  map2(unique(points$k), ~ mutate(.x, k = .y)) %>% 
  reduce(rbind)

tm_shape(points) +
  tm_dots(col = "k", size = 0.1, legend.show = FALSE) +
  tm_shape(polygons) +
  tm_fill(col = "k", alpha = 0.5, legend.show = FALSE) +
  tm_borders() +
  tm_layout(frame = FALSE)
#> Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

Created on 2021-10-19 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       America/Los_Angeles         
#>  date     2021-10-19                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package      * version     date       lib
#>  abind          1.4-5       2016-07-21 [1]
#>  assertthat     0.2.1       2019-03-21 [1]
#>  backports      1.2.1       2020-12-09 [1]
#>  base64enc      0.1-3       2015-07-28 [1]
#>  class          7.3-19      2021-05-03 [2]
#>  classInt       0.4-3       2020-04-07 [1]
#>  cli            3.0.1       2021-07-17 [1]
#>  codetools      0.2-18      2020-11-04 [2]
#>  concaveman   * 1.1.0       2020-05-11 [1]
#>  crayon         1.4.1       2021-02-08 [1]
#>  crosstalk      1.1.1       2021-01-12 [1]
#>  curl           4.3.2       2021-06-23 [1]
#>  DBI            1.1.1       2021-01-15 [1]
#>  dichromat      2.0-0       2013-01-24 [1]
#>  digest         0.6.28      2021-09-23 [1]
#>  dplyr        * 1.0.7.9000  2021-10-15 [1]
#>  e1071          1.7-9       2021-09-16 [1]
#>  ellipsis       0.3.2       2021-04-29 [1]
#>  evaluate       0.14        2019-05-28 [1]
#>  fansi          0.5.0       2021-05-25 [1]
#>  fastmap        1.1.0       2021-01-25 [1]
#>  fs             1.5.0       2020-07-31 [1]
#>  generics       0.1.0       2020-10-31 [1]
#>  glue           1.4.2       2020-08-27 [1]
#>  highr          0.9         2021-04-16 [1]
#>  htmltools      0.5.2.9000  2021-08-26 [1]
#>  htmlwidgets    1.5.4       2021-09-08 [1]
#>  httr           1.4.2       2020-07-20 [1]
#>  jsonlite       1.7.2       2020-12-09 [1]
#>  KernSmooth     2.23-20     2021-05-03 [2]
#>  knitr          1.36        2021-09-29 [1]
#>  lattice        0.20-45     2021-09-22 [1]
#>  leafem         0.1.6       2021-05-24 [1]
#>  leaflet        2.0.4.1     2021-01-07 [1]
#>  leafsync       0.1.0       2019-03-05 [1]
#>  lifecycle      1.0.1       2021-09-24 [1]
#>  lwgeom         0.2-8       2021-10-06 [1]
#>  magrittr       2.0.1       2020-11-17 [1]
#>  mime           0.12        2021-09-28 [1]
#>  pillar         1.6.4       2021-10-18 [1]
#>  pkgconfig      2.0.3       2019-09-22 [1]
#>  png            0.1-7       2013-12-03 [1]
#>  proxy          0.4-26      2021-06-07 [1]
#>  purrr        * 0.3.4.9000  2021-05-18 [1]
#>  R.cache        0.15.0      2021-04-30 [1]
#>  R.methodsS3    1.8.1       2020-08-26 [1]
#>  R.oo           1.24.0      2020-08-26 [1]
#>  R.utils        2.11.0      2021-09-26 [1]
#>  R6             2.5.1       2021-08-19 [1]
#>  raster         3.5-2       2021-10-11 [1]
#>  RColorBrewer   1.1-2       2014-12-07 [1]
#>  Rcpp           1.0.7       2021-07-07 [1]
#>  reprex         2.0.1       2021-08-05 [1]
#>  rlang          0.99.0.9000 2021-10-19 [1]
#>  rmarkdown      2.11        2021-09-14 [1]
#>  rstudioapi     0.13        2020-11-12 [1]
#>  s2             1.0.7       2021-09-28 [1]
#>  sessioninfo    1.1.1       2018-11-05 [1]
#>  sf           * 1.0-3       2021-10-07 [1]
#>  sp             1.4-5       2021-01-10 [1]
#>  stars          0.5-3       2021-06-08 [1]
#>  stringi        1.7.5       2021-10-04 [1]
#>  stringr        1.4.0       2019-02-10 [1]
#>  styler         1.6.2       2021-09-23 [1]
#>  terra          1.4-14      2021-10-15 [1]
#>  tibble         3.1.5       2021-09-30 [1]
#>  tidyselect     1.1.1       2021-04-30 [1]
#>  tmap         * 3.3-2       2021-06-16 [1]
#>  tmaptools      3.1-1       2021-01-19 [1]
#>  units          0.7-2       2021-06-08 [1]
#>  utf8           1.2.2       2021-07-24 [1]
#>  V8             3.4.2       2021-05-01 [1]
#>  vctrs          0.3.8       2021-04-29 [1]
#>  viridisLite    0.4.0       2021-04-13 [1]
#>  withr          2.4.2       2021-04-18 [1]
#>  wk             0.5.0       2021-07-13 [1]
#>  xfun           0.27        2021-10-18 [1]
#>  XML            3.99-0.8    2021-09-17 [1]
#>  xml2           1.3.2       2020-04-23 [1]
#>  yaml           2.2.1       2020-02-01 [1]
#>  source                                   
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.1)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.1)                           
#>  Github (tidyverse/dplyr@74ace27)         
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  Github (rstudio/htmltools@6a03c3f)       
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.1)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  Github (tidyverse/purrr@5aca9df)         
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  Github (r-lib/rlang@eb77d6e)             
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.1)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.1)                           
#>  https://rspatial.r-universe.dev (R 4.1.1)
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.1)                           
#>  CRAN (R 4.1.0)                           
#>  CRAN (R 4.1.0)                           
#> 

tungttnguyen avatar Oct 19 '21 18:10 tungttnguyen

I've also experienced this. Increasing the concavity value can make the returned polygon valid, but the shape becomes more of a convex hull.

prncevince avatar Aug 02 '22 18:08 prncevince