rmapshaper icon indicating copy to clipboard operation
rmapshaper copied to clipboard

ms_simplify 0.4.1 aborts R session

Open fabiocs8 opened this issue 6 years ago • 10 comments

Hi,

When executing the following code, ms_simplify causes R to abort after about 2 minutes running.

library(rgdal)               #1.3-6 version
library (rgeos)             #0.4-2 version
library(rmapshaper)    #0.4.1 version
shp.mun <- 
  readOGR("C:\\br_municipios\\."
          , "BRMUE250GC_SIR"
          , stringsAsFactors=FALSE
          , encoding="UTF-8"
  )
shp2 <- ms_simplify(shp.mun) # the execution of this line causes R 3.5.1 to abort

The map file can be found here https://1drv.ms/f/s!Av16Xvc5Na3Ngr12e_gRbjyZXjyh4g OBS 1): gSimplify can handle this map, thus I believe that map´s format is ok OBS 2): I successfully simplified other maps using ms_simplify, so my rmapshaper version is supposedly ok and well installed.

Please let me know if you need additional clarification.

Regards Fabio

fabiocs8 avatar Nov 26 '18 17:11 fabiocs8

Hi Fabio,

Thanks for the report, I can reproduce this and will investigage. Interestingly, this works with sf:

library(sf)
library(rmapshaper)
shp.mun.sf <- read_sf("BRMUE250GC_SIR.shp")
shp2 <- ms_simplify(shp.mun)

ateucher avatar Nov 27 '18 23:11 ateucher

Hello Andy,

I tried through sf. It took a while but worked.

However, when fortifying it through broom::tidy(), it generated an error:

shp3 <- tidy(shp2) Error in is.data.frame(x) : (list) object cannot be coerced to type 'double' In addition: Warning messages: 1: 'tidy.data.frame' is deprecated. See help("Deprecated") 2: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA 3: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA 4: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA 5: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : NAs introduced by coercion

Note that I am able to fortify either when 1) I use gSimplify, or 2) when I use a smaller map with ms_simplify at https://1drv.ms/f/s!Av16Xvc5Na3NgrgmU16K-7FG0hN35g

Kind regards,

Fabio Corrêa

Em 27/11/2018 21:39, Andy Teucher escreveu:

Hi Fabio,

Thanks for the report, I can reproduce this and will investigage. Interestingly, this works with |sf|:

library(sf) library(rmapshaper) shp.mun.sf <- read_sf("BRMUE250GC_SIR.shp") shp2 <- ms_simplify(shp.mun)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ateucher/rmapshaper/issues/83#issuecomment-442260623, or mute the thread https://github.com/notifications/unsubscribe-auth/AjqJCaNnOumO2AhK5DYOjmH60iEr-sGLks5uzc0ZgaJpZM4YzhsM.


Este email foi escaneado pelo Avast antivírus. https://www.avast.com/antivirus

fabiocs8 avatar Nov 28 '18 01:11 fabiocs8

Yes, an sf object is an entirely different beast than a "Spatial" object from package sp, and there is no tidy method for sf objects.

I recommend checking out the sf package, and plotting sf objects directly with ggplot2 via geom_sf (I'm just guessing that's why you want to fortify).

If you want to stay with sp "Spatial" objects, a work-around is to convert to sf, run it through ms_simplify(), and then back to "Spatial":

library(rgdal)
library(sf)
library(rmapshaper)
library(broom)
shp_sp <- readOGR(".", "BRMUE250GC_SIR", stringsAsFactors = FALSE, encoding = "UTF-8")
shp_sf <- st_as_sf(shp_sp)
shp2_sf <- ms_simplify(shp_sf)
shp2_sp <- as(shp2_sf, "Spatial")
shp_tidy <- tidy(shp2_sp)

I will look at why it is crashing, but this should work in the meantime...

ateucher avatar Nov 28 '18 05:11 ateucher

Thank you Andy, learning a lot with you. Best regards,

Fábio

On Wed, Nov 28, 2018, 03:15 Andy Teucher <[email protected] wrote:

Yes, an sf object is an entirely different beast than a "Spatial" object from package sp, and there is no tidy method for sf objects.

I recommend checking out the sf package, and plotting sf objects directly with ggplot2 via geom_sf (I'm just guessing that's why you want to fortify).

If you want to stay with sp "Spatial" objects, a work-around is to convert to sf, run it through ms_simplify(), and then back to "Spatial":

library(rgdal) library(sf) library(rmapshaper) library(broom)shp_sp <- readOGR(".", "BRMUE250GC_SIR", stringsAsFactors = FALSE, encoding = "UTF-8")shp_sf <- st_as_sf(shp_sp)shp2_sf <- ms_simplify(shp_sf)shp2_sp <- as(shp2_sf, "Spatial")shp_tidy <- tidy(shp2_sp)

I will look at why it is crashing, but this should work in the meantime...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ateucher/rmapshaper/issues/83#issuecomment-442322656, or mute the thread https://github.com/notifications/unsubscribe-auth/AjqJCTbEH_DGvmCS7MxHf69Zab90UYwnks5uzhv3gaJpZM4YzhsM .

fabiocs8 avatar Nov 28 '18 07:11 fabiocs8

My pleasure, thanks again for reporting this.

ateucher avatar Nov 28 '18 17:11 ateucher

Related perhaps. Also on version 0.4.1 my session aborts when simplifying the NTEM boundaries shape.

See an example here

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

fermumen avatar May 15 '19 09:05 fermumen

Thanks @fermm92 - it's due to the size of the spatial object and the memory limitations of V8 in which the javascript mapshaper engine is running. I will work on catching these limitations earlier so that it fails a little less dramatically.

You can use the sys = TRUE argument to use the system mapshaper, it worked well for me with this example (and quite quickly too). This does however require install node and the mapshaper module on your system: https://github.com/ateucher/rmapshaper#using-the-system-mapshaper

ateucher avatar May 15 '19 18:05 ateucher

Thanks for your answer @ateucher using sys = TRUE worked great!

fermumen avatar May 16 '19 08:05 fermumen

Excellent, glad it worked!

ateucher avatar May 16 '19 16:05 ateucher

This should be fixed by #118

ateucher avatar Feb 28 '22 20:02 ateucher