| saveContours | R Documentation |
Generate depth contours from an interpolated DEM and save them to a file format usable outside R - a GPS unit or chartplotter (.gpx), Google Earth or similar (.kml), or standard GIS formats (.shp, .gpkg).
saveContours(
dem,
by = NULL,
levels = NULL,
file_type = "gpx",
file_name = "contours"
)
dem |
a SpatRaster of interpolated bathymetry (e.g. from interpBathy()). If 'dem' has multiple layers (e.g. the depth/error output of OK or UK), the layer named 'depth' is used if present, otherwise the first layer. |
by |
numeric value giving a regular contour interval, e.g. by = 5 contours every 5 depth units starting at 5 (the 0 contour is skipped, since it's just the shoreline itself). Exactly one of 'by' or 'levels' must be specified. |
levels |
numeric vector of specific depth values to contour, e.g. c(1, 5, 10, 20). Exactly one of 'by' or 'levels' must be specified. |
file_type |
character giving the output format: "gpx", "kml", "shp", or "gpkg". Default = "gpx". |
file_name |
character giving the output file name, with or without a path. Default = "contours". |
GPX and KML both require geographic (WGS84 lon/lat) coordinates - if 'dem' is in a projected CRS, the contours are automatically reprojected to EPSG:4326 before being written (.shp/.gpkg outputs keep 'dem's original CRS).
an sf object of the generated contour lines (with a 'depth' column), invisibly saved to 'file_name' as a side effect. Returned regardless of 'file_type' so you can plot or inspect it without re-reading the saved file.
Tristan Blechinger, Department of Zoology & Physiology, University of Wyoming
if (requireNamespace("sf", quietly = TRUE)) {
outline <- terra::vect(system.file("extdata", "example_outline.shp", package = 'rLakeHabitat'))
data <- read.csv(system.file("extdata", "example_depths.csv", package = 'rLakeHabitat'))
dem <- interpBathy(outline, data, "x", "y", "z", zeros = FALSE, separation = 10,
res = 10, method = "IDW", nmax = 4)
saveContours(dem, by = 2, file_type = "gpx", file_name = tempfile("contours"))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.