繁体   English   中英

栅格类未出现在图例中

[英]Raster classes not appearing in legend

我创建了这个光栅.tiff文件。 它可以从这里获得。

我通过这段代码创建了countieslandcover.tiff

countieslandcover <- aggregate(landcover_countiesmap, fact = 3, fun = min, expand = F, na.rm = T)

其中landcover_countiesmap是在下采样之前具有 13988 行和 16304 列的初始栅格。 当我尝试使用“tmap”绘制这个初始栅格landcover_countiesmap时,出现错误:

Warning in fetch(.x, ..., downsample = downsample) :
  with RasterIO defined, argument downsample is ignored
stars_proxy object shown at 16304 by 13998 cells.
Error: cannot allocate vector of size 870.6 Mb
Error during wrapup: cannot allocate vector of size 870.6 Mb
Error: no more error handlers available (recursive errors?); invoking 'abort' restart

这是我决定使用aggregate function 将栅格下采样到countieslandcover.tiff的时候。这样做后,可以使用tmap绘制栅格。 然而,问题是我的价值观并没有清楚地出现在图例上。 它们不是“1”、“2”、“3”等,而是显示为“1 到 2”、“2 到 3”,如下图所示。

图例值应该清楚地出现,而不是成对出现

这是countieslandcover.tiff的栅格元数据,是使用aggregate function 使其变得更粗糙的结果。

> class      : RasterLayer 
> dimensions : 4666, 5434, 25355044  (nrow, ncol, ncell)
> resolution : 0.0005555558, 0.0005555558  (x, y)
> extent     : 34.34335, 37.36224, -1.442097, 1.150127  (xmin, xmax, ymin, ymax)
> crs        : +proj=longlat +datum=WGS84 +no_defs 
> source     : r_tmp_2022-04-19_212648_10928_21780.grd 
> names      : Kenya_Sentinel2_LULC2016 
> values     : 1, 10  (min, max)

这是我用tmap绘制栅格时使用的代码

tm_shape(countieslandcover) + tm_raster(palette = terrain.colors(10, 0.7, rev = F), n= 10, legend.show = T, legend.is.portrait = T, colorNA = NULL) + 
  tm_layout(title = 'Landcover types of top 5 counties by population', legend.position = c('left', 'bottom'))

我怎样才能使values在 map 和图例上都显得与众不同(如分开)? 我的values代表土地覆盖类别,如果每个类别在图例中独立出现(即“1”、“2”等),两个土地覆盖不能存在于同一个地方,这将是有意义的。 我尝试将光栅重新投影到本地基准+init=epsg:32737并且还尝试使用tm_raster arguments 无济于事。

示例分类栅格

library(terra)
set.seed(0)
r <- rast(nrows=10, ncols=10)
values(r) <- sample(3, ncell(r), replace=TRUE) - 1
cls <- c("forest", "water", "urban")
levels(x) <- cls
names(x) <- "land cover"
plot(x)

聚合这个的标准方法是使用“模态”function

a <- aggregate(x, 2, "modal")
plot(a)

但是你使用了min function,这可能毫无意义,然后你放松了水平

b <- aggregate(x, 2, "min")
plot(b)

但你可以恢复类别

levels(b) <- levels(x)
plot(b)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM