c4h_convert
Description
Convert s2dv_cube objects to terra, stars, data.frame, and sf.
c4h_convert(data, output_format, crs = NULL, drop = FALSE)Arguments
data: The object to transform.output_format: A string specifying the desired output format. Valid options are ‘terra’ (SpatRaster), ‘stars’, ‘data.frame’ or ‘sf’ (polygons).crs: Coordinate reference system to use for output_formats ‘terra’ and ‘sf’. The default is NULL, in which case the CRS will be taken from the input data if available, or assumed to be WGS 84 (EPSG:4326) for gridded data.drop: Logical. If TRUE, dimensions with only one value will be dropped when converting to the desired format. Default is FALSE.
Details
The function only allows to convert into the following classes depending on the data format.
- Gridded data: ‘terra’ SpatRaster, ‘stars’, ‘sf’ polygons, or ‘data.frame’.
- Areal data: ‘sf’ polygons or ‘data.frame’.
- Point data: ‘data.frame’.
Returns
An R object of type terra SpatRaster, stars, data.frame or sf polygons.
Examples
# Load example forecast data
fcst_path <- system.file("extdata", "forecast", package = "clim4health")
fcst <- c4h_load(fcst_path, variable = "t2m", leadtime_month = 1, ext = 'nc')
############################
### Convert gridded data
## to terra
out <- c4h_convert(fcst, "terra", crs = "EPSG:4326")
## to data.frame
out <- c4h_convert(fcst, "data.frame")
## to sf
out <- c4h_convert(fcst, "sf", crs = "EPSG:4326")
#############################
##### load polygon data #####
library(sf)
# Region municipalities
munip_path <- system.file("extdata", "areas", "munip_vallecauca.gpkg",
package = "clim4health")
munip <- read_sf(munip_path)
munip <- munip[1:2, ]
fcst_aggr <- c4h_space(fcst, munip)
### Convert polygon data
## to data.frame
out <- c4h_convert(fcst_aggr, "data.frame")
## to sf
out <- c4h_convert(fcst_aggr, "sf", crs = "EPSG:4326")
#############################
##### load station data #####
station_path <- system.file("extdata", "stations", "temp_vallecauca.csv",
package = "clim4health")
stations <- c4h_load(station_path, variable = "temp_mean", ext = 'csv')
### Convert location data
## to data.frame
out <- c4h_convert(stations, "data.frame")