saveContours: Generate and Export Bathymetric Contours

View source: R/saveContours.R

saveContoursR Documentation

Generate and Export Bathymetric Contours

Description

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).

Usage

saveContours(
  dem,
  by = NULL,
  levels = NULL,
  file_type = "gpx",
  file_name = "contours"
)

Arguments

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".

Details

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).

Value

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.

Author(s)

Tristan Blechinger, Department of Zoology & Physiology, University of Wyoming

Examples


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"))
}

rLakeHabitat documentation built on July 30, 2026, 5:11 p.m.