How do i get mapview to plot my raster image with the same colors as plotRGB or plot

874 views Asked by At

I have a raster file that I have exported from ArcGIS as a georeferenced tif file. The raster will be used as a background map in mapview because the mapview background layers CartoDB.Positron, CartoDB.DarkMatter, OpenStreetMap , Esri.WorldImagery and OpenTopoMapdont dont give the required background at the zoom level i need.

First I read my raster brick into R using the raster package and then I plot using plotRGB.

library(raster)
library(mapview)
r<-brick("KYENGERA2.tif")#raster exported from Arcgis read. has 4 bands
r<-subset(r,1:3) #Retain only layers/bands with RGB
plotRGB(r, 1,2,3, stretch='lin') 

The result is as expected enter image description here

I then create a new raster r2 with values 0 - 255 and assign a colortable based on the rgb values in r. I plot it in two difefrent ways and i get the same result.

pct <- rgdal::SGDF2PCT(as(r, "SpatialGridDataFrame"))
r2 <- setValues(raster(r), pct$idx-1) #create a new raster with values 0 - 255
colortable(r2) <- pct$ct #define 256 colors
plotRGB(r, 1,2,3, stretch='lin')
plot(r2)#plot(r2,col=r2@legend@colortable)

Here is the result and it is much better enter image description here

However when I try mapview, the result is not as expected.

mapview(r2, col.regions = pct$ct,na.col="transparent")

enter image description here

Can anyone help me solve this issue with mapview? I have taken a look at this but i couldnt figure out a solution.

1

There are 1 answers

4
TimSalabim On

You can simply use mapview::viewRGB()

viewRGB(r, 1, 2, 3, method = "ngb", quantiles = c(0, 1), maxpixels = ncell(r))