ateucher / rmapshaper

An R wrapper for the mapshaper javascript library
http://andyteucher.ca/rmapshaper/
Other
200 stars 13 forks source link

can't ms_simplify() a donut polygon with another polygon inside it #105

Closed SimonCoulombe closed 4 years ago

SimonCoulombe commented 4 years ago

I am trying to simplify the following shapefile:

http://ftp.maps.canada.ca/pub/elections_elections/Electoral-districts_Circonscription-electorale/Elections_Canada_2019/polling_divisions_boundaries_2019.shp.zip

I am having issue with the following two polygons (a small one inside a big one)

damn_two <- sf::st_read(here::here("content/post/data/downloads/","PD_CA_2019_EN.shp")) %>% 
  filter(FEDNUM  == 10003, PDNUM %in% c(177, 178) ) 

damn_two %>% mapview(zcol = "PDNUM")

damntwo

If I dont add any options, then the small polygon is dropped:


damn_two %>% rmapshaper::ms_simplify()
Simple feature collection with 1 feature and 14 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 8627930 ymin: 2025880 xmax: 8815810 ymax: 2192651
CRS:            3347
  PDNUMSFX PDTYPE FEDNUM ADVPOLLNUM PDNUM PDNUMSFXCO CIVICNUM CIVICNUMSF STREETNAME STREETTYPE STREETDIR BLDGNAMEEN BLDGNAMEFR POLLNAME                       geometry
1        0      N  10003        617   177        177     <NA>       <NA>       <NA>       <NA>      <NA>       <NA>       <NA>  Buchans POLYGON ((8806836 2181057, ...

and if I ask it to keep all shapes then I get an error:


damn_two %>% rmapshaper::ms_simplify(keep_shapes= TRUE)

Error in context_eval(join(src), private$context, serialize) : Error in V8 context: ReferenceError: Uint8ClampedArray is not defined at <anonymous>:1:5492 at FlatQueue.ids (<anonymous>:1:3995) at Object.<anonymous> (<anonymous>:1:4109) at o (<anonymous>:1:265) at <anonymous>:1:316 at Object.internal.getBoundsSearchFunction (<anonymous>:1:586891) at Object.internal.groupPolygonRings (<anonymous>:1:563644) at GeoJSON.exportPolygonGeom (<anonymous>:1:739932) at <anonymous>:1:736355 at Array.reduce (native)

Any idea how to work around this?

ateucher commented 4 years ago

Hi @SimonCoulombe, thanks for reporting this. The behaviour of dropping the polygon is expected, but the error indicates that your version of the V8 package (which rmapshaper relies on) is built with an old version of the libv8 system library. Can you please post the output of sessionInfo() and V8::engine_info()?

SimonCoulombe commented 4 years ago

here is the output, I'll try to update libv8 next:


> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.10

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8    LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] rmapshaper_0.4.3 mapview_2.7.5    mapdeck_0.3.1    DT_0.13          sf_0.9-0         furrr_0.1.0.9002 future_1.16.0    forcats_0.5.0    stringr_1.4.0    dplyr_0.8.5      purrr_0.3.3      readr_1.3.1      tidyr_1.0.2      tibble_2.1.3     ggplot2_3.3.0    tidyverse_1.3.0 

loaded via a namespace (and not attached):
 [1] nlme_3.1-137       fs_1.3.2           satellite_1.0.2    lubridate_1.7.4    webshot_0.5.2      httr_1.4.1         rprojroot_1.3-2    tools_3.5.1        backports_1.1.5    R6_2.4.1           KernSmooth_2.23-15 DBI_1.1.0          colorspace_1.4-1   raster_3.0-12      withr_2.1.2       
[16] sp_1.4-1           tidyselect_1.0.0   leaflet_2.0.3      curl_4.3           compiler_3.5.1     leafem_0.0.7       cli_2.0.2          rvest_0.3.5        xml2_1.2.5         bookdown_0.18      scales_1.1.0       classInt_0.4-2     digest_0.6.25      rmarkdown_2.1      base64enc_0.1-3   
[31] pkgconfig_2.0.3    htmltools_0.4.0    dbplyr_1.4.2       jsonvalidate_1.1.0 htmlwidgets_1.5.1  rlang_0.4.5        readxl_1.3.1       httpcode_0.2.0     rstudioapi_0.11    generics_0.0.2     jsonlite_1.6.1     crosstalk_1.1.0.1  magrittr_1.5       Rcpp_1.0.4         munsell_0.5.0     
[46] fansi_0.4.1        lifecycle_0.2.0    stringi_1.4.6      yaml_2.2.1         grid_3.5.1         widgetframe_0.3.1  parallel_3.5.1     listenv_0.8.0      crayon_1.3.4       lattice_0.20-35    haven_2.2.0        hms_0.5.3          knitr_1.28         pillar_1.4.3       geojsonlint_0.4.0 
[61] codetools_0.2-15   stats4_3.5.1       crul_0.9.0         reprex_0.3.0       glue_1.3.2         packrat_0.5.0      evaluate_0.14      blogdown_0.18      V8_3.0.2           modelr_0.1.6       png_0.1-7          vctrs_0.2.4        cellranger_1.1.0   gtable_0.3.0       assertthat_0.2.1  
[76] xfun_0.12          broom_0.5.5        e1071_1.7-3        class_7.3-14       viridisLite_0.3.0  units_0.6-6        globals_0.12.5     here_0.1          
> V8::engine_info()
$version
[1] "3.14.5.9"
SimonCoulombe commented 4 years ago

turns out it is the latest version.


(base) simon@ryzen:~/git/snippets$ sudo apt-get install  libv8-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libv8-dev is already the newest version (3.14.5.8-11ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

edit.. oh not it isnt:


> library(rmapshaper)
Warning: v8 Engine is version 3.14.5.9 but version >=6 is required for full functionality. Some rmapshaper functions, notably ms_clip() and ms_erase(), may not work. See https://github.com/jeroen/V8 for help installing a modern version of v8 on your operating system.)

edit 2 : oh , I'm on ubuntu 18.10,, it "no longer has a release file". I think this will have to wait for my ubuntu 20.04 update at the end of the month :)

ateucher commented 4 years ago

There is a ppa for Bionic/Xenial with a modern libv8, now called libnode. From the V8 installation instructions:

sudo add-apt-repository ppa:cran/v8
sudo apt-get update
sudo apt-get install libnode-dev

Then you can reinstall V8. Please let me know if that fixes it

SimonCoulombe commented 4 years ago

Hi Andy,

Thanks for helping :)
I couldnt use these instructions because I was on ubuntu 18.10. Last night I updated to 19.04, then 19.10. It borked my R install and now I have to re-install all packages. I'll let you know after that if my computer doesnt explode by then.

I managed to simplify the file and complete the project on the Windows computer after splitting it into 3 parts.

SimonCoulombe commented 4 years ago

update: I can ms_simplify(keep_shapes = TRUE, explode = TRUE) the full shapefile on my updated linux afterrunning lwgeom:: st_make_valid() .

all is well. thanks for the package!!

ateucher commented 4 years ago

Glad you got it working in the end, sorry you had to go through all the trials and tribulations. I didn’t realize that ppa was only for 18.04 and not 18.10