From ff6fd157afb7060482d0d4a20ba289870032297b Mon Sep 17 00:00:00 2001 From: geryan Date: Fri, 12 Jul 2024 10:07:54 +1000 Subject: [PATCH 1/5] make_mask --- R/{make_africa_mask.R => make_mask.R} | 45 ++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 8 deletions(-) rename R/{make_africa_mask.R => make_mask.R} (88%) diff --git a/R/make_africa_mask.R b/R/make_mask.R similarity index 88% rename from R/make_africa_mask.R rename to R/make_mask.R index be6ee51..214d4f4 100644 --- a/R/make_africa_mask.R +++ b/R/make_mask.R @@ -25,7 +25,7 @@ #' # or do both at once #' africa_mask_r <- make_africa_mask("africa_mask.tif") #' } -make_africa_mask <- function( +make_mask <- function( filename = NULL, type = c("raster", "vector"), res = c("high", "low"), @@ -53,13 +53,6 @@ make_africa_mask <- function( type <- match.arg(type) - # get list of African countries - if(is.null(countries)){ - countries <- sdmtools::global_regions %>% - dplyr::filter(continent == "Africa") %>% - dplyr::pull(iso3) - } - # make using MAP data # has issue with validity, st_make_valid 'fixes' this however...? #library(sf) @@ -131,6 +124,42 @@ make_africa_mask <- function( } +make_africa_mask <- function( + filename = NULL, + type = c("raster", "vector"), + res = c("high", "low") +){ + + # get list of African countries + african_countries <- sdmtools::global_regions %>% + dplyr::filter(continent == "Africa") %>% + dplyr::pull(iso3) + + make_mask( + filename = filename, + type = type, + res = res, + countries = african_countries + ) + +} + +make_vector_mask <- function( + filename = NULL, + res = c("high", "low"), + countries = NULL +){ + + make_mask( + filename = filename, + type = "vector", + res = res, + countries = countries + ) + +} + + # small example to illustrate validity issue with MAP shapefiles # sf::sf_use_s2(FALSE) From 3108513f4b2eac99bbc0adfcbb63b8fa30529ec5 Mon Sep 17 00:00:00 2001 From: geryan Date: Fri, 12 Jul 2024 10:35:39 +1000 Subject: [PATCH 2/5] make_mask works but make_vector_mask and make_africa_mask no longer do. Also problem with hard coded extent for rasters --- NAMESPACE | 2 +- R/make_mask.R | 38 +++++++++++++++++++++++----------- man/make_africa_mask.Rd | 43 --------------------------------------- man/make_mask.Rd | 45 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 56 deletions(-) delete mode 100644 man/make_africa_mask.Rd create mode 100644 man/make_mask.Rd diff --git a/NAMESPACE b/NAMESPACE index 3d958b3..1e7ce5a 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,7 @@ export(example_vector) export(extract_covariates) export(import_rasts) export(inside_mask) -export(make_africa_mask) +export(make_mask) export(make_mpp_list) export(mask_all) export(maskpointsdf) diff --git a/R/make_mask.R b/R/make_mask.R index 214d4f4..cf10eaf 100644 --- a/R/make_mask.R +++ b/R/make_mask.R @@ -1,6 +1,6 @@ -#' @title Make Africa Mask +#' @title Make Mask #' @description -#' Makes a `SpatRaster` or `SpatVector` mask layer of Africa, based on shapefiles for African nations from the `malaraAtlas` package. +#' Makes a `SpatRaster` or `SpatVector` mask layer of countries, based on shapefiles for from the `malaraAtlas` package. #' #' #' @param filename Character of file path and name if mask is to be written to disc. @@ -8,19 +8,30 @@ #' @param res Character `"high"` or `"low"`; corresponding to resolution of 0.008333333 or 0.04166667 decimal degrees #' @param countries Character of ISO3 country names. If `NULL` returns all countries in Africa. #' +#' @aliases make_africa_mask make_vector_mask +#' @usage +#' # standard usage +#' make_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) +#' +#' # `make_africa_mask` is intended for backward compatibility but is a simple alias for `make_mask` +#' make_africa_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) +#' +#' # `make_vector_mask` sets `type = "vector"` +#' make_mask(filename = NULL, res = c("high", "low"), countries = NULL) +#' #' @return `SpatRaster` or `SpatVector` in WGS 84 (EPSG:4326). #' @export #' #' @details -#' Raster layers creates with extent of `terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324)` +#' If `countries = NULL`, raster layers have extent of `terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324)` #' #' @examples #' \dontrun{ #' # Create an object in workspace -#' africa_mask_v <- make_africa_mask(type = "vector") +#' africa_mask_v <- make_mask(type = "vector") #' #' # Save to disk -#' make_africa_mask(filename = "africa_mask.tif", type = "raster") +#' make_mask(filename = "africa_mask.tif", type = "raster") #' #' # or do both at once #' africa_mask_r <- make_africa_mask("africa_mask.tif") @@ -51,6 +62,13 @@ make_mask <- function( } } + if(is.null(countries)){ + # get list of African countries + african_countries <- sdmtools::global_regions %>% + dplyr::filter(continent == "Africa") %>% + dplyr::pull(iso3) + } + type <- match.arg(type) # make using MAP data @@ -127,19 +145,15 @@ make_mask <- function( make_africa_mask <- function( filename = NULL, type = c("raster", "vector"), - res = c("high", "low") + res = c("high", "low"), + countries = NULL ){ - # get list of African countries - african_countries <- sdmtools::global_regions %>% - dplyr::filter(continent == "Africa") %>% - dplyr::pull(iso3) - make_mask( filename = filename, type = type, res = res, - countries = african_countries + countries = countries ) } diff --git a/man/make_africa_mask.Rd b/man/make_africa_mask.Rd deleted file mode 100644 index fff7e76..0000000 --- a/man/make_africa_mask.Rd +++ /dev/null @@ -1,43 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/make_africa_mask.R -\name{make_africa_mask} -\alias{make_africa_mask} -\title{Make Africa Mask} -\usage{ -make_africa_mask( - filename = NULL, - type = c("raster", "vector"), - res = c("high", "low"), - countries = NULL -) -} -\arguments{ -\item{filename}{Character of file path and name if mask is to be written to disc.} - -\item{type}{Character \code{raster} or \code{vector}; to return mask as either \code{SpatRaster} or \code{SpatVector}.} - -\item{res}{Character \code{"high"} or \code{"low"}; corresponding to resolution of 0.008333333 or 0.04166667 decimal degrees} - -\item{countries}{Character of ISO3 country names. If \code{NULL} returns all countries in Africa.} -} -\value{ -\code{SpatRaster} or \code{SpatVector} in WGS 84 (EPSG:4326). -} -\description{ -Makes a \code{SpatRaster} or \code{SpatVector} mask layer of Africa, based on shapefiles for African nations from the \code{malaraAtlas} package. -} -\details{ -Raster layers creates with extent of \code{terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324)} -} -\examples{ -\dontrun{ -# Create an object in workspace -africa_mask_v <- make_africa_mask(type = "vector") - -# Save to disk -make_africa_mask(filename = "africa_mask.tif", type = "raster") - -# or do both at once -africa_mask_r <- make_africa_mask("africa_mask.tif") -} -} diff --git a/man/make_mask.Rd b/man/make_mask.Rd new file mode 100644 index 0000000..fd1f475 --- /dev/null +++ b/man/make_mask.Rd @@ -0,0 +1,45 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/make_mask.R +\name{make_mask} +\alias{make_mask} +\alias{make_africa_mask} +\alias{make_vector_mask} +\title{Make Mask} +\usage{ +# standard usage +make_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) +# `make_africa_mask`` is intended for backward compatibility but is a simple alias for `make_mask` +make_africa_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) +# `make_vector_mask` sets `type = "vector"` +make_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) +} +\arguments{ +\item{filename}{Character of file path and name if mask is to be written to disc.} + +\item{type}{Character \code{raster} or \code{vector}; to return mask as either \code{SpatRaster} or \code{SpatVector}.} + +\item{res}{Character \code{"high"} or \code{"low"}; corresponding to resolution of 0.008333333 or 0.04166667 decimal degrees} + +\item{countries}{Character of ISO3 country names. If \code{NULL} returns all countries in Africa.} +} +\value{ +\code{SpatRaster} or \code{SpatVector} in WGS 84 (EPSG:4326). +} +\description{ +Makes a \code{SpatRaster} or \code{SpatVector} mask layer of countries, based on shapefiles for from the \code{malaraAtlas} package. +} +\details{ +If \code{countries = NULL}, raster layers have extent of \code{terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324)} +} +\examples{ +\dontrun{ +# Create an object in workspace +africa_mask_v <- make_mask(type = "vector") + +# Save to disk +make_mask(filename = "africa_mask.tif", type = "raster") + +# or do both at once +africa_mask_r <- make_africa_mask("africa_mask.tif") +} +} From b156fa1f179ece66756f267f9c48298812924e6f Mon Sep 17 00:00:00 2001 From: geryan Date: Fri, 20 Dec 2024 13:42:31 +1100 Subject: [PATCH 3/5] document std_rast --- man/std_rast.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/std_rast.Rd b/man/std_rast.Rd index 21bc0b5..7c1806a 100644 --- a/man/std_rast.Rd +++ b/man/std_rast.Rd @@ -4,7 +4,7 @@ \alias{std_rast} \title{Standardise raster} \usage{ -std_rast(x, reverse, filename = NULL, overwrite = TRUE) +std_rast(x, reverse = FALSE, filename = NULL, overwrite = TRUE) } \arguments{ \item{x}{\code{SpatRaster} to standardise} From dcfe4d69df1377d4e39f11a0a7d7fca40f6df6b8 Mon Sep 17 00:00:00 2001 From: geryan Date: Fri, 20 Dec 2024 13:43:23 +1100 Subject: [PATCH 4/5] deprecate make_africa_mask --- NAMESPACE | 2 ++ R/make_mask.R | 92 +++++++++++++++++++++++------------------------- man/make_mask.Rd | 42 +++++++++++++++------- 3 files changed, 75 insertions(+), 61 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 0bbd60b..754c0b3 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,8 +9,10 @@ export(example_vector) export(extract_covariates) export(import_rasts) export(inside_mask) +export(make_africa_mask) export(make_mask) export(make_mpp_list) +export(make_vector_mask) export(mask_all) export(mask_from_all) export(maskpointsdf) diff --git a/R/make_mask.R b/R/make_mask.R index cf10eaf..583fd72 100644 --- a/R/make_mask.R +++ b/R/make_mask.R @@ -1,30 +1,32 @@ #' @title Make Mask -#' @description -#' Makes a `SpatRaster` or `SpatVector` mask layer of countries, based on shapefiles for from the `malaraAtlas` package. +#' @description Makes a `SpatRaster` or `SpatVector` mask layer of countries, +#' based on shapefiles for from the `malaraAtlas` package. #' #' -#' @param filename Character of file path and name if mask is to be written to disc. -#' @param type Character `raster` or `vector`; to return mask as either `SpatRaster` or `SpatVector`. -#' @param res Character `"high"` or `"low"`; corresponding to resolution of 0.008333333 or 0.04166667 decimal degrees -#' @param countries Character of ISO3 country names. If `NULL` returns all countries in Africa. +#' @param filename Character of file path and name if mask is to be written to +#' disc. +#' @param type Character `raster` or `vector`; to return mask as either +#' `SpatRaster` or `SpatVector`. +#' @param res Character `"high"` or `"low"`; corresponding to resolution of +#' 0.008333333 or 0.04166667 decimal degrees +#' @param countries Character of ISO3 country names. If `NULL` returns all +#' countries in Africa. +#' @param overwrite logical #' #' @aliases make_africa_mask make_vector_mask -#' @usage -#' # standard usage -#' make_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) -#' -#' # `make_africa_mask` is intended for backward compatibility but is a simple alias for `make_mask` -#' make_africa_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) -#' -#' # `make_vector_mask` sets `type = "vector"` -#' make_mask(filename = NULL, res = c("high", "low"), countries = NULL) #' #' @return `SpatRaster` or `SpatVector` in WGS 84 (EPSG:4326). #' @export #' -#' @details -#' If `countries = NULL`, raster layers have extent of `terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324)` +#' @details # standard usage make_mask(filename = NULL, type = c("raster", +#' "vector"), res = c("high", "low"), countries = NULL) +#' +#' `make_africa_mask` is intended for backward compatibility but is a simple +#' alias for `make_mask` make_africa_mask(filename = NULL, type = c("raster", +#' "vector"), res = c("high", "low"), countries = NULL) #' +#' `make_vector_mask` sets `type = "vector"` make_mask(filename = NULL, res = +#' c("high", "low"), countries = NULL) #' @examples #' \dontrun{ #' # Create an object in workspace @@ -40,14 +42,15 @@ make_mask <- function( filename = NULL, type = c("raster", "vector"), res = c("high", "low"), - countries = NULL + countries = NULL, + overwrite = FALSE ){ if(!is.null(filename)){ - if(file.exists(filename)){ + if(file.exists(filename) & !overwrite){ warning(sprintf( - "%s exists\nUsing existing file\nto re-generate, delete existing %s", + "%s exists\nUsing existing file", filename, filename )) @@ -64,7 +67,7 @@ make_mask <- function( if(is.null(countries)){ # get list of African countries - african_countries <- sdmtools::global_regions %>% + countries <- sdmtools::global_regions %>% dplyr::filter(continent == "Africa") %>% dplyr::pull(iso3) } @@ -73,8 +76,6 @@ make_mask <- function( # make using MAP data # has issue with validity, st_make_valid 'fixes' this however...? - #library(sf) - #library(malariaAtlas) afvect <- malariaAtlas::getShp( ISO = countries @@ -99,31 +100,24 @@ make_mask <- function( } res <- match.arg(res) - if(res == "high"){ - - afrast <- terra::rast( - nlyrs=1, - crs = terra::crs("EPSG:4326"), - #extent = ext(-25.3583333333333, 63.5, -40.3666666666667, 37.5416666666667), - extent = terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324), # extent based on malariaAtlas::getRaster("Explorer__2020_Africa_ITN_Use") - resolution = c(0.008333333, 0.008333333), - vals = 1, - names = "mask" - ) |> - terra::mask(afvect) - - } else if (res == "low"){ - afrast <- terra::rast( - nlyrs=1, - crs = terra::crs("EPSG:4326"), - #extent = ext(-25.3583333333333, 63.5, -40.3666666666667, 37.5416666666667), - extent = terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324), - resolution = c(0.04166667, 0.04166667), - vals = 1, - names = "mask" - ) |> - terra::mask(afvect) - } + + resn <- ifelse( + res == "high", + 0.008333333, + 0.04166667 + ) + + xt <- terra::ext(afvect) + + afrast <- terra::rast( + nlyrs=1, + crs = terra::crs("EPSG:4326"), + extent = xt, + resolution = c(resn, resn), + vals = 1, + names = "mask" + ) |> + terra::mask(afvect) if(!is.null(filename)){ terra::writeRaster( @@ -142,6 +136,7 @@ make_mask <- function( } +#' @export make_africa_mask <- function( filename = NULL, type = c("raster", "vector"), @@ -158,6 +153,7 @@ make_africa_mask <- function( } +#' @export make_vector_mask <- function( filename = NULL, res = c("high", "low"), diff --git a/man/make_mask.Rd b/man/make_mask.Rd index fd1f475..d1fd4c7 100644 --- a/man/make_mask.Rd +++ b/man/make_mask.Rd @@ -6,31 +6,47 @@ \alias{make_vector_mask} \title{Make Mask} \usage{ -# standard usage -make_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) -# `make_africa_mask`` is intended for backward compatibility but is a simple alias for `make_mask` -make_africa_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) -# `make_vector_mask` sets `type = "vector"` -make_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL) +make_mask( + filename = NULL, + type = c("raster", "vector"), + res = c("high", "low"), + countries = NULL, + overwrite = FALSE +) } \arguments{ -\item{filename}{Character of file path and name if mask is to be written to disc.} +\item{filename}{Character of file path and name if mask is to be written to +disc.} -\item{type}{Character \code{raster} or \code{vector}; to return mask as either \code{SpatRaster} or \code{SpatVector}.} +\item{type}{Character \code{raster} or \code{vector}; to return mask as either +\code{SpatRaster} or \code{SpatVector}.} -\item{res}{Character \code{"high"} or \code{"low"}; corresponding to resolution of 0.008333333 or 0.04166667 decimal degrees} +\item{res}{Character \code{"high"} or \code{"low"}; corresponding to resolution of +0.008333333 or 0.04166667 decimal degrees} -\item{countries}{Character of ISO3 country names. If \code{NULL} returns all countries in Africa.} +\item{countries}{Character of ISO3 country names. If \code{NULL} returns all +countries in Africa.} + +\item{overwrite}{logical} } \value{ \code{SpatRaster} or \code{SpatVector} in WGS 84 (EPSG:4326). } \description{ -Makes a \code{SpatRaster} or \code{SpatVector} mask layer of countries, based on shapefiles for from the \code{malaraAtlas} package. +Makes a \code{SpatRaster} or \code{SpatVector} mask layer of countries, +based on shapefiles for from the \code{malaraAtlas} package. } -\details{ -If \code{countries = NULL}, raster layers have extent of \code{terra::ext(-18.0000019073486, 52.0416647593181, -34.9999987284343, 37.5416679382324)} +\section{standard usage make_mask(filename = NULL, type = c("raster",}{ +"vector"), res = c("high", "low"), countries = NULL) + +\code{make_africa_mask} is intended for backward compatibility but is a simple +alias for \code{make_mask} make_africa_mask(filename = NULL, type = c("raster", +"vector"), res = c("high", "low"), countries = NULL) + +\code{make_vector_mask} sets \code{type = "vector"} make_mask(filename = NULL, res = +c("high", "low"), countries = NULL) } + \examples{ \dontrun{ # Create an object in workspace From 0583fbc475e2f0a23c1e62f6121376c9a34396a5 Mon Sep 17 00:00:00 2001 From: geryan Date: Fri, 20 Dec 2024 13:47:32 +1100 Subject: [PATCH 5/5] correct documentation for `make_mask` --- R/make_mask.R | 15 ++++++++------- man/make_mask.Rd | 9 ++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/R/make_mask.R b/R/make_mask.R index 583fd72..a5542b9 100644 --- a/R/make_mask.R +++ b/R/make_mask.R @@ -1,6 +1,6 @@ #' @title Make Mask #' @description Makes a `SpatRaster` or `SpatVector` mask layer of countries, -#' based on shapefiles for from the `malaraAtlas` package. +#' based on shapefiles for from the `malaraAtlas` package. #' #' #' @param filename Character of file path and name if mask is to be written to @@ -19,14 +19,15 @@ #' @export #' #' @details # standard usage make_mask(filename = NULL, type = c("raster", -#' "vector"), res = c("high", "low"), countries = NULL) +#' "vector"), res = c("high", "low"), countries = NULL) #' -#' `make_africa_mask` is intended for backward compatibility but is a simple -#' alias for `make_mask` make_africa_mask(filename = NULL, type = c("raster", -#' "vector"), res = c("high", "low"), countries = NULL) +#' `make_africa_mask` is deprecated, and is intended for backward +#' compatibility. It is a simple alias for `make_mask`: +#' `make_africa_mask(filename = NULL, type = c("raster", "vector"), res = +#' c("high", "low"), countries = NULL)` #' -#' `make_vector_mask` sets `type = "vector"` make_mask(filename = NULL, res = -#' c("high", "low"), countries = NULL) +#' `make_vector_mask` sets `type = "vector"`: `make_mask(filename = NULL, type +#' = "vector", res =' c("high", "low"), countries = NULL)` #' @examples #' \dontrun{ #' # Create an object in workspace diff --git a/man/make_mask.Rd b/man/make_mask.Rd index d1fd4c7..15b038a 100644 --- a/man/make_mask.Rd +++ b/man/make_mask.Rd @@ -39,12 +39,11 @@ based on shapefiles for from the \code{malaraAtlas} package. \section{standard usage make_mask(filename = NULL, type = c("raster",}{ "vector"), res = c("high", "low"), countries = NULL) -\code{make_africa_mask} is intended for backward compatibility but is a simple -alias for \code{make_mask} make_africa_mask(filename = NULL, type = c("raster", -"vector"), res = c("high", "low"), countries = NULL) +\code{make_africa_mask} is deprecated, and is intended for backward +compatibility. It is a simple alias for \code{make_mask}: +\code{make_africa_mask(filename = NULL, type = c("raster", "vector"), res = c("high", "low"), countries = NULL)} -\code{make_vector_mask} sets \code{type = "vector"} make_mask(filename = NULL, res = -c("high", "low"), countries = NULL) +\code{make_vector_mask} sets \code{type = "vector"}: \verb{make_mask(filename = NULL, type = "vector", res =' c("high", "low"), countries = NULL)} } \examples{