2

我正在尝试使用带有全彩色 RGB、蒙面 Landsat 图像的库(tmap)在 R 中创建地图。然而,NA 显示为黑色。这就是我所做的。

使用 library(sf) 我计算了 5 个多边形的质心并将它们缓冲 5000m。在此之后,我使用 library(raster) 通过缓冲质心来掩盖 Landsat 图像。代码看起来像这样并且运行良好。

# Read in the data
polys <- st_read("nybb.shp")
rast <- brick("LC08_L1TP_013032_20171027_20171027_01_RT.tif")

# Transform polys, crop raster, calculate centroids
polys <- st_transform(polys, crs = crs(rast, asText = TRUE))
rast <- crop(rast, as(polys, "Spatial"))
cent <- st_centroid(polys) %>% st_buffer(., 5000) %>% as(., "Spatial")

# Mask the raster using buffered centroids
r <- mask(rast, cent)

我可以使用 base R 和 library(raster) 完成我想要的——但我更愿意使用 tmap 来完成。

# Code that works
plot(polys$geometry, col = "grey", border = "white")
plotRGB(r, bgalpha = 0, add = TRUE) 


# Code that does not work
# The NAs in the masked raster appear as black
# even when using the colorNA argument
tm_shape(polys) + tm_polygons() + 
  tm_shape(r, bbox = polys) + 
  tm_rgb(colorNA = "red")

知道如何使用 tmap 的 tm_rgb() 函数显示蒙版栅格而不将 NA 显示为黑色吗?

4

1 回答 1

0

要使用 tmap 创建底图,您可以使用包中的read_osm function,tmaptools如下所示。请注意,您必须首先将数据转换为地理 CRS:epsg=4326

library(tmaptools)
library(OpenStreetMap)

rast <-projectRaster(rast,crs="+init=epsg:4326",method = "ngb") #(you can use method=method="bilinear") 

polys <- spTransform(polys, CRS("+init=epsg:4326"))

background <- read_osm(bbox(rast))

tm_shape(background) + tm_raster() +
tm_shape(polys) + tm_polygons() + 
tm_shape(r, bbox = polys) + 
tm_rgb(colorNA = "red")
于 2019-05-16T13:19:17.677 回答