knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(sf) vector_filepath = system.file("shapes/world.gpkg", package = "spData") world = st_read(vector_filepath)
To demonstrate this, we will use a function to compare st_read()
with its sp equivalent, rgdal::readOGR()
:
bench_read = function(file, n) { m = microbenchmark(times = n, rgdal::readOGR(vector_filepath), st_read(vector_filepath) ) mean(m$time[1:n]) / mean(m$time[(n + 1):(n * 2)]) }
This function takes as arguments an input file (file
) and a number of times to run each command (n
) and returns how many times faster st_read()
is than readOGR()
.
Let's run the benchmark for the world.gpkg
file represented by the object vector_filepath
:
library(microbenchmark) read_world_gpkg = bench_read(file = vector_filepath, n = 5)
read_world_gpkg
The results demonstrate that sf was around r round(read_world_gpkg)
times faster than rgdal at reading-in the world countries vector.
The relative performance of st_read()
compared with other functions will vary depending on file format and the nature of the data.
To illustrate this point, we performed the same operation on a geojson file and found a greater speed saving:
vector_filepath_gj = system.file("shapes/cycle_hire_osm.geojson", package = "spData") read_lnd_geojson = bench_read(file = vector_filepath_gj, n = 5)
read_lnd_geojson
In this case sf was around r round(read_lnd_geojson)
times faster than rgdal.
library(rgdal) world_sp = as(world, "Spatial")
system.time(writeOGR(world_sp, dsn = "world_sp.geojson", layer = "world_sp.geojson", driver = "GeoJSON")) system.time(writeOGR(world_sp, dsn = ".", layer = "world_sp", driver = "ESRI Shapefile")) system.time(writeOGR(world_sp, dsn = "world_sp.gpkg", layer = "world_sp.gpkg", driver = "GPKG")) system.time(st_write(world, "world_sf.geojson", quiet = TRUE)) system.time(st_write(world, "world_sf.shp", quiet = TRUE)) system.time(st_write(world, "world_sf.gpkg", quiet = TRUE))
world_files = list.files(pattern = "world*") file.remove(world_files)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.