Skip to content

Commit

Permalink
create function to process spatial data
Browse files Browse the repository at this point in the history
  • Loading branch information
Marques committed Dec 11, 2023
1 parent b8a1c49 commit 734c7f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 152 deletions.
46 changes: 18 additions & 28 deletions R/process_spatial_covariates.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
#' Project and save imperviousness data to a new crs
#' (used for projection to EPSG:5070 previously to prediction grid creation
#' Aggregate raster and store it in a new file (useful for dem)
#'
#' @param crs_dest A character with the crs of destination
#' @param crs_name A character for the crs without weird characters
#' to concat to file name
project_imp <- function(crs_dest, crs_name) {
# imperviousness is by default in wgs84 (epsg:4326)
imp <- terra::rast("../input/NC_imperviousness_2019.tif")
if (!terra::same.crs(terra::crs(imp), crs_dest)) {
imp_proj <- terra::project(imp, crs_dest)
terra::writeRaster(imp_proj,
filename = paste0(
"../input/",
"NC_imperviousness_2019_",
crs_name,
".tif"
),
#' @param in_filepath A character path to data file with high resolution
#' @param out_filepath A character path to the folder where the aggregate file
#' will be stored
agg_rast <- function(in_filepath, out_filepath, agg_fact = 30) {
raw <- terra::rast(in_filepath)
agg <- terra::aggregate(raw, fact = agg_fact, fun = "median") %>%
terra::writeRaster(
filename = paste0(out_filepath),
overwrite = TRUE
)
}
return(agg)
}

#' Aggregate digital elevation model (~1m to ~30m)

#' Subset a polygon area from a SpatRaster or a SpatVector
#'
#' @param dem_path A path to dem file with high resolution
agg_dem <- function(dem_path = "../input/NC-DEM.tif") {
dem <- terra::rast(dem_path)
dem_agg <- terra::aggregate(dem, fact = 30, fun = "median")
terra::writeRaster(dem_agg,
filename = "../input/NC-DEM-agg.tif",
overwrite = TRUE
)
return(dem_agg)
#' @param sp a SpatRaster or a SpatVector
#' @param poly a SpatVector with polygon geometry
subset_area <- function(sp, poly) {
poly_proj <- terra::project(poly, terra::crs(sp))
crop_sp <- terra::crop(sp, poly_proj)
return(crop_sp)
}
4 changes: 2 additions & 2 deletions vignettes/create_testdata.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ rtp_subset <- function(sp) {
type = "polygons",
crs = "EPSG:4326")
ext_proj <- terra::project(ext, terra::crs(sp))
crop_raster <- terra::crop(sp, ext_proj)
return(crop_raster)
crop_sp <- terra::crop(sp, ext_proj)
return(crop_sp)
}
```

Expand Down
57 changes: 0 additions & 57 deletions vignettes/open-format-forest-height.Rmd

This file was deleted.

65 changes: 0 additions & 65 deletions vignettes/open-format-nlcd.Rmd

This file was deleted.

0 comments on commit 734c7f3

Please sign in to comment.