【问题标题】:How do i get mapview to plot my raster image with the same colors as plotRGB or plot我如何让 mapview 用与 plotRGB 或 plot 相同的颜色绘制我的光栅图像
【发布时间】:2021-04-30 17:10:37
【问题描述】:

我有一个栅格file,已从 ArcGIS 导出为地理参考 tif 文件。 栅格将用作 mapview 中的背景地图,因为 mapview 背景层 CartoDB.Positron、CartoDB.DarkMatter、OpenStreetMap、Esri.WorldImagery 和 OpenTopoMapdont 在我需要的缩放级别上不提供所需的背景。

首先我使用 raster 包将我的光栅砖读入 R,然后使用 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') 

结果如预期

然后我创建一个值为 0 - 255 的新栅格 r2,并根据 r 中的 rgb 值分配一个颜色表。我用两种不同的方式绘制它,我得到了相同的结果。

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)

这是结果,它好多了

但是,当我尝试使用 mapview 时,结果并不如预期。

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

谁能帮我用 mapview 解决这个问题? 我已经查看了this,但我无法找到解决方案。

【问题讨论】:

  • 很遗憾,我们无法重现您的示例,因为我们没有 pct
  • 我忘记了几行 pct

标签: r r-raster r-leaflet r-mapview


【解决方案1】:

您可以简单地使用mapview::viewRGB()

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

【讨论】:

  • 使用viewRGB 得到Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'extent': NA。我之前曾尝试过,这就是为什么我决定按照上面的方式编写代码。
  • remotes::install_github("r-spatial/mapview")了吗?
  • 这不起作用。 Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'extent': NA In addition: Warning messages: 1: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) : Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs 2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) : Discarded datum World Geodetic System 1984 in Proj4 definition
  • 我认为最好在github.com/r-spatial/mapview 上打开一个问题。您可以事先尝试的最后一件事是remotes::install_github("r-spatial/leafem"),看看是否能解决问题。如果没有,请在 github 上打开一个问题。
猜你喜欢
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2019-01-23
  • 2019-05-16
  • 2015-12-04
  • 1970-01-01
相关资源
最近更新 更多