r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
865 stars 121 forks source link

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

Closed xtbgtraining closed 3 years ago

xtbgtraining commented 3 years ago

Dear Dr. Martijn Tennekes,

I got this "Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot" when I run the code, could you guide me to solve this problem.

Thanks so much!

Jingxin

Code

urb_2020 = subset(urban_agglomerations, year == 2020) tm_shape(world) + tm_borders() + tm_shape(urb_2020) + tm_symbols(col = "black", size = "population_millions", shape = 19)

sessionInfo()

################################################### R version 4.1.0 (2021-05-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 16299)

Matrix products: default

locale: [1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936

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

other attached packages: [1] shiny_1.6.0 ochRe_1.0.0 viridis_0.6.1 viridisLite_0.4.0 mapproj_1.2.7 maps_3.3.0
[7] OpenStreetMap_0.3.4 leaflet_2.0.4.1 mapview_2.10.0 cartogram_0.2.2 tmaptools_3.1-1 tmap_3.3-1
[13] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.6 purrr_0.3.4 readr_1.4.0 tidyr_1.1.3
[19] tibble_3.1.2 ggplot2_3.3.3 tidyverse_1.3.1 stars_0.5-3 abind_1.4-5 raster_3.4-10
[25] sf_1.0-0 sp_1.4-5 spDataLarge_0.5.4 spData_0.3.8

loaded via a namespace (and not attached): [1] utf8_1.2.1 tidyselect_1.1.1 lme4_1.1-27 htmlwidgets_1.5.3 grid_4.1.0
[6] devtools_2.4.2 munsell_0.5.0 codetools_0.2-18 units_0.7-2 miniUI_0.1.1.1
[11] withr_2.4.2 colorspace_2.0-1 rstudioapi_0.13 stats4_4.1.0 wk_0.4.1
[16] ggsignif_0.6.1 rJava_1.0-4 emmeans_1.6.1 piecewiseSEM_2.1.2 rprojroot_2.0.2
[21] coda_0.19-4 vctrs_0.3.8 generics_0.1.0 TH.data_1.0-10 ggpmisc_0.4.0
[26] R6_2.5.0 cachem_1.0.5 assertthat_0.2.1 promises_1.2.0.1 scales_1.1.1
[31] multcomp_1.4-17 ggExtra_0.9 gtable_0.3.0 lwgeom_0.2-6 conquer_1.0.2
[36] processx_3.5.2 sandwich_3.0-1 rlang_0.4.11 MatrixModels_0.5-0 splines_4.1.0
[41] rstatix_0.7.0 rgdal_1.5-23 dichromat_2.0-0 broom_0.7.7 BiocManager_1.30.15
[46] s2_1.0.5 reshape2_1.4.4 modelr_0.1.8 crosstalk_1.1.1 backports_1.2.1
[51] rsconnect_0.8.18 httpuv_1.6.1 DiagrammeR_1.0.6.1 tools_4.1.0 usethis_2.0.1
[56] ellipsis_0.3.2 jquerylib_0.1.4 RColorBrewer_1.1-2 proxy_0.4-26 sessioninfo_1.1.1
[61] Rcpp_1.0.6 plyr_1.8.6 base64enc_0.1-3 visNetwork_2.0.9 classInt_0.4-3
[66] ps_1.6.0 prettyunits_1.1.1 ggpubr_0.4.0 zoo_1.8-9 haven_2.4.1
[71] fs_1.5.0 leafem_0.1.6 magrittr_2.0.1 data.table_1.14.0 openxlsx_4.2.3
[76] SparseM_1.81 reprex_2.0.0 mvtnorm_1.1-2 matrixStats_0.59.0 pkgload_1.2.1
[81] packcircles_0.3.4 hms_1.1.0 shinyjs_2.0.0 mime_0.10 xtable_1.8-4
[86] XML_3.99-0.6 rio_0.5.26 readxl_1.3.1 gridExtra_2.3 testthat_3.0.2
[91] compiler_4.1.0 KernSmooth_2.23-20 crayon_1.4.1 minqa_1.2.4 htmltools_0.5.1.1
[96] later_1.2.0 lubridate_1.7.10 DBI_1.1.1 dbplyr_2.1.1 MASS_7.3-54
[101] boot_1.3-28 Matrix_1.3-3 car_3.0-10 cli_2.5.0 parallel_4.1.0
[106] pkgconfig_2.0.3 foreign_0.8-81 xml2_1.3.2 bslib_0.2.5.1 sciplot_1.2-0
[111] webshot_0.5.2 estimability_1.3 ggpp_0.4.0 rvest_1.0.0 callr_3.7.0
[116] digest_0.6.27 cellranger_1.1.0 leafsync_0.1.0 curl_4.3.1 quantreg_5.86
[121] satellite_1.0.2 nloptr_1.2.2.2 lifecycle_1.0.0 nlme_3.1-152 jsonlite_1.7.2
[126] carData_3.0-4 desc_1.3.0 fansi_0.5.0 pillar_1.6.1 lattice_0.20-44
[131] fastmap_1.1.0 httr_1.4.2 pkgbuild_1.2.0 survival_3.2-11 glue_1.4.2
[136] remotes_2.4.0 zip_2.2.0 png_0.1-7 leaflet.providers_1.9.0 sass_0.4.0
[141] class_7.3-19 stringi_1.6.2 memoise_2.0.0 e1071_1.7-7

xtbgtraining commented 3 years ago

interestingly, this problem was solved by downgrade sf from version 1.0.0 to 0.9.8 ################ library(spData) # Datasets library(spDataLarge) # Datasets

library(sp) # Vector data library(sf) # Vector data library(raster) # Raster data library(stars) # Raster data

library(tidyverse) # Tidyverse packages (including ggplot2) library(tmap) # Static and interactive maps library(tmaptools) # Static and interactive maps

library(cartogram) # Create cartograms library(mapview) # Interactive maps library(leaflet) # Interactive maps library(OpenStreetMap)# OpenStreetMap raster map library(maps) # Some map data library(mapproj) # Basic and ggplot map projections

library(viridis) # palettes library(ochRe) # palettes

urb_2020 = subset(urban_agglomerations, year == 2020) tm_shape(world) + tm_borders() + tm_shape(urb_2020) + tm_symbols(col = "black", size = "population_millions", shape = 19) ################ sessionInfo() R version 4.1.0 (2021-05-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 16299)

Matrix products: default

locale: [1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936

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

other attached packages: [1] ochRe_1.0.0 viridis_0.6.1 viridisLite_0.4.0 mapproj_1.2.7 maps_3.3.0 OpenStreetMap_0.3.4 [7] leaflet_2.0.4.1 mapview_2.10.0 cartogram_0.2.2 tmaptools_3.1-1 tmap_3.3-1 forcats_0.5.1
[13] stringr_1.4.0 dplyr_1.0.6 purrr_0.3.4 readr_1.4.0 tidyr_1.1.3 tibble_3.1.2
[19] ggplot2_3.3.3 tidyverse_1.3.1 stars_0.5-3 abind_1.4-5 raster_3.4-10 sf_0.9-8
[25] sp_1.4-5 spDataLarge_0.5.4 spData_0.3.8

loaded via a namespace (and not attached): [1] readxl_1.3.1 backports_1.2.1 lwgeom_0.2-6 plyr_1.8.6 splines_4.1.0
[6] crosstalk_1.1.1 usethis_2.0.1 TH.data_1.0-10 digest_0.6.27 htmltools_0.5.1.1
[11] leaflet.providers_1.9.0 fansi_0.5.0 magrittr_2.0.1 memoise_2.0.0 openxlsx_4.2.3
[16] remotes_2.4.0 modelr_0.1.8 sandwich_3.0-1 prettyunits_1.1.1 colorspace_2.0-1
[21] rvest_1.0.0 rgdal_1.5-23 haven_2.4.1 leafem_0.1.6 callr_3.7.0
[26] crayon_1.4.1 jsonlite_1.7.2 lme4_1.1-27 survival_3.2-11 zoo_1.8-9
[31] glue_1.4.2 gtable_0.3.0 emmeans_1.6.1 webshot_0.5.2 car_3.0-10
[36] pkgbuild_1.2.0 scales_1.1.1 mvtnorm_1.1-2 DBI_1.1.1 Rcpp_1.0.6
[41] xtable_1.8-4 units_0.7-2 foreign_0.8-81 proxy_0.4-26 stats4_4.1.0
[46] piecewiseSEM_2.1.2 htmlwidgets_1.5.3 httr_1.4.2 DiagrammeR_1.0.6.1 RColorBrewer_1.1-2
[51] ellipsis_0.3.2 rJava_1.0-4 pkgconfig_2.0.3 XML_3.99-0.6 dbplyr_2.1.1
[56] utf8_1.2.1 tidyselect_1.1.1 rlang_0.4.11 reshape2_1.4.4 munsell_0.5.0
[61] cellranger_1.1.0 tools_4.1.0 visNetwork_2.0.9 cachem_1.0.5 cli_2.5.0
[66] generics_0.1.0 devtools_2.4.2 broom_0.7.7 fastmap_1.1.0 processx_3.5.2
[71] leafsync_0.1.0 fs_1.5.0 zip_2.2.0 satellite_1.0.2 nlme_3.1-152
[76] xml2_1.3.2 compiler_4.1.0 rstudioapi_0.13 curl_4.3.1 png_0.1-7
[81] e1071_1.7-7 testthat_3.0.2 reprex_2.0.0 stringi_1.6.2 ps_1.6.0
[86] desc_1.3.0 lattice_0.20-44 Matrix_1.3-3 classInt_0.4-3 nloptr_1.2.2.2
[91] vctrs_0.3.8 pillar_1.6.1 lifecycle_1.0.0 BiocManager_1.30.15 estimability_1.3
[96] data.table_1.14.0 R6_2.5.0 gridExtra_2.3 KernSmooth_2.23-20 rio_0.5.26
[101] sessioninfo_1.1.1 codetools_0.2-18 dichromat_2.0-0 boot_1.3-28 MASS_7.3-54
[106] assertthat_0.2.1 pkgload_1.2.1 rprojroot_2.0.2 withr_2.4.2 multcomp_1.4-17
[111] parallel_4.1.0 hms_1.1.0 grid_4.1.0 coda_0.19-4 class_7.3-19
[116] minqa_1.2.4 carData_3.0-4 packcircles_0.3.4 lubridate_1.7.10 base64enc_0.1-3

mtennekes commented 3 years ago

Yes, we know and are working on it. See #564 and also https://github.com/r-spatial/sf/issues/1649.

Saadi4469 commented 3 years ago

Any update on this please?

mtennekes commented 3 years ago

Should have been fixed in CRAN 3.3-2 (released on 2021-06-16).

Let us know if there are any problems (with a reproducible example please).

baarthur commented 3 years ago

Hi,

I'm recently experiencing the same problem here with two shapefiles (this one and another), but others work fine. Is the problem with my files or is it with tmap?

When running st_is_valid():

> st_is_valid(shp_distritos_policiais)
 [1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
[26] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
[51]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
[76]  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

Fun thing is that if I use tmap_options(check.and.fix = TRUE) as suggested, I can plot it with tm_borders() but not with tm_fill(). I get:

Error in gsub("</", "\\u003c/", payload, fixed = TRUE) : 
  input string 1 is invalid UTF-8
In addition: Warning message:
The shape shp_distritos_policiais is invalid. See sf::st_is_valid 

However, if I use a variable as fill, I can plot it with no problems:

Edit: forgot to mention that I'm using tmap_mode("view"). I just tested with "plot" and it plots, though I still get error messages.

Reprex

The shapefile is attached and I got it from here: https://github.com/sergiospagnuolo/distritos-sp/tree/master/cidade

shp_distritos_policiais <- st_read(dsn = "data/shp_distritos_policiais/shp_distritos_policiais.shp")

df <- tibble(COD_DIST_A = 1:103, value = rnorm(103))

shp_distritos_policiais <- shp_distritos_policiais %>% 
  dplyr::left_join(df)

tm_shape(shp_distritos_policiais) +
  tm_borders()
# Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

tmap_options(check.and.fix = TRUE)
tm_shape(shp_distritos_policiais) +
  tm_fill(col = "grey")
#Error in gsub("</", "\\u003c/", payload, fixed = TRUE) : 
#  input string 1 is invalid UTF-8
#In addition: Warning message:
#The shape shp_distritos_policiais is invalid. See sf::st_is_valid

tm_shape(shp_distritos_policiais) +
  tm_fill(col = "value")
# The shape shp_distritos_policiais is invalid. See sf::st_is_valid 

sessionInfo()

sessionInfo() R version 4.1.1 (2021-08-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale: [1] LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252 LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C
[5] LC_TIME=Portuguese_Brazil.1252

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

other attached packages: [1] tmap_3.3-2 aopdata_0.2.2 geobr_1.6.4 sf_1.0-2 readxl_1.3.1 modelr_0.1.8 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7
[10] purrr_0.3.4 readr_2.0.2 tidyr_1.1.4 tibble_3.1.5 ggplot2_3.3.5 tidyverse_1.3.1

loaded via a namespace (and not attached): [1] fs_1.5.0 lubridate_1.7.10 RColorBrewer_1.1-2 httr_1.4.2 tools_4.1.1 backports_1.2.1 utf8_1.2.2 R6_2.5.1
[9] KernSmooth_2.23-20 DBI_1.1.1 colorspace_2.0-2 raster_3.4-13 withr_2.4.2 sp_1.4-5 tidyselect_1.1.1 leaflet_2.0.4.1
[17] curl_4.3.2 compiler_4.1.1 leafem_0.1.6 cli_3.0.1 rvest_1.0.1 xml2_1.3.2 triebeard_0.3.0 scales_1.1.1
[25] classInt_0.4-3 proxy_0.4-26 digest_0.6.28 rmarkdown_2.11 base64enc_0.1-3 dichromat_2.0-0 pkgconfig_2.0.3 htmltools_0.5.2
[33] dbplyr_2.1.1 fastmap_1.1.0 htmlwidgets_1.5.4 rlang_0.4.11 httpcode_0.3.0 rstudioapi_0.13 generics_0.1.0 jsonlite_1.7.2
[41] crosstalk_1.1.1 magrittr_2.0.1 s2_1.0.7 Rcpp_1.0.7 munsell_0.5.0 fansi_0.5.0 abind_1.4-5 lifecycle_1.0.1
[49] stringi_1.7.5 leafsync_0.1.0 yaml_2.2.1 tmaptools_3.1-1 grid_4.1.1 parallel_4.1.1 crayon_1.4.1 lattice_0.20-44
[57] stars_0.5-3 haven_2.4.3 hms_1.1.1 knitr_1.36 pillar_1.6.3 codetools_0.2-18 crul_1.1.0 wk_0.5.0
[65] reprex_2.0.1 XML_3.99-0.8 glue_1.4.2 evaluate_0.14 data.table_1.14.2 urltools_1.7.3 vctrs_0.3.8 png_0.1-7
[73] tzdb_0.1.2 cellranger_1.1.0 gtable_0.3.0 assertthat_0.2.1 xfun_0.23 lwgeom_0.2-7 broom_0.7.9 e1071_1.7-9
[81] rsconnect_0.8.24 class_7.3-19 viridisLite_0.4.0 units_0.7-2 ellipsis_0.3.2

shp_distritos_policiais.zip