diff --git a/R/ShapeToMask.R b/R/ShapeToMask.R index 248775eec5371271d2e997867145ae512fae951b..350e38bbb793502c98ed3a1eae3234ed41a0e496 100644 --- a/R/ShapeToMask.R +++ b/R/ShapeToMask.R @@ -22,27 +22,30 @@ #' data. Either (1) a netCDF file or (2) a list of lon and lat to provide the #' reference grid points. It is NULL by default. #'@param shp_system A character string containing the Shapefile System Database -#' name. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. It is set to -#' 'NUTS' by default. +#' Name used to subset the shapefile into regions by using parameters 'reg_ids' +#' or 'reg_names'. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. When it +#' is used, you must specify either 'reg_ids' or 'reg_names'; if you don't need +#' to subset different regions, set it to NULL. It is set to "NUTS" by default +#' (optional). #'@param shp_col_name_ids A character string indicating the column name of the -#' column in where the specified 'reg_ids' will be taken. +#' column in where the specified 'reg_ids' will be taken (optional). #'@param reg_ids A character string indicating the unique ID in shapefile. -#' It is NULL by default. +#' It is NULL by default (optional). #'@param reg_names A named list of character string vectors indicating the #' country and the region name. The name of the list stands for the country #' name code and the vector character strings indicate the region name for -#' each country. It is NULL by default. +#' each country. It is NULL by default (optional). #'@param reg_level An integer number from 1 to 3 indicating the 'NUTS' dataset #' level. For other datasets this parameter is not used. One mask can only have -#' a unique level. It is set to 3 by default. +#' a unique level. It is set to 3 by default (optional). #'@param lat_dim A character string indicating the latitudinal dimension. If it #' is NULL, the latitudinal name will be searched using an internal function #' with the following possible names: 'lat', 'latitude', 'y', 'j' and -#' 'nav_lat'. It is NULL by default. +#' 'nav_lat'. It is set to NULL by default. #'@param lon_dim A character string indicating the longitudinal dimension. If it #' is NULL, the longitudinal name will be searched using an internal function #' with the following possible names: 'lon', 'longitude', 'x', 'i' and -#' 'nav_lon'. +#' 'nav_lon'. It is set to NULL by default. #'@param target_crs A character string indicating the target 'Coordinate #' Reference System'. #'@param region A logical value indicating if we want a dimension for the @@ -79,9 +82,9 @@ #'@importFrom doParallel registerDoParallel #'@export ShapeToMask <- function(shp_file, ref_grid, - shp_system = "NUTS", reg_names = NULL, - reg_ids = NULL, shp_col_name_ids = NULL, - reg_level = 3, lat_dim = NULL, lon_dim = NULL, + shp_system = "NUTS", reg_names = NULL, reg_ids = NULL, + shp_col_name_ids = NULL, reg_level = 3, + lat_dim = NULL, lon_dim = NULL, region = FALSE, target_crs = NULL, check_valid = FALSE, find_min_dist = FALSE, max_dist = 50, ncores = NULL, ...) { @@ -143,11 +146,10 @@ ShapeToMask <- function(shp_file, ref_grid, stop("Parameter 'shp_system' must be a character strinig.") } if (all(is.null(reg_ids), is.null(reg_names))) { - stop("Either provide parameter 'reg_ids' or 'reg_names'.") + stop("If 'shp_system' is used, you must provide either parameter ", + "'reg_ids' or 'reg_names'.") } - } else if (is.null(shp_col_name_ids)) { - stop("Either provide parameter 'shp_system' or 'shp_col_name_ids'.") - } else { + } else if (!is.null(shp_col_name_ids)) { if (is.null(reg_ids)) { stop("If 'shp_col_name_ids' is used, parameter 'reg_ids' must be provided.") } @@ -331,6 +333,7 @@ ShapeToMask <- function(shp_file, ref_grid, registerDoSEQ() } if (region) { + if (length(dim(mask)) == 2) dim(mask) <- c(dim(mask), 1) names(dim(mask)) <- c(lon_dim, lat_dim, 'region') } else { names(dim(mask)) <- c(lon_dim, lat_dim) diff --git a/man/ShapeToMask.Rd b/man/ShapeToMask.Rd index f5beede0a5562e3688e3bbd87995736a793f1b23..5f4a6600baeccb14e0cda51d4c8c31ca4d561d5e 100644 --- a/man/ShapeToMask.Rd +++ b/man/ShapeToMask.Rd @@ -31,33 +31,36 @@ data. Either (1) a netCDF file or (2) a list of lon and lat to provide the reference grid points. It is NULL by default.} \item{shp_system}{A character string containing the Shapefile System Database -name. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. It is set to -'NUTS' by default.} +Name used to subset the shapefile into regions by using parameters 'reg_ids' +or 'reg_names'. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. When it +is used, you must specify either 'reg_ids' or 'reg_names'; if you don't need +to subset different regions, set it to NULL. It is set to "NUTS" by default +(optional).} \item{reg_names}{A named list of character string vectors indicating the country and the region name. The name of the list stands for the country name code and the vector character strings indicate the region name for -each country. It is NULL by default.} +each country. It is NULL by default (optional).} \item{reg_ids}{A character string indicating the unique ID in shapefile. -It is NULL by default.} +It is NULL by default (optional).} \item{shp_col_name_ids}{A character string indicating the column name of the -column in where the specified 'reg_ids' will be taken.} +column in where the specified 'reg_ids' will be taken (optional).} \item{reg_level}{An integer number from 1 to 3 indicating the 'NUTS' dataset level. For other datasets this parameter is not used. One mask can only have -a unique level. It is set to 3 by default.} +a unique level. It is set to 3 by default (optional).} \item{lat_dim}{A character string indicating the latitudinal dimension. If it is NULL, the latitudinal name will be searched using an internal function with the following possible names: 'lat', 'latitude', 'y', 'j' and -'nav_lat'. It is NULL by default.} +'nav_lat'. It is set to NULL by default.} \item{lon_dim}{A character string indicating the longitudinal dimension. If it is NULL, the longitudinal name will be searched using an internal function with the following possible names: 'lon', 'longitude', 'x', 'i' and -'nav_lon'.} +'nav_lon'. It is set to NULL by default.} \item{region}{A logical value indicating if we want a dimension for the regions in the resulting mask array. It is FALSE by default.} diff --git a/tests/testthat/test-ShapeToMask.R b/tests/testthat/test-ShapeToMask.R index 0217da5ab4511cdb2e69cd430cc4caf49901365a..550f2ee008d8dca00fe7307cda69c071ac04d049 100644 --- a/tests/testthat/test-ShapeToMask.R +++ b/tests/testthat/test-ShapeToMask.R @@ -87,6 +87,11 @@ test_that("2. Output", { sum(mask3), 105 ) + # Test a single region + mask3_1 <- ShapeToMask(shp_file1, ref_grid = ref_grid1_1, + shp_col_name_ids = "NUTS_ID", lon_dim = 'lon', + lat_dim = 'lat', reg_ids = "DE149", region = TRUE) + # Test GADM mask4 <- ShapeToMask(shp_file = shp_file2, ref_grid = ref_grid2, reg_ids = GADM_id2, shp_system = "GADM") expect_equal(