ateucher / rmapshaper

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

New function — ms_union #170

Open atsyplenkov opened 9 months ago

atsyplenkov commented 9 months ago

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

ateucher commented 8 months ago

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

atsyplenkov commented 8 months ago

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

ateucher commented 7 months ago

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.