rmapshaper icon indicating copy to clipboard operation
rmapshaper copied to clipboard

New function — ms_union

Open atsyplenkov opened this issue 2 years ago • 5 comments

Hi guys,

whenever you have a moment, could you please take a look at this? I propose to add ms_union function family which is a wrapper to -union command.

Cheers

atsyplenkov avatar Dec 15 '23 01:12 atsyplenkov

Thanks for this @atsyplenkov. Does this have very different behaviour to sf::st_union()?

ateucher avatar Jan 08 '24 18:01 ateucher

Hi @ateucher! As far as I understand, not really. However, it does provide a significant increase in speed. See a quick reprex below.

UPD: ms_union is even faster than terra::union

library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(rmapshaper)
library(terra)

file <- system.file("gpkg/nc.gpkg", package = "sf")
nc_sf <- read_sf(file)

nc_sf_buffer <- 
  st_buffer(nc_sf, 10^4)

# Convert to terra objects
nc_vect <- vect(nc_sf)

nc_buffer <- vect(nc_sf_buffer)

microbenchmark::microbenchmark(
  st_union = {
    
    st_union(nc_sf, nc_sf_buffer)
    
  },
  ms_union = {
    
    ms_union(nc_sf, nc_sf_buffer)
    
  },
  union = {
    
    terra::union(nc_vect, nc_buffer)
    
  },  
  times = 10L
)

#> Unit: milliseconds
#>      expr        min        lq       mean     median         uq        max
#>  st_union 16926.3117 17169.587 17528.1777 17483.3264 17780.6008 18183.9126
#>  ms_union   420.9673   440.738   460.7762   452.6254   477.9516   518.3586
#>  union 3465.1852 3492.5957 3562.6814 3537.1537 3599.4537 3726.8144
#>  neval cld
#>     10  a 
#>     10   b
#>     10   c

Created on 2024-01-09 with reprex v2.0.2

atsyplenkov avatar Jan 08 '24 20:01 atsyplenkov

That's pretty good! Thanks @atsyplenkov, I will probably take this PR, but I won't have time to work on the package for a little while, so I will look at it in detail then.

ateucher avatar Jan 23 '24 17:01 ateucher

Hey, any update on this PR?

atsyplenkov avatar Dec 19 '24 04:12 atsyplenkov

Thanks for your patience @atsyplenkov - I will try and look at this in the next few weeks

ateucher avatar Jan 30 '25 20:01 ateucher