I have a raster projected in EPSG:21781 (available here) that I'd like to display with mapview. Raster values correspond to the grid id, and need to be preserved. If I understood correctly, mapview is calling leaflet::addRasterImage() to do so, and reproject the raster into EPSG:3857 (pseudo-mercator) through raster::projectRaster(). The first problem is that the reprojection is done through a 'bilinear' method (by default) whereas I need the nearest-neighbor method. To overcome this problem, I have reprojected the raster manually using the following command:
grid <- raster::raster("~/grid100.tif")
grid2 <- projectRaster(grid,crs="+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 +wktext +no_defs",method="ngb") # corresponding to epsg 3857
summary(values(grid))
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
1 1035406 2070572 2074309 3111142 4157490 3554337
summary(values(grid2))
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
1 1056410 2097896 2093243 3133844 4157490 3917853
table(is.na(match(values(grid),values(grid2))))
FALSE TRUE
6206341 1488539
Conclusion: 1'488'539 cells are dropped after reprojection... Any idea of what is going on here? Thanks
ncell(grid)
andncell(grid2)
should still be identical.ncell(grid)
= 7694880 whilencell(grid2)
= 7751910