Skip to content

Instantly share code, notes, and snippets.

@paleolimbot
Created July 12, 2020 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paleolimbot/38e7442a4f97126491e2520a163f6bc3 to your computer and use it in GitHub Desktop.
Save paleolimbot/38e7442a4f97126491e2520a163f6bc3 to your computer and use it in GitHub Desktop.
library(sf)
library(geos)
states <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_USA_1_sf.rds"))
counties <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_USA_2_sf.rds"))
states_sfc <- states$geometry %>% st_set_crs(NA)
states_wkb <- st_as_binary(states_sfc)
states_geos <- geos_read_wkb(states_wkb)
counties_sfc <- counties$geometry %>% st_set_crs(NA)
counties_wkb <- st_as_binary(counties_sfc)
counties_geos <- geos_read_wkb(counties_wkb)
vt_sfc <- states_sfc[states$NAME_1 == "Vermont"]
vt_wkb <- states_wkb[states$NAME_1 == "Vermont"]
vt_geos <- states_geos[states$NAME_1 == "Vermont"]
add_sfc <- counties_sfc[counties$NAME_1 == "Vermont" & counties$NAME_2 == "Addison"]
add_wkb <- st_as_binary(add_sfc)
add_geos <- geos_read_wkb(add_wkb)
bench::mark(
st_intersects(states_sfc, vt_sfc),
geos_intersects(states_geos, vt_geos),
min_time = 10,
check = FALSE
)
bench::mark(
st_intersects(counties_sfc, add_sfc),
geos_intersects(counties_geos, add_geos),
min_time = 10,
check = FALSE
)
@paleolimbot
Copy link
Author

paleolimbot commented Jul 12, 2020

library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(geos) # remotes::install("paleolimbot/geos")

states <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_USA_1_sf.rds"))
counties <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_USA_2_sf.rds"))

states_sfc <- states$geometry %>% st_set_crs(NA)
states_wkb <- st_as_binary(states_sfc)
states_geos <- geos_read_wkb(states_wkb)

counties_sfc <- counties$geometry %>% st_set_crs(NA)
counties_wkb <- st_as_binary(counties_sfc)
counties_geos <- geos_read_wkb(counties_wkb)

vt_sfc <- states_sfc[states$NAME_1 == "Vermont"]
vt_wkb <- states_wkb[states$NAME_1 == "Vermont"]
vt_geos <- states_geos[states$NAME_1 == "Vermont"]

add_sfc <- counties_sfc[counties$NAME_1 == "Vermont" & counties$NAME_2 == "Addison"]
add_wkb <- st_as_binary(add_sfc)
add_geos <- geos_read_wkb(add_wkb)

bench::mark(
  st_intersects(states_sfc, vt_sfc),
  geos_intersects(states_geos, vt_geos),
  min_time = 10,
  check = FALSE
)
#> # A tibble: 2 x 6
#>   expression                                min  median `itr/sec` mem_alloc
#>   <bch:expr>                            <bch:t> <bch:t>     <dbl> <bch:byt>
#> 1 st_intersects(states_sfc, vt_sfc)     531.2ms 535.2ms      1.87      66MB
#> 2 geos_intersects(states_geos, vt_geos)  43.7ms  45.3ms     21.7     58.4KB
#> # … with 1 more variable: `gc/sec` <dbl>

bench::mark(
  st_intersects(counties_sfc, add_sfc),
  geos_intersects(counties_geos, add_geos),
  min_time = 10,
  check = FALSE
)
#> # A tibble: 2 x 6
#>   expression                                    min   median `itr/sec` mem_alloc
#>   <bch:expr>                               <bch:tm> <bch:tm>     <dbl> <bch:byt>
#> 1 st_intersects(counties_sfc, add_sfc)     482.97ms 492.65ms      2.01    78.5MB
#> 2 geos_intersects(counties_geos, add_geos)   2.37ms   2.79ms    354.     160.2KB
#> # … with 1 more variable: `gc/sec` <dbl>

Created on 2020-07-12 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment