在R中使用tmap显示遮罩RGB光栅的黑色背景
我正在尝试使用带有全彩RGB掩蔽的陆地卫星图像的库(tmap)在R中创建地图。但是,NAs显示为黑色。这就是我所做的 使用库(sf),我计算了5个多边形的质心,并将它们缓冲了5000m。接下来,我使用库(光栅)通过缓冲质心遮罩陆地卫星图像。代码看起来像这样,并且工作得非常完美在R中使用tmap显示遮罩RGB光栅的黑色背景,r,mask,r-raster,tmap,R,Mask,R Raster,Tmap,我正在尝试使用带有全彩RGB掩蔽的陆地卫星图像的库(tmap)在R中创建地图。但是,NAs显示为黑色。这就是我所做的 使用库(sf),我计算了5个多边形的质心,并将它们缓冲了5000m。接下来,我使用库(光栅)通过缓冲质心遮罩陆地卫星图像。代码看起来像这样,并且工作得非常完美 # Read in the data polys <- st_read("nybb.shp") rast <- brick("LC08_L1TP_013032_20171027_20171027_01_RT.t
# 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)
您知道如何使用tmap的tm_rgb()函数显示遮罩光栅而不将NAs显示为黑色吗?要使用tmap创建底图,您可以使用
读取osm函数
,从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")
库(tmaptools)
图书馆(OpenStreetMap)
拉斯特
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")