Commit 0980178e authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

Raster resolution with 100 cells in the largest dimension

- Rather than the smallest which produced large rasters and bad performance in regions with a long form factor.
parent 274c5027
Package: mapMCDA
Title: Produce an epidemiological risk map by weighting multiple risk
factors
Version: 0.4.13
Date: 2019-06-12
Version: 0.4.14
Date: 2019-06-15
Authors@R: c( person("Andrea", "Apolloni", email =
"andrea.apolloni@cirad.fr", role = c("ctb"), comment = "Animal
mobility algorithm"), person("Elena", "Arsevska", email =
......
......@@ -18,10 +18,10 @@
#' @examples
#' cmr <- mapMCDA_datasets()
#' raster::plot(distance_map(cmr$water_bodies, boundaries = cmr$cmr_admin3))
distance_map <- function(x, boundaries, res = resolution(boundaries, min_ncells = 100)) {
distance_map <- function(x, boundaries, res = resolution(boundaries, max_ncells = 100)) {
if (inherits(boundaries, "Spatial")) {
max_res <- resolution(boundaries, min_ncells = 2)
max_res <- resolution(boundaries, max_ncells = 2)
stopifnot(res > 0, res < max_res)
ext_grid <- raster::raster(raster::extent(boundaries), resolution = res)
msk <- raster::rasterize(boundaries, ext_grid, field = 1, background = NA, fun = "mean")
......
......@@ -260,7 +260,7 @@ setMethod(
setMethod(
rasterize, c("geonetwork", "SpatialPolygons"),
{
function (x, y, res = resolution(y, min_ncells = 100), ...) {
function (x, y, res = resolution(y, max_ncells = 100), ...) {
ext_grid <- raster::raster(raster::extent(y), resolution = res)
msk <- raster::rasterize(y, ext_grid, field = 1, background = NA, fun = "mean")
......
......@@ -5,7 +5,7 @@
#'
#'
#' @param x a Raster* or Spatial* object
#' @param min_ncells Number of cells for the smallest dimension
#' @param max_ncells Number of cells for the largest dimension
#'
#' @return A number. The resolution that respects the desired number of cells.
#'
......@@ -13,13 +13,13 @@
#' @export
#' @examples
#' r <- raster::raster(nrows = 5, ncols = 11, xmn = 0, xmx = 11, ymn = 0, ymx = 5)
#' resolution(r, min_ncells = 10)
resolution <- function(x, min_ncells = 100) {
#' resolution(r, max_ncells = 10)
resolution <- function(x, max_ncells = 100) {
ext <- raster::extent(x)
span_x <- raster::xmax(x) - raster::xmin(x)
span_y <- raster::ymax(x) - raster::ymin(x)
min_span <- min(span_x, span_y)
res <- min_span/min_ncells
max_span <- max(span_x, span_y)
res <- max_span/max_ncells
return(res)
}
......@@ -4,7 +4,7 @@
\alias{distance_map}
\title{Compute a distance map to spatial features}
\usage{
distance_map(x, boundaries, res = resolution(boundaries, min_ncells =
distance_map(x, boundaries, res = resolution(boundaries, max_ncells =
100))
}
\arguments{
......
......@@ -10,7 +10,7 @@
\S4method{rasterize}{geonetwork,Raster}(x, y, field = "importance", ...)
\S4method{rasterize}{geonetwork,SpatialPolygons}(x, y,
res = resolution(y, min_ncells = 100), ...)
res = resolution(y, max_ncells = 100), ...)
}
\arguments{
\item{x}{a geographic network (a geonetwork object with node attributes
......
......@@ -4,12 +4,12 @@
\alias{resolution}
\title{Compute a suitable resolution for a map}
\usage{
resolution(x, min_ncells = 100)
resolution(x, max_ncells = 100)
}
\arguments{
\item{x}{a Raster* or Spatial* object}
\item{min_ncells}{Number of cells for the smallest dimension}
\item{max_ncells}{Number of cells for the largest dimension}
}
\value{
A number. The resolution that respects the desired number of cells.
......@@ -20,5 +20,5 @@ criteria.
}
\examples{
r <- raster::raster(nrows = 5, ncols = 11, xmn = 0, xmx = 11, ymn = 0, ymx = 5)
resolution(r, min_ncells = 10)
resolution(r, max_ncells = 10)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment