From 06d8f61318690c35d1c51d4af65151d85f1e824d Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Tue, 5 Sep 2023 11:56:14 +0200 Subject: [PATCH 1/4] Add function ShapeToMask --- R/ShapeToMask.R | 318 +++++++++++++++++++++++++++++++++++++++++++++ man/ShapeToMask.Rd | 135 +++++++++++++++++++ 2 files changed, 453 insertions(+) create mode 100644 R/ShapeToMask.R create mode 100644 man/ShapeToMask.Rd diff --git a/R/ShapeToMask.R b/R/ShapeToMask.R new file mode 100644 index 0000000..1695682 --- /dev/null +++ b/R/ShapeToMask.R @@ -0,0 +1,318 @@ +#'Convert Shapefile to Mask Array +#' +#'This function reads a shapefile (.shp) containing information about polygonal +#'regions. It then transfers the shapefile data into an array and subsets the +#'output based on requested region names or IDs. The accepted shapefile +#'databases are 'NUTS', 'LAU', and 'GADM', each with its own unique format. +#'However, the function can use other shapefiles databases with specifying the +#'categories names with the parameter 'shp.system.name'. +#' +#'To ensure accurate comparison with the shapefile, the function loads a +#'reference dataset that provides longitude and latitude information. By +#'intersecting each subset of the shapefile with the reference coordinates, the +#'function selects only the desired regions. The final step involves creating a +#'mask array. Depending on the chosen option, the mask array is either returned +#'as the function's output or saved into a NetCDF format in the specified +#'directory. +#' +#'Note: Modules GDAL, PROJ and GEOS are required. +#' +#'@param shp.file A character string indicating the shp file path. +#'@param ref.grid A character string indicating the path to the reference +#' 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. +#'@param shp.system.name A character string indicating the column name of the +#' column in where the specified 'reg.ids' will be taken. +#'@param reg.ids A character string indicating the unique ID in shapefile. +#' It is NULL by default. +#'@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. +#'@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. +#'@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. +#'@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'. It is 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 +#' regions in the resulting mask array. It is FALSE by default. +#'@param check_valid A logical value that when it is TRUE it uses the function +#' 'sf::st_make_valid' applied to the shapefile and to the coordinates. +#'@param max_dist A numeric value indicating the maximum distance is accepted +#' to the closest gridpoint when there is no intersection between the shapefile +#' and the reference grid. +#'@param savefile A logical value indicating wether to save the mask array into +#' a NetCDF format file (TRUE) or to return an array (FALSE). It is FALSE by +#' default. This functionality is not developed yet. +#'@param ... Arguments passed on to 's2_options' in function 'st_intersection'. +#' See 's2 package'. +#' +#'@return A multidimensional array containing a mask array with longitude and +#'latitude dimensions. If 'region' is TRUE, there will be a dimension for +#'the region. +#' +#'@examples +#'# Exmple (1): NUTS +#'shp.file <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', +#' 'NUTS_RG_60M_2021_4326.shp') +#'# shp.file <- paste0('/esarchive/scratch/cdelgado/focus_outputs/Shapefiles/', +#'# 'tza_admbnda_adm1/tza_admbnda_adm1_20181019.shp') +#'# ref.grid <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', +#'# 'tas_f6h/tas_20170201.nc') +#'# ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', +#'# 'tas_f6h/tas_20191212.nc') +#'ref.grid <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/', +#' 'tas_f1h/tas_201006.nc') +#'# ref.grid <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5)) +#' +#'NUTS.id <- paste0("FI1D", c(1:3, 5, 7:9)) +#'NUTS.name <- list(FI = c('Lappi', 'Kainuu'), SI = c('Pomurska', 'Podravska')) +#'mask1 <- ShapeToMask(shp.file, ref.grid, reg.ids = NUTS.id) +#'mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, +#' reg.names = NUTS.name) +#' +#'# Exmple (2): GADM +#'shp.file <- "/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp" +#'ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', +#' 'tas_f6h/tas_20191212.nc') +#'GADM.id <- c("ESP", "ITA") +#'GADM.name <- c("Spain", "Italy") +#'mask1 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, +#' reg.ids = GADM.id, shp.system = "GADM") +#'mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, +#' reg.names = GADM.name, shp.system = "GADM") +#'@import easyNCDF +#'@import sf +#'@export +ShapeToMask <- function(shp.file, ref.grid = NULL, + shp.system = "NUTS", shp.system.name = NULL, + reg.ids = NULL, reg.names = NULL, + reg.level = 3, lat_dim = NULL, lon_dim = NULL, + savefile = FALSE, region = FALSE, target_crs = NULL, + check_valid = FALSE, max_dist = 99999999, ...) { + + # NOTE: One region is one number; need to have the option to combine them? + # TODO: Suppress warnings? + # TODO: Substitute packages + + # Step 1: Load the shapefile + shp <- sf::st_read(shp.file) # class sf + if (!is.null(target_crs)) { + transformed_shapefile <- st_transform(shp, crs = target_crs) + shp <- transformed_shapefile + } + + if (all(is.null(reg.ids), is.null(reg.names))) { + stop("Either provide parameter 'reg.ids' or 'reg.names'.") + } else if (!is.null(reg.ids)) { + ## Method 1: Directly use IDs + if (!is.null(shp.system.name)) { + if (shp.system.name %in% names(shp)) { + shp <- subset(shp, get(shp.system.name) %in% reg.ids) + } else { + stop("Shape system name not found in shapefile names.") + } + } else if (shp.system == "NUTS") { + shp <- subset(shp, NUTS_ID %in% reg.ids) + shp.system.name <- NUTS_ID + } else if (shp.system == "ADM") { + shp <- subset(shp, ADM1_PCODE %in% reg.ids) + shp.system.name <- ADM1_PCODE + } else if (shp.system == "GADM") { + shp <- subset(shp, ISO %in% reg.ids) + shp.system.name <- ISO + } else { + stop("shp.system ", shp.system, " is not defined yet.") + } + if (!is.null(reg.names)) { + warning("Only use 'reg.ids' to get the shape region. 'reg.names' is not used.") + } + + } else if (!is.null(reg.names)) { + shp.system.name <- NULL + ## Method 2: Use country code & region name + for (cntr_i in 1:length(reg.names)) { + if (shp.system == "NUTS") { + tmp <- subset(shp, CNTR_CODE == names(reg.names)[cntr_i]) + tmp <- subset(tmp, NUTS_NAME %in% reg.names[[cntr_i]]) + shp.system.name <- NUTS_NAME + } else if (shp.system == "ADM") { + tmp <- subset(shp, ADM0_EN == names(reg.names)[cntr_i]) + tmp <- subset(tmp, ADM1_EN %in% reg.names[[cntr_i]]) + shp.system.name <- ADM1_EN + } else if (shp.system == "GADM") { + tmp <- subset(shp, Name %in% reg.names) + shp.system.name <- Name + } + if (cntr_i == 1) { + shp_tmp <- tmp + } else { + shp_tmp <- rbind(shp_tmp, tmp) + } + } + if (shp.system == "NUTS") { + shp <- subset(shp_tmp, LEVL_CODE == reg.level) + } else if (shp.system == "ADM" | shp.system == "GADM") { + shp <- shp_tmp + } + } + + # Step 2: Use the reference file to get lat and lon + + if (all(tools::file_ext(ref.grid) == 'nc')) { + if (!file.exists(ref.grid)) { + stop("ref.grid file does not exist.") + } else { + + ## Method 1: ref.grid is a netCDF file + if (is.null(lat_dim) | is.null(lon_dim)) { + var_names <- easyNCDF::NcReadVarNames(ref.grid) + lat_dim <- var_names[which(var_names %in% s2dv:::.KnownLatNames())] + lon_dim <- var_names[which(var_names %in% s2dv:::.KnownLonNames())] + } + latlon <- NcToArray(ref.grid, vars_to_read = c(lat_dim, lon_dim)) + lat <- NcToArray(ref.grid, vars_to_read = lat_dim)[1, ] + lon <- NcToArray(ref.grid, vars_to_read = lon_dim)[1, ] + } + } else if (is.list(ref.grid)) { + ## Method 2: ref.grid is a list of lon and lat + if (length(ref.grid) != 2) { + stop("If 'ref.grid' is a list, it must have two items for longitude and latitude.") + } + if (is.null(lat_dim) | is.null(lon_dim)) { + # NOTE: the names come from s2dv:::.KnownLonNames and .KnownLatNames + lon_known_names <- c(s2dv:::.KnownLonNames(), 'lons') + lat_known_names <- c(s2dv:::.KnownLatNames(), 'lats') + lon_dim <- lon_known_names[which(lon_known_names %in% names(ref.grid))] + lat_dim <- lat_known_names[which(lat_known_names %in% names(ref.grid))] + + if (identical(lon_dim, character(0)) | identical(lat_dim, character(0))) { + stop("longitude and latitude names are not recognized in 'ref.grid'. Please specify 'lon_dim' and 'lat_dim'.") + } + } + lat <- ref.grid[[lat_dim]] + lon <- ref.grid[[lon_dim]] + + } else { + stop("Parameter 'ref.grid' must be either a netCDF file or a list of lon and lat.") + } + + ## Create data frame & sp class for ref grid + ref.df <- data.frame(data = 0, + lon = rep(lon, times = length(lat)), + lat = rep(lat, each = length(lon))) + coord <- as.matrix(data.frame(x = ref.df$lon, y = ref.df$lat)) + + xy.sfg <- st_multipoint(coord) + xy.sfc <- st_sfc(xy.sfg) + # Assign crs of original shapefile + if (!is.null(target_crs)) { + st_crs(xy.sfc) <- sf::st_crs(target_crs) #initial_crs # asign crs of original shapefile + xy.sfc <- sf::st_transform(xy.sfc, st_crs(shp)) + } else { + st_crs(xy.sfc) <- sf::st_crs(shp) + } + + # Step 3: Create mask + ## Create mask array with 0; 1, 2, etc. will be filled in for each shp region + mask <- array(0, dim = c(length(lon), length(lat))) + names(dim(mask)) <- c(lon_dim, lat_dim) + if (region) { + mask <- array(0, dim = c(nrow(shp), length(lon), length(lat))) + names(dim(mask)) <- c('region', lon_dim, lat_dim) + } + + if (check_valid) { + xy.sfc <- st_make_valid(xy.sfc) + shp <- st_make_valid(shp) + } + + ## Loop through each shp region + for (shp_i in 1:nrow(shp)) { + # NOTE: Don't know it's a problem in st_intersection or st_coordinates, tmp_coords may + # not be identical as lon/lat. E.g., (29, 65) may be (29 - -3.552714e-15, 65). + shp_pol <- sf::st_intersection(xy.sfc, shp[shp_i, ]) + # shp_pol <- sf::st_intersection(xy.sfc, shp[shp_i, ], ...) + tmp_coords <- st_coordinates(shp_pol)[, 1:2] + if (length(tmp_coords) == 0) { + dim(tmp_coords) <- NULL + } else if (is.null(dim(tmp_coords))) { + tmp_coords <- array(tmp_coords, dim = c(1, length(tmp_coords))) + } + if (!is.null(dim(tmp_coords))) { + + # polygon_instersection + for (ii in 1:nrow(tmp_coords)) { + # pt_x <- which(lon == tmp_coords[ii, 1]) + # pt_y <- which.min(abs(lat - tmp_coords[ii, 2])) + if (length(dim(mask)) == 2) { + # min(abs(lon - tmp_coords[ii, 1])) + # min(abs(lat - tmp_coords[ii, 2])) + mask[which.min(abs(lon - tmp_coords[ii, 1])), + which.min(abs(lat - tmp_coords[ii, 2]))] <- shp_i + } else { + mask[shp_i, which.min(abs(lon - tmp_coords[ii, 1])), + which.min(abs(lat - tmp_coords[ii, 2]))] <- 1 + } + } + } else { + x.centroid.shpi <- sf::st_coordinates(sf::st_centroid(shp[shp_i,]))[,1] + y.centroid.shpi <- sf::st_coordinates(sf::st_centroid(shp[shp_i,]))[,2] + dist <- sqrt((xy.sfg[,1] - x.centroid.shpi)**2 + (xy.sfg[,2] - y.centroid.shpi)**2) + tmp_coords <- array(xy.sfg[which(dist == min(dist, na.rm = TRUE)),], dim = c(1,2)) + colnames(tmp_coords) <- c('X', 'Y') + if (max(dist) <= max_dist & (any(round(lat,2) == round(tmp_coords[1,2],2)) & + any(round(lon,2) == round(tmp_coords[1,1],2))) ) { + if (length(dim(mask)) == 2) { + mask[which.min(abs(lon - tmp_coords[, 1])), + which.min(abs(lat - tmp_coords[, 2]))] <- shp_i + } else { + mask[shp_i, which.min(abs(lon - tmp_coords[, 1])), + which.min(abs(lat - tmp_coords[, 2]))] <- 1 + } + warning(paste0('The reference grid has no intersection with region ', + ifelse(is.character(shp.system.name), shp[shp_i,][[shp.system.name]], paste0('n° ', shp_i)), + ' from the shapefile; the provided grid cell is at a distance of ', dist[which(dist == min(dist, na.rm = TRUE))], + ' to the centroid of the region (units are: ° or meters depending on the crs of the shapefile).')) + } else { + warning(paste0('The reference grid has no intersection with region ', + ifelse(is.character(shp.system.name), shp[shp_i,][[shp.system.name]], paste0('n° ', shp_i)))) + } + } + } + + # Step 4: Add attributes + # attr(mask, lon_dim) <- lon + # attr(mask, lat_dim) <- lat + # if (shp.system == "NUTS") { + # attr(mask, "index") <- as.list(shp$NUTS_ID) + # } else if (shp.system == "ADM") { + # attr(mask, "index") <- as.list(shp$ADM1_PCODE) + # } else if (shp.system == "GADM") { + # attr(mask, "index") <- as.list(shp$ISO) + # } + # names(attr(mask, "index")) <- 1:nrow(shp) + + # ## Return all the info from shp + # attr(mask, "shapefile") <- attributes(shp) + + # Step 5: Save the file or return the array + if (!savefile) { + return(mask) + } else { + warning("This functionality is not developed yet.") + # TODO + # ArrayToNc() + } +} \ No newline at end of file diff --git a/man/ShapeToMask.Rd b/man/ShapeToMask.Rd new file mode 100644 index 0000000..20d1154 --- /dev/null +++ b/man/ShapeToMask.Rd @@ -0,0 +1,135 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ShapeToMask.R +\name{ShapeToMask} +\alias{ShapeToMask} +\title{Convert Shapefile to Mask Array} +\usage{ +ShapeToMask( + shp.file, + ref.grid = NULL, + shp.system = "NUTS", + shp.system.name = NULL, + reg.ids = NULL, + reg.names = NULL, + reg.level = 3, + lat_dim = NULL, + lon_dim = NULL, + savefile = FALSE, + region = FALSE, + target_crs = NULL, + check_valid = FALSE, + max_dist = 99999999, + ... +) +} +\arguments{ +\item{shp.file}{A character string indicating the shp file path.} + +\item{ref.grid}{A character string indicating the path to the reference +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.} + +\item{shp.system.name}{A character string indicating the column name of the +column in where the specified 'reg.ids' will be taken.} + +\item{reg.ids}{A character string indicating the unique ID in shapefile. +It is NULL by default.} + +\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.} + +\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.} + +\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.} + +\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'. It is NULL by default.} + +\item{savefile}{A logical value indicating wether to save the mask array into +a NetCDF format file (TRUE) or to return an array (FALSE). It is FALSE by +default. This functionality is not developed yet.} + +\item{region}{A logical value indicating if we want a dimension for the +regions in the resulting mask array. It is FALSE by default.} + +\item{target_crs}{A character string indicating the target 'Coordinate +Reference System'.} + +\item{check_valid}{A logical value that when it is TRUE it uses the function +'sf::st_make_valid' applied to the shapefile and to the coordinates.} + +\item{max_dist}{A numeric value indicating the maximum distance is accepted +to the closest gridpoint when there is no intersection between the shapefile +and the reference grid.} + +\item{...}{Arguments passed on to 's2_options' in function 'st_intersection'. +See 's2 package'.} +} +\value{ +A multidimensional array containing a mask array with longitude and +latitude dimensions. If 'region' is TRUE, there will be a dimension for +the region. +} +\description{ +This function reads a shapefile (.shp) containing information about polygonal +regions. It then transfers the shapefile data into an array and subsets the +output based on requested region names or IDs. The accepted shapefile +databases are 'NUTS', 'LAU', and 'GADM', each with its own unique format. +However, the function can use other shapefiles databases with specifying the +categories names with the parameter 'shp.system.name'. +} +\details{ +To ensure accurate comparison with the shapefile, the function loads a +reference dataset that provides longitude and latitude information. By +intersecting each subset of the shapefile with the reference coordinates, the +function selects only the desired regions. The final step involves creating a +mask array. Depending on the chosen option, the mask array is either returned +as the function's output or saved into a NetCDF format in the specified +directory. + +Note: Modules GDAL, PROJ and GEOS are required. +} +\examples{ +# Exmple (1): NUTS +shp.file <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', + 'NUTS_RG_60M_2021_4326.shp') +# shp.file <- paste0('/esarchive/scratch/cdelgado/focus_outputs/Shapefiles/', +# 'tza_admbnda_adm1/tza_admbnda_adm1_20181019.shp') +# ref.grid <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', +# 'tas_f6h/tas_20170201.nc') +# ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', +# 'tas_f6h/tas_20191212.nc') +ref.grid <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/', + 'tas_f1h/tas_201006.nc') +# ref.grid <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5)) + +NUTS.id <- paste0("FI1D", c(1:3, 5, 7:9)) +NUTS.name <- list(FI = c('Lappi', 'Kainuu'), SI = c('Pomurska', 'Podravska')) +mask1 <- ShapeToMask(shp.file, ref.grid, reg.ids = NUTS.id) +mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, + reg.names = NUTS.name) + +# Exmple (2): GADM +shp.file <- "/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp" +ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', + 'tas_f6h/tas_20191212.nc') +GADM.id <- c("ESP", "ITA") +GADM.name <- c("Spain", "Italy") +mask1 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, + reg.ids = GADM.id, shp.system = "GADM") +mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, + reg.names = GADM.name, shp.system = "GADM") +} -- GitLab From 3e9aabd09e70432c4145821a1cdd989c36e550bf Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Tue, 5 Sep 2023 14:13:02 +0200 Subject: [PATCH 2/4] Substitute the dot of some parameter names in ShapeToMask to _ --- NAMESPACE | 3 + R/ShapeToMask.R | 174 ++++++++++++++++++++-------------------- man/ColorBarDiscrete.Rd | 4 +- man/ShapeToMask.Rd | 62 +++++++------- 4 files changed, 123 insertions(+), 120 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index e7fb6d0..bdaec10 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,9 @@ export(ClimColors) export(ClimPalette) export(ColorBarDiscrete) +export(ShapeToMask) +import(easyNCDF) +import(sf) importFrom(grDevices,col2rgb) importFrom(grDevices,colorRampPalette) importFrom(grDevices,rgb) diff --git a/R/ShapeToMask.R b/R/ShapeToMask.R index 1695682..a891824 100644 --- a/R/ShapeToMask.R +++ b/R/ShapeToMask.R @@ -5,7 +5,7 @@ #'output based on requested region names or IDs. The accepted shapefile #'databases are 'NUTS', 'LAU', and 'GADM', each with its own unique format. #'However, the function can use other shapefiles databases with specifying the -#'categories names with the parameter 'shp.system.name'. +#'categories names with the parameter 'shp_system_name'. #' #'To ensure accurate comparison with the shapefile, the function loads a #'reference dataset that provides longitude and latitude information. By @@ -17,22 +17,22 @@ #' #'Note: Modules GDAL, PROJ and GEOS are required. #' -#'@param shp.file A character string indicating the shp file path. -#'@param ref.grid A character string indicating the path to the reference +#'@param shp_file A character string indicating the shp file path. +#'@param ref_grid A character string indicating the path to the reference #' 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 +#'@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. -#'@param shp.system.name A character string indicating the column name of the -#' column in where the specified 'reg.ids' will be taken. -#'@param reg.ids A character string indicating the unique ID in shapefile. +#'@param shp_system_name A character string indicating the column name of the +#' column in where the specified 'reg_ids' will be taken. +#'@param reg_ids A character string indicating the unique ID in shapefile. #' It is NULL by default. -#'@param reg.names A named list of character string vectors indicating the +#'@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. -#'@param reg.level An integer number from 1 to 3 indicating the 'NUTS' dataset +#'@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. #'@param lat_dim A character string indicating the latitudinal dimension. If it @@ -64,41 +64,41 @@ #' #'@examples #'# Exmple (1): NUTS -#'shp.file <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', +#'shp_file <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', #' 'NUTS_RG_60M_2021_4326.shp') -#'# shp.file <- paste0('/esarchive/scratch/cdelgado/focus_outputs/Shapefiles/', +#'# shp_file <- paste0('/esarchive/scratch/cdelgado/focus_outputs/Shapefiles/', #'# 'tza_admbnda_adm1/tza_admbnda_adm1_20181019.shp') -#'# ref.grid <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', +#'# ref_grid <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', #'# 'tas_f6h/tas_20170201.nc') -#'# ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', +#'# ref_grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', #'# 'tas_f6h/tas_20191212.nc') -#'ref.grid <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/', +#'ref_grid <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/', #' 'tas_f1h/tas_201006.nc') -#'# ref.grid <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5)) +#'# ref_grid <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5)) #' #'NUTS.id <- paste0("FI1D", c(1:3, 5, 7:9)) #'NUTS.name <- list(FI = c('Lappi', 'Kainuu'), SI = c('Pomurska', 'Podravska')) -#'mask1 <- ShapeToMask(shp.file, ref.grid, reg.ids = NUTS.id) -#'mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, -#' reg.names = NUTS.name) +#'mask1 <- ShapeToMask(shp_file, ref_grid, reg_ids = NUTS.id) +#'mask2 <- ShapeToMask(shp_file = shp_file, ref_grid = ref_grid, +#' reg_names = NUTS.name) #' #'# Exmple (2): GADM -#'shp.file <- "/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp" -#'ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', +#'shp_file <- "/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp" +#'ref_grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', #' 'tas_f6h/tas_20191212.nc') #'GADM.id <- c("ESP", "ITA") #'GADM.name <- c("Spain", "Italy") -#'mask1 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, -#' reg.ids = GADM.id, shp.system = "GADM") -#'mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, -#' reg.names = GADM.name, shp.system = "GADM") +#'mask1 <- ShapeToMask(shp_file = shp_file, ref_grid = ref_grid, +#' reg_ids = GADM.id, shp_system = "GADM") +#'mask2 <- ShapeToMask(shp_file = shp_file, ref_grid = ref_grid, +#' reg_names = GADM.name, shp_system = "GADM") #'@import easyNCDF #'@import sf #'@export -ShapeToMask <- function(shp.file, ref.grid = NULL, - shp.system = "NUTS", shp.system.name = NULL, - reg.ids = NULL, reg.names = NULL, - reg.level = 3, lat_dim = NULL, lon_dim = NULL, +ShapeToMask <- function(shp_file, ref_grid = NULL, + shp_system = "NUTS", shp_system_name = NULL, + reg_ids = NULL, reg_names = NULL, + reg_level = 3, lat_dim = NULL, lon_dim = NULL, savefile = FALSE, region = FALSE, target_crs = NULL, check_valid = FALSE, max_dist = 99999999, ...) { @@ -107,53 +107,53 @@ ShapeToMask <- function(shp.file, ref.grid = NULL, # TODO: Substitute packages # Step 1: Load the shapefile - shp <- sf::st_read(shp.file) # class sf + shp <- sf::st_read(shp_file) # class sf if (!is.null(target_crs)) { transformed_shapefile <- st_transform(shp, crs = target_crs) shp <- transformed_shapefile } - if (all(is.null(reg.ids), is.null(reg.names))) { - stop("Either provide parameter 'reg.ids' or 'reg.names'.") - } else if (!is.null(reg.ids)) { + if (all(is.null(reg_ids), is.null(reg_names))) { + stop("Either provide parameter 'reg_ids' or 'reg_names'.") + } else if (!is.null(reg_ids)) { ## Method 1: Directly use IDs - if (!is.null(shp.system.name)) { - if (shp.system.name %in% names(shp)) { - shp <- subset(shp, get(shp.system.name) %in% reg.ids) + if (!is.null(shp_system_name)) { + if (shp_system_name %in% names(shp)) { + shp <- subset(shp, get(shp_system_name) %in% reg_ids) } else { stop("Shape system name not found in shapefile names.") } - } else if (shp.system == "NUTS") { - shp <- subset(shp, NUTS_ID %in% reg.ids) - shp.system.name <- NUTS_ID - } else if (shp.system == "ADM") { - shp <- subset(shp, ADM1_PCODE %in% reg.ids) - shp.system.name <- ADM1_PCODE - } else if (shp.system == "GADM") { - shp <- subset(shp, ISO %in% reg.ids) - shp.system.name <- ISO + } else if (shp_system == "NUTS") { + shp <- subset(shp, NUTS_ID %in% reg_ids) + shp_system_name <- NUTS_ID + } else if (shp_system == "ADM") { + shp <- subset(shp, ADM1_PCODE %in% reg_ids) + shp_system_name <- ADM1_PCODE + } else if (shp_system == "GADM") { + shp <- subset(shp, ISO %in% reg_ids) + shp_system_name <- ISO } else { - stop("shp.system ", shp.system, " is not defined yet.") + stop("shp_system ", shp_system, " is not defined yet.") } - if (!is.null(reg.names)) { - warning("Only use 'reg.ids' to get the shape region. 'reg.names' is not used.") + if (!is.null(reg_names)) { + warning("Only use 'reg_ids' to get the shape region. 'reg_names' is not used.") } - } else if (!is.null(reg.names)) { - shp.system.name <- NULL + } else if (!is.null(reg_names)) { + shp_system_name <- NULL ## Method 2: Use country code & region name - for (cntr_i in 1:length(reg.names)) { - if (shp.system == "NUTS") { - tmp <- subset(shp, CNTR_CODE == names(reg.names)[cntr_i]) - tmp <- subset(tmp, NUTS_NAME %in% reg.names[[cntr_i]]) - shp.system.name <- NUTS_NAME - } else if (shp.system == "ADM") { - tmp <- subset(shp, ADM0_EN == names(reg.names)[cntr_i]) - tmp <- subset(tmp, ADM1_EN %in% reg.names[[cntr_i]]) - shp.system.name <- ADM1_EN - } else if (shp.system == "GADM") { - tmp <- subset(shp, Name %in% reg.names) - shp.system.name <- Name + for (cntr_i in 1:length(reg_names)) { + if (shp_system == "NUTS") { + tmp <- subset(shp, CNTR_CODE == names(reg_names)[cntr_i]) + tmp <- subset(tmp, NUTS_NAME %in% reg_names[[cntr_i]]) + shp_system_name <- NUTS_NAME + } else if (shp_system == "ADM") { + tmp <- subset(shp, ADM0_EN == names(reg_names)[cntr_i]) + tmp <- subset(tmp, ADM1_EN %in% reg_names[[cntr_i]]) + shp_system_name <- ADM1_EN + } else if (shp_system == "GADM") { + tmp <- subset(shp, Name %in% reg_names) + shp_system_name <- Name } if (cntr_i == 1) { shp_tmp <- tmp @@ -161,51 +161,51 @@ ShapeToMask <- function(shp.file, ref.grid = NULL, shp_tmp <- rbind(shp_tmp, tmp) } } - if (shp.system == "NUTS") { - shp <- subset(shp_tmp, LEVL_CODE == reg.level) - } else if (shp.system == "ADM" | shp.system == "GADM") { + if (shp_system == "NUTS") { + shp <- subset(shp_tmp, LEVL_CODE == reg_level) + } else if (shp_system == "ADM" | shp_system == "GADM") { shp <- shp_tmp } } # Step 2: Use the reference file to get lat and lon - if (all(tools::file_ext(ref.grid) == 'nc')) { - if (!file.exists(ref.grid)) { - stop("ref.grid file does not exist.") + if (all(tools::file_ext(ref_grid) == 'nc')) { + if (!file.exists(ref_grid)) { + stop("ref_grid file does not exist.") } else { - ## Method 1: ref.grid is a netCDF file + ## Method 1: ref_grid is a netCDF file if (is.null(lat_dim) | is.null(lon_dim)) { - var_names <- easyNCDF::NcReadVarNames(ref.grid) + var_names <- easyNCDF::NcReadVarNames(ref_grid) lat_dim <- var_names[which(var_names %in% s2dv:::.KnownLatNames())] lon_dim <- var_names[which(var_names %in% s2dv:::.KnownLonNames())] } - latlon <- NcToArray(ref.grid, vars_to_read = c(lat_dim, lon_dim)) - lat <- NcToArray(ref.grid, vars_to_read = lat_dim)[1, ] - lon <- NcToArray(ref.grid, vars_to_read = lon_dim)[1, ] + latlon <- NcToArray(ref_grid, vars_to_read = c(lat_dim, lon_dim)) + lat <- NcToArray(ref_grid, vars_to_read = lat_dim)[1, ] + lon <- NcToArray(ref_grid, vars_to_read = lon_dim)[1, ] } - } else if (is.list(ref.grid)) { - ## Method 2: ref.grid is a list of lon and lat - if (length(ref.grid) != 2) { - stop("If 'ref.grid' is a list, it must have two items for longitude and latitude.") + } else if (is.list(ref_grid)) { + ## Method 2: ref_grid is a list of lon and lat + if (length(ref_grid) != 2) { + stop("If 'ref_grid' is a list, it must have two items for longitude and latitude.") } if (is.null(lat_dim) | is.null(lon_dim)) { # NOTE: the names come from s2dv:::.KnownLonNames and .KnownLatNames lon_known_names <- c(s2dv:::.KnownLonNames(), 'lons') lat_known_names <- c(s2dv:::.KnownLatNames(), 'lats') - lon_dim <- lon_known_names[which(lon_known_names %in% names(ref.grid))] - lat_dim <- lat_known_names[which(lat_known_names %in% names(ref.grid))] + lon_dim <- lon_known_names[which(lon_known_names %in% names(ref_grid))] + lat_dim <- lat_known_names[which(lat_known_names %in% names(ref_grid))] if (identical(lon_dim, character(0)) | identical(lat_dim, character(0))) { - stop("longitude and latitude names are not recognized in 'ref.grid'. Please specify 'lon_dim' and 'lat_dim'.") + stop("longitude and latitude names are not recognized in 'ref_grid'. Please specify 'lon_dim' and 'lat_dim'.") } } - lat <- ref.grid[[lat_dim]] - lon <- ref.grid[[lon_dim]] + lat <- ref_grid[[lat_dim]] + lon <- ref_grid[[lon_dim]] } else { - stop("Parameter 'ref.grid' must be either a netCDF file or a list of lon and lat.") + stop("Parameter 'ref_grid' must be either a netCDF file or a list of lon and lat.") } ## Create data frame & sp class for ref grid @@ -282,12 +282,12 @@ ShapeToMask <- function(shp.file, ref.grid = NULL, which.min(abs(lat - tmp_coords[, 2]))] <- 1 } warning(paste0('The reference grid has no intersection with region ', - ifelse(is.character(shp.system.name), shp[shp_i,][[shp.system.name]], paste0('n° ', shp_i)), + ifelse(is.character(shp_system_name), shp[shp_i,][[shp_system_name]], paste0('n° ', shp_i)), ' from the shapefile; the provided grid cell is at a distance of ', dist[which(dist == min(dist, na.rm = TRUE))], ' to the centroid of the region (units are: ° or meters depending on the crs of the shapefile).')) } else { warning(paste0('The reference grid has no intersection with region ', - ifelse(is.character(shp.system.name), shp[shp_i,][[shp.system.name]], paste0('n° ', shp_i)))) + ifelse(is.character(shp_system_name), shp[shp_i,][[shp_system_name]], paste0('n° ', shp_i)))) } } } @@ -295,11 +295,11 @@ ShapeToMask <- function(shp.file, ref.grid = NULL, # Step 4: Add attributes # attr(mask, lon_dim) <- lon # attr(mask, lat_dim) <- lat - # if (shp.system == "NUTS") { + # if (shp_system == "NUTS") { # attr(mask, "index") <- as.list(shp$NUTS_ID) - # } else if (shp.system == "ADM") { + # } else if (shp_system == "ADM") { # attr(mask, "index") <- as.list(shp$ADM1_PCODE) - # } else if (shp.system == "GADM") { + # } else if (shp_system == "GADM") { # attr(mask, "index") <- as.list(shp$ISO) # } # names(attr(mask, "index")) <- 1:nrow(shp) diff --git a/man/ColorBarDiscrete.Rd b/man/ColorBarDiscrete.Rd index 82dcc7c..9ba59d9 100644 --- a/man/ColorBarDiscrete.Rd +++ b/man/ColorBarDiscrete.Rd @@ -11,7 +11,7 @@ ColorBarDiscrete( subsampleg = NULL, bar_limits = NULL, var_limits = NULL, - color_fun = clim.palette(), + color_fun = ClimPalette(), plot = TRUE, draw_ticks = FALSE, draw_separators = TRUE, @@ -81,7 +81,7 @@ specified.} \item{color_fun}{Function to generate the colours of the color bar. Must take an integer and must return as many colours. The returned colour vector can have the attribute 'na_color', with a colour to draw NA values. This -parameter is set by default to clim.palette().} +parameter is set by default to ClimPalette().} \item{plot}{Logical value indicating whether to only compute its breaks and colours (FALSE) or to also draw it on the current device (TRUE).} diff --git a/man/ShapeToMask.Rd b/man/ShapeToMask.Rd index 20d1154..92235ef 100644 --- a/man/ShapeToMask.Rd +++ b/man/ShapeToMask.Rd @@ -5,13 +5,13 @@ \title{Convert Shapefile to Mask Array} \usage{ ShapeToMask( - shp.file, - ref.grid = NULL, - shp.system = "NUTS", - shp.system.name = NULL, - reg.ids = NULL, - reg.names = NULL, - reg.level = 3, + shp_file, + ref_grid = NULL, + shp_system = "NUTS", + shp_system_name = NULL, + reg_ids = NULL, + reg_names = NULL, + reg_level = 3, lat_dim = NULL, lon_dim = NULL, savefile = FALSE, @@ -23,28 +23,28 @@ ShapeToMask( ) } \arguments{ -\item{shp.file}{A character string indicating the shp file path.} +\item{shp_file}{A character string indicating the shp file path.} -\item{ref.grid}{A character string indicating the path to the reference +\item{ref_grid}{A character string indicating the path to the reference 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 +\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.} -\item{shp.system.name}{A character string indicating the column name of the -column in where the specified 'reg.ids' will be taken.} +\item{shp_system_name}{A character string indicating the column name of the +column in where the specified 'reg_ids' will be taken.} -\item{reg.ids}{A character string indicating the unique ID in shapefile. +\item{reg_ids}{A character string indicating the unique ID in shapefile. It is NULL by default.} -\item{reg.names}{A named list of character string vectors indicating the +\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.} -\item{reg.level}{An integer number from 1 to 3 indicating the 'NUTS' dataset +\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.} @@ -89,7 +89,7 @@ regions. It then transfers the shapefile data into an array and subsets the output based on requested region names or IDs. The accepted shapefile databases are 'NUTS', 'LAU', and 'GADM', each with its own unique format. However, the function can use other shapefiles databases with specifying the -categories names with the parameter 'shp.system.name'. +categories names with the parameter 'shp_system_name'. } \details{ To ensure accurate comparison with the shapefile, the function loads a @@ -104,32 +104,32 @@ Note: Modules GDAL, PROJ and GEOS are required. } \examples{ # Exmple (1): NUTS -shp.file <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', +shp_file <- paste0('/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp/', 'NUTS_RG_60M_2021_4326.shp') -# shp.file <- paste0('/esarchive/scratch/cdelgado/focus_outputs/Shapefiles/', +# shp_file <- paste0('/esarchive/scratch/cdelgado/focus_outputs/Shapefiles/', # 'tza_admbnda_adm1/tza_admbnda_adm1_20181019.shp') -# ref.grid <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', +# ref_grid <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', # 'tas_f6h/tas_20170201.nc') -# ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', +# ref_grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', # 'tas_f6h/tas_20191212.nc') -ref.grid <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/', +ref_grid <- paste0('/esarchive/recon/ecmwf/era5land/monthly_mean/', 'tas_f1h/tas_201006.nc') -# ref.grid <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5)) +# ref_grid <- list(lon = seq(10, 40, 0.5), lat = seq(40, 85, 0.5)) NUTS.id <- paste0("FI1D", c(1:3, 5, 7:9)) NUTS.name <- list(FI = c('Lappi', 'Kainuu'), SI = c('Pomurska', 'Podravska')) -mask1 <- ShapeToMask(shp.file, ref.grid, reg.ids = NUTS.id) -mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, - reg.names = NUTS.name) +mask1 <- ShapeToMask(shp_file, ref_grid, reg_ids = NUTS.id) +mask2 <- ShapeToMask(shp_file = shp_file, ref_grid = ref_grid, + reg_names = NUTS.name) # Exmple (2): GADM -shp.file <- "/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp" -ref.grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', +shp_file <- "/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp" +ref_grid <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensfor/weekly_mean/', 'tas_f6h/tas_20191212.nc') GADM.id <- c("ESP", "ITA") GADM.name <- c("Spain", "Italy") -mask1 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, - reg.ids = GADM.id, shp.system = "GADM") -mask2 <- ShapeToMask(shp.file = shp.file, ref.grid = ref.grid, - reg.names = GADM.name, shp.system = "GADM") +mask1 <- ShapeToMask(shp_file = shp_file, ref_grid = ref_grid, + reg_ids = GADM.id, shp_system = "GADM") +mask2 <- ShapeToMask(shp_file = shp_file, ref_grid = ref_grid, + reg_names = GADM.name, shp_system = "GADM") } -- GitLab From 2e028594f4c1a7afc1a5d17e5724f7131de47d1f Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Thu, 21 Sep 2023 13:03:57 +0200 Subject: [PATCH 3/4] Add parameter if we want to search the closest distance or not --- R/ShapeToMask.R | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/R/ShapeToMask.R b/R/ShapeToMask.R index a891824..8c91675 100644 --- a/R/ShapeToMask.R +++ b/R/ShapeToMask.R @@ -49,6 +49,10 @@ #' regions in the resulting mask array. It is FALSE by default. #'@param check_valid A logical value that when it is TRUE it uses the function #' 'sf::st_make_valid' applied to the shapefile and to the coordinates. +#'@param find_min_dist A logical value indicating if we want to look for the +#' nearest coordinate between the shapefile region and the reference grid when +#' there is no intersection between the shapefile and the reference grid. It is +#' FALSE by default. #'@param max_dist A numeric value indicating the maximum distance is accepted #' to the closest gridpoint when there is no intersection between the shapefile #' and the reference grid. @@ -100,11 +104,14 @@ ShapeToMask <- function(shp_file, ref_grid = NULL, reg_ids = NULL, reg_names = NULL, reg_level = 3, lat_dim = NULL, lon_dim = NULL, savefile = FALSE, region = FALSE, target_crs = NULL, - check_valid = FALSE, max_dist = 99999999, ...) { + check_valid = FALSE, find_min_dist = FALSE, + max_dist = 50, ...) { # NOTE: One region is one number; need to have the option to combine them? # TODO: Suppress warnings? # TODO: Substitute packages + # TODO: Substitute loop for each region with multiApply + # TODO: Add initial checks # Step 1: Load the shapefile shp <- sf::st_read(shp_file) # class sf @@ -125,13 +132,13 @@ ShapeToMask <- function(shp_file, ref_grid = NULL, } } else if (shp_system == "NUTS") { shp <- subset(shp, NUTS_ID %in% reg_ids) - shp_system_name <- NUTS_ID + shp_system_name <- "NUTS_ID" } else if (shp_system == "ADM") { shp <- subset(shp, ADM1_PCODE %in% reg_ids) - shp_system_name <- ADM1_PCODE + shp_system_name <- "ADM1_PCODE" } else if (shp_system == "GADM") { shp <- subset(shp, ISO %in% reg_ids) - shp_system_name <- ISO + shp_system_name <- "ISO" } else { stop("shp_system ", shp_system, " is not defined yet.") } @@ -266,7 +273,7 @@ ShapeToMask <- function(shp_file, ref_grid = NULL, which.min(abs(lat - tmp_coords[ii, 2]))] <- 1 } } - } else { + } else if (find_min_dist) { x.centroid.shpi <- sf::st_coordinates(sf::st_centroid(shp[shp_i,]))[,1] y.centroid.shpi <- sf::st_coordinates(sf::st_centroid(shp[shp_i,]))[,2] dist <- sqrt((xy.sfg[,1] - x.centroid.shpi)**2 + (xy.sfg[,2] - y.centroid.shpi)**2) -- GitLab From 1402669421bfdcc377e4241e0595eaf355817a2e Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Thu, 21 Sep 2023 13:05:29 +0200 Subject: [PATCH 4/4] Update documentation of shapetomask --- man/ShapeToMask.Rd | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/man/ShapeToMask.Rd b/man/ShapeToMask.Rd index 92235ef..87826d3 100644 --- a/man/ShapeToMask.Rd +++ b/man/ShapeToMask.Rd @@ -18,7 +18,8 @@ ShapeToMask( region = FALSE, target_crs = NULL, check_valid = FALSE, - max_dist = 99999999, + find_min_dist = FALSE, + max_dist = 50, ... ) } @@ -71,6 +72,11 @@ Reference System'.} \item{check_valid}{A logical value that when it is TRUE it uses the function 'sf::st_make_valid' applied to the shapefile and to the coordinates.} +\item{find_min_dist}{A logical value indicating if we want to look for the +nearest coordinate between the shapefile region and the reference grid when +there is no intersection between the shapefile and the reference grid. It is +FALSE by default.} + \item{max_dist}{A numeric value indicating the maximum distance is accepted to the closest gridpoint when there is no intersection between the shapefile and the reference grid.} -- GitLab