Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

projectRasterForLeaflet should support method="ngb" #219

Closed
adrfantini opened this issue Dec 11, 2015 · 1 comment
Closed

projectRasterForLeaflet should support method="ngb" #219

adrfantini opened this issue Dec 11, 2015 · 1 comment
Milestone

Comments

@adrfantini
Copy link

adrfantini commented Dec 11, 2015

The raster package supports nearest neighbour projection in projectRaster, by issuing method="ngb". This should be possible in projectRasterForLeaflet too - it's useful for a number of different datasets.

@tim-salabim
Copy link
Contributor

I totally agree that there needs to be an option for re-projecting factorial rasters using nearest neighbor. The linear interpolation of the bilinear re-projection (the default in raster::projectRaster) produces floating point values which, in addition to being false values for factorial raster layers, also mess up the legend produced with leaflet::colorFactor.

In mapview we actually do the raster projection outside the leaflet::addRasterImage call like this (lines 178 - 187 in https://github.com/environmentalinformatics-marburg/mapview/blob/master/R/leafletControls.R):

is.fact <- raster::is.factor(x)[1]
if (is.fact) {
    x <- raster::projectRaster(
      x, raster::projectExtent(x, crs = sp::CRS(wmcrs)),
      method = "ngb")
    x <- raster::as.factor(x)
  } else {
    x <- raster::projectRaster(
      x, raster::projectExtent(x, crs = sp::CRS(wmcrs)),
      method = "bilinear")
  }

The user still needs to make sure that their raster is of class factor (as having integer values does not necessary mean factorial, hence the explicit call to raster::as.factor after the re-projection), but that can be easily done with raster::as.factor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants