
Hand off spatial objects to tmap, leaflet and friends
geo-map-interop.Rdtidymapr keeps mapping in ggplot via plot_sites(), plot_geo() and
plot_topo(), but never blocks tmap, leaflet or other spatial
viewers. These helpers return plain sf and
terra objects (with a CRS) so
tmap::tm_shape(), leaflet::addTiles(), leaflet::addCircleMarkers()
and leaflet::addRasterImage() work without extra wrangling. Neither
tmap nor leaflet is a hard Import; examples are wrapped in
requireNamespace().
Usage
as_sf(x, ...)
# Default S3 method
as_sf(x, lon = lon, lat = lat, crs = 4326, ...)
# S3 method for class 'sf'
as_sf(x, crs = NULL, ...)
# S3 method for class 'tmr_geo_sf'
as_sf(x, ...)
# S3 method for class 'tmr_sdm'
as_sf(x, ...)
as_spat_raster(x, ...)
# Default S3 method
as_spat_raster(x, ...)
# S3 method for class 'SpatRaster'
as_spat_raster(x, crs = NULL, ...)
# S3 method for class 'data.frame'
as_spat_raster(x, lon = lon, lat = lat, value = NULL, crs = "EPSG:4326", ...)
# S3 method for class 'tmr_sdm'
as_spat_raster(x, ...)Arguments
- x
A data frame with coordinates, an
sfobject, a SpatRaster, atmr_sdmprediction tibble, or another supported spatial result.- ...
Passed to
geo_as_sf()or ignored.- lon, lat
Coordinate columns when
xis a data frame.- crs
Target CRS (default EPSG:4326).
- value
Column to place in a single-layer SpatRaster when converting a point table with a numeric attribute (optional).
Value
as_sf() returns an sf object (subclass tmr_geo_sf when built
from coordinates). as_spat_raster() returns a terra::SpatRaster
with a CRS.
Examples
sites <- geo_example_sites()
sites <- sites[!is.na(sites$lon), ]
pts <- as_sf(sites)
inherits(pts, "sf")
#> [1] TRUE
sf::st_crs(pts)$epsg
#> [1] 4326
# Optional: same sites in tmap (skipped when tmap is not installed)
# tmap::tm_shape(as_sf(sites)) + tmap::tm_dots(col = "elevation")
# Optional: leaflet (skipped when leaflet is not installed)
# leaflet::leaflet(as_sf(sites)) |>
# leaflet::addTiles() |>
# leaflet::addCircleMarkers()