diff --git a/R/Spatial_Limit.r b/R/Spatial_Limit.r index 3c26e93..ff3aea1 100644 --- a/R/Spatial_Limit.r +++ b/R/Spatial_Limit.r @@ -15,13 +15,58 @@ #' #' @author Erik Kusch #' +#' @seealso \code{\link{Spatial_Reproject}}. +#' #' @examples -#' Data_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub")) +#' # single-layer exaples +#' ## limit by sf object +#' KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] #' data(Jotunheimen_sf) -#' Data_rast <- Spatial_Reproject(Data_rast, Jotunheimen_sf) -#' Spatial_Limit(Data_rast, Jotunheimen_sf) +#' # Kin_Limited <- Spatial_Limit(KiN_rast, Jotunheimen_sf) # this would throw an error due to different projections of the spatRaster and the sf object +#' KiN_rast <- Spatial_Reproject(KiN_rast, Jotunheimen_sf) +#' KiN_Limited <- Spatial_Limit(spatRaster = KiN_rast, shape = Jotunheimen_sf) +#' # Viz_SpatRast(KiN_Limited) +#' +#' ## limit by SpatExtent +#' KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] +#' KiN_Limited <- Spatial_Limit(spatRaster = KiN_rast, shape = terra::ext(-7e4, 3e5, 6.5e6, 7e6)) +#' # Viz_SpatRast(KiN_Limited) +#' +#' # multi-layer exaple +#' KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub")) +#' KiN_Limited <- Spatial_Limit(spatRaster = KiN_rast, shape = terra::ext(-7e4, 3e5, 6.5e6, 7e6)) +#' # Viz_SpatRast(KiN_Limited, panelColumns = 5) #' @export Spatial_Limit <- function(spatRaster, shape, verbose = TRUE) { + ## check that shape is specified correctly + class_name <- class(shape)[1] + if (!(class_name %in% c("sf", "SpatExtent"))) { + stop("You have misspecified your shape argument. It must be an sf object or SpatExtent.") + } + + ## check that cropping/masking can even happen + if (class_name == "sf") { + if (!identical(as.numeric(terra::crs(spatRaster, describe = TRUE)$code), as.numeric(st_crs(shape)$epsg))) { + stop("The CRS EPSG Code of the spatRast and shape are not identical. You need to reproject one of these objects first. You can do so with the Spatial_Reproject function.") + } + } + if (class_name == "SpatExtent") { + if ( + any( + c( + terra::ext(spatRaster)[c(1, 3)] > shape[c(1, 3)], + terra::ext(spatRaster)[c(2, 4)] < shape[c(2, 4)] + ) + ) + ) { + stop( + "The SpatExtent you have specified in the shape argument extends beyond the limits of the SpatRaster provided via the spatRaster argument. The extent of this object (and maximum extent for Spatial_limit operations on it is:\n", + paste0("xmin: ", terra::ext(spatRaster)[1], ", xmax: ", terra::ext(spatRaster)[2], ", ymin: ", terra::ext(spatRaster)[3], ", ymax: ", terra::ext(spatRaster)[4]), + "\nPlease provide a SpatExtent that is within these limits." + ) + } + } + ## progress bar pb <- Helper_Progress(iterLength = nlyr(spatRaster), text = "Cropping/Masking Layers") diff --git a/R/Spatial_Reproject.r b/R/Spatial_Reproject.r index 051e255..c0bd224 100644 --- a/R/Spatial_Reproject.r +++ b/R/Spatial_Reproject.r @@ -18,13 +18,28 @@ #' @author Erik Kusch #' #' @examples -#' Data_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] -#' ## reprojecting raster +#' ## reprojecting raster to match specific EPSG +#' KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] # this data is stored in K*10 +#' SpatRast_repro <- Spatial_Reproject(KiN_rast, 3995) +#' # Viz_SpatRast(SpatRast_repro) +#' +#' ## reprojecting raster to match sf +#' KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] #' data(Jotunheimen_sf) -#' Spatial_Reproject(Data_rast, Jotunheimen_sf) -#' ## reprojecting sf +#' SpatRast_repro <- Spatial_Reproject(KiN_rast, Jotunheimen_sf) +#' # Viz_SpatRast(SpatRast_repro, sfObj = Jotunheimen_sf, sfColour = "white") +#' +#' ## reprojecting raster to match raster +#' KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] +#' Nora3_rast <- terra::rast(system.file("extdata", "Nora3_rast.nc", package = "ClimHub"))[[1]] +#' SpatRast_repro <- Spatial_Reproject(KiN_rast, Nora3_rast) +#' # Viz_SpatRast(SpatRast_repro) +#' +#' ## reprojecting sf to match raster +#' KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] #' data(Nor2K_sf) -#' Spatial_Reproject(Nor2K_sf, Data_rast) +#' Nor2K_repro <- Spatial_Reproject(Nor2K_sf, KiN_rast) +#' # Viz_SpatRast(KiN_rast, sfObj = Nor2K_repro, sfColour = "white", sfFill = "white", sfSize = 2) #' @export Spatial_Reproject <- function(projFrom, projTo, rasterResample = TRUE) { ## classes of arguments @@ -69,6 +84,12 @@ Spatial_Reproject <- function(projFrom, projTo, rasterResample = TRUE) { Reprojected <- sf::st_transform(projFrom, projTo) } + ## ensure correct attributes + if (package_name[[1]] == "terra") { + warning("SpatRaster data was reprojected using terra functionality which may lead to shifts in decimal points. Please verify results carefully.") + Reprojected <- Helper_AttrOverride(projFrom, Reprojected) + } + ## return return(Reprojected) } diff --git a/data-raw/DATASET.R b/data-raw/DATASET.R index 1d1ca82..0645963 100644 --- a/data-raw/DATASET.R +++ b/data-raw/DATASET.R @@ -24,10 +24,20 @@ KiN_rast <- Access_KlimaiNorge2100( scenario = "rcp85", cores = 1, fileName = file.path(getwd(), "inst/extdata", "KiN_rast.nc"), - compression = 9, - removeTemporary = TRUE + removeTemporary = FALSE, + writeFile = FALSE +) +## data is stored multiplied by 10 so needs to be adjusted here +KiN_rast <- NC_Write( + spatRaster = KiN_rast / 10, + fileName = file.path(getwd(), "inst/extdata", "KiN_rast.nc"), + varName = unique(terra::varnames(KiN_rast)), + longName = unique(terra::longnames(KiN_rast)), + unit = unique(terra::units(KiN_rast)), + meta = setNames(terra::metags(KiN_rast)[, 2], terra::metags(KiN_rast)[, 1]), + compression = 9 ) -# usethis::use_data(KiN_rast) +unlink(list.files(pattern = "TEMP_")) # Jotunheimen boundary as spatialfeatureobject ------------------ Jotunheimen_sf <- sf::st_read("data-raw/Shape/Shape-polygon.shp") diff --git a/inst/extdata/KiN_rast.nc b/inst/extdata/KiN_rast.nc index 63e0e53..f143713 100644 Binary files a/inst/extdata/KiN_rast.nc and b/inst/extdata/KiN_rast.nc differ diff --git a/man/Spatial_Limit.Rd b/man/Spatial_Limit.Rd index 6e8d441..93295a7 100644 --- a/man/Spatial_Limit.Rd +++ b/man/Spatial_Limit.Rd @@ -20,10 +20,27 @@ A SpatRaster. Cropped and masking the original SpatRaster (\code{spatRaster}) using supplied SpatExtent or shapefile (\code{shape}) and retaining all pixels which are even just partially covered. } \examples{ -Data_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub")) +# single-layer exaples +## limit by sf object +KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] data(Jotunheimen_sf) -Data_rast <- Spatial_Reproject(Data_rast, Jotunheimen_sf) -Spatial_Limit(Data_rast, Jotunheimen_sf) +# Kin_Limited <- Spatial_Limit(KiN_rast, Jotunheimen_sf) # this would throw an error due to different projections of the spatRaster and the sf object +KiN_rast <- Spatial_Reproject(KiN_rast, Jotunheimen_sf) +KiN_Limited <- Spatial_Limit(spatRaster = KiN_rast, shape = Jotunheimen_sf) +# Viz_SpatRast(KiN_Limited) + +## limit by SpatExtent +KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] +KiN_Limited <- Spatial_Limit(spatRaster = KiN_rast, shape = terra::ext(-7e4, 3e5, 6.5e6, 7e6)) +# Viz_SpatRast(KiN_Limited) + +# multi-layer exaple +KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub")) +KiN_Limited <- Spatial_Limit(spatRaster = KiN_rast, shape = terra::ext(-7e4, 3e5, 6.5e6, 7e6)) +# Viz_SpatRast(KiN_Limited, panelColumns = 5) +} +\seealso{ +\code{\link{Spatial_Reproject}}. } \author{ Erik Kusch diff --git a/man/Spatial_Reproject.Rd b/man/Spatial_Reproject.Rd index f7c4c32..d062a7a 100644 --- a/man/Spatial_Reproject.Rd +++ b/man/Spatial_Reproject.Rd @@ -20,13 +20,28 @@ A SpatRaster or SF object reprojected to the same CRS as in ProjTo. Reprojecting one spatial data product to match another. } \examples{ -Data_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] -## reprojecting raster +## reprojecting raster to match specific EPSG +KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] # this data is stored in K*10 +SpatRast_repro <- Spatial_Reproject(KiN_rast, 3995) +# Viz_SpatRast(SpatRast_repro) + +## reprojecting raster to match sf +KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] data(Jotunheimen_sf) -Spatial_Reproject(Data_rast, Jotunheimen_sf) -## reprojecting sf +SpatRast_repro <- Spatial_Reproject(KiN_rast, Jotunheimen_sf) +# Viz_SpatRast(SpatRast_repro, sfObj = Jotunheimen_sf, sfColour = "white") + +## reprojecting raster to match raster +KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] +Nora3_rast <- terra::rast(system.file("extdata", "Nora3_rast.nc", package = "ClimHub"))[[1]] +SpatRast_repro <- Spatial_Reproject(KiN_rast, Nora3_rast) +# Viz_SpatRast(SpatRast_repro) + +## reprojecting sf to match raster +KiN_rast <- terra::rast(system.file("extdata", "KiN_rast.nc", package = "ClimHub"))[[1]] data(Nor2K_sf) -Spatial_Reproject(Nor2K_sf, Data_rast) +Nor2K_repro <- Spatial_Reproject(Nor2K_sf, KiN_rast) +# Viz_SpatRast(KiN_rast, sfObj = Nor2K_repro, sfColour = "white", sfFill = "white", sfSize = 2) } \author{ Erik Kusch