Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions R/Spatial_Limit.r
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
31 changes: 26 additions & 5 deletions R/Spatial_Reproject.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
16 changes: 13 additions & 3 deletions data-raw/DATASET.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Binary file modified inst/extdata/KiN_rast.nc
Binary file not shown.
23 changes: 20 additions & 3 deletions man/Spatial_Limit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions man/Spatial_Reproject.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.