Interoperability
This chapter provides practical roundtrip patterns between WbW-R and R spatial tooling.
Interoperability should be treated as deliberate boundary crossing. Each conversion step can affect metadata fidelity, CRS representation, numeric precision, and schema details. The examples here emphasize explicit handoff points and validation checkpoints so multi-package workflows remain defensible.
Copy-Boundary Model
- Array exchange via
to_array()andwbw_array_to_raster(...)is an explicit in-memory boundary. - terra/stars/sf workflows are typically file or object conversion boundaries.
- Always validate metadata after roundtrip.
terra Roundtrip
Use this when your raster workflow depends on terra-native functions.
library(whiteboxworkflows)
library(terra)
r <- wbw_read_raster('dem.tif')
terra_r <- r$to_terra()
# terra-side operation
terra_r2 <- terra::focal(terra_r, w = 3, fun = mean, na.rm = TRUE)
terra::writeRaster(terra_r2, 'dem_terra_smoothed.tif', overwrite = TRUE)
r_back <- wbw_read_raster('dem_terra_smoothed.tif')
print(r_back$metadata())
stars Roundtrip
Use this for labeled-array operations and grid math in stars workflows.
library(whiteboxworkflows)
library(stars)
r <- wbw_read_raster('dem.tif')
st <- r$to_stars()
# stars-side operation
st2 <- st * 1.05
st_as_stars <- st_as_stars(st2)
write_stars(st_as_stars, 'dem_stars_scaled.tif', driver = 'GTiff')
r_back <- wbw_read_raster('dem_stars_scaled.tif')
print(r_back$metadata())
sf Roundtrip (Vector)
Use this when vector editing and validity checks are more convenient in sf.
library(whiteboxworkflows)
library(sf)
v <- wbw_read_vector('roads.gpkg')
sf_obj <- v$to_sf()
# sf-side edit
sf_obj$len_m <- as.numeric(st_length(sf_obj))
sf_obj <- sf_obj[sf_obj$len_m > 20, ]
st_write(sf_obj, 'roads_sf_filtered.gpkg', delete_dsn = TRUE, quiet = TRUE)
v_back <- wbw_read_vector('roads_sf_filtered.gpkg')
print(v_back$schema())
Validation Checklist
- Re-check CRS and bounds after conversion.
- Re-check schema and representative attributes for vector flows.
- Prefer stable interchange formats (
.tif,.gpkg) for routine roundtrips.