From 7029341bc12bd86e5e730fa92d82c0daf697e799 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 14 Jan 2016 16:25:51 -0800 Subject: [PATCH 1/2] Two key changes to address issues #224 and #219: 1. Added `method` to the options so that users can define eother `'ngb'` or `'bilinear'` options for `raster::projectExtent` to improve plotting of categorical data. (and added to `roxygen2` documentation block) 2. Do not re-project if CRS for the raster is equivalent to the current CRS. I'm not sure if this is that common an issue, but just to avoid any potential (accidental) reprojection (see issue #224). --- R/layers.R | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/R/layers.R b/R/layers.R index 84785f4d9..a41fb00a1 100644 --- a/R/layers.R +++ b/R/layers.R @@ -163,6 +163,9 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y #' coordinates #' @param maxBytes the maximum number of bytes to allow for the projected image #' (before base64 encoding); defaults to 4MB. +#' @param method the method used to interpolate to the leaflet web Mercator +#' projection. Default is 'bilinear'. 'ngd' (nearest neighbor) is better for +#' categorical variables. #' #' @examples #' library(raster) @@ -183,12 +186,13 @@ addRasterImage = function( layerId = NULL, group = NULL, project = TRUE, - maxBytes = 4*1024*1024 + maxBytes = 4*1024*1024, + method = "bilinear" ) { stopifnot(inherits(x, "RasterLayer")) if (project) { - projected <- projectRasterForLeaflet(x) + projected <- projectRasterForLeaflet(x, method) } else { projected <- x } @@ -218,8 +222,10 @@ addRasterImage = function( #' @rdname addRasterImage #' @export -projectRasterForLeaflet <- function(x) { - raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857))) +projectRasterForLeaflet <- function(x, method) { + if(!raster::crs(x) == sp::CRS(epsg3857)){ + raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857), method = 'method')) + } } #' @rdname remove From 64c0274de12ed330c325ac3f847804230321bd9d Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 15 Jan 2016 21:24:30 -0800 Subject: [PATCH 2/2] Removed the `if` comparison, it's not so simply to compare the projection strings. The `+init=epsg:3857` projection expands to: ``` "+init=epsg:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs" ``` which is not directly comparable to the variable `epsg3857`: ``` "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs" ``` --- R/layers.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/layers.R b/R/layers.R index a41fb00a1..fe2ad9c35 100644 --- a/R/layers.R +++ b/R/layers.R @@ -223,9 +223,7 @@ addRasterImage = function( #' @rdname addRasterImage #' @export projectRasterForLeaflet <- function(x, method) { - if(!raster::crs(x) == sp::CRS(epsg3857)){ - raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857), method = 'method')) - } + raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857), method = method)) } #' @rdname remove