在R中将遮罩应用于空间栅格?

时间:2020-04-03 05:04:29

标签: r raster sf sp tmap

我想使用代表区域边界的shapefile遮罩我的背景图。为此,我已使用read_osm

将空间栅格读入Rstudio。
library(sp)
library(tmaptools)
HB_map <- spData::nz %>% 
  filter(Name=="Hawke's Bay") %>% 
  tmaptools::read_osm(type = "stamen-terrain")

然后我导入了我的shapefile

libary(sf)
Regional_boundary <- sf::st_read("regional_boundary.shp")
sf::st_crs(Regional_boundary)= 2193 
Regional_boundary_sf_poly <- sf::st_transform(Regional_boundary, 27200) %>% 
  sf::st_cast(to="POLYGON")

我有许多GIS数据集,所以我重新投影了栅格,使其与GIS数据处于同一投影中(我不确定这是否正确)

test_map <- projectRaster(HB_map, crs ="+init=epsg:27200")

然后我检查数据预测是否一致

crs(Regional_boundary_sf_poly)

[1]“ + proj = nzmg + lat_0 = -41 + lon_0 = 173 + x_0 = 2510000 + y_0 = 6023150 + ellps = intl + towgs84 = 59.47,-5.04,187.44,0.47,-0.1,1.024,- 4.5993 + units = m + no_defs“

crs(test_map)

+ init = epsg:27200 + proj = nzmg + lat_0 = -41 + lon_0 = 173 + x_0 = 2510000 + y_0 = 6023150 + datum = nzgd49 + units = m + no_defs + ellps = intl + towgs84 = 59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993

现在我戴上口罩:

Library(raster)
test_map_mask <- raster::mask(test,Regional_boundary_sf_poly,inverse=FALSE,updatevalue=NA, updadateNA=FALSE)

并检查结果

tmap::qtm(test_map_mask)

除了地图颜色不再看起来像原始的“雄蕊地形”,而是变为不同的橙色阴影之外,这一切似乎都起作用。如何调整设置以使地图看起来像原始的但带有蒙版?

感谢您的帮助。 亲切的问候, 西蒙

2 个答案:

答案 0 :(得分:1)

新答案

尝试使用cartography::getTiles来获得磁贴,这似乎可行。请注意,您应该已经安装了sf v0.9-0cartography v2.4-0,它们都是最近在 CRAN 上更新的。您可以在getTiles here上找到完整的图块列表。

library(sf)
library(cartography)
library(dplyr)

nz <- spData::nz %>% st_transform(27200)

#Get tile
HB_map <- cartography::getTiles(nz, type = "Stamen.Terrain")

class(HB_map)
#> [1] "RasterBrick"
#> attr(,"package")
#> [1] "raster"

st_crs(HB_map)$proj4string
#> [1] "+proj=nzmg +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150 +ellps=intl +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +units=m +no_defs "
st_crs(nz)$proj4string
#> [1] "+proj=nzmg +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150 +ellps=intl +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +units=m +no_defs "

library(raster)
test_map_mask <-
  raster::mask(
    HB_map,
    nz,
  )

#tmap
tmap::qtm(test_map_mask)
#> Warning: replacing previous import 'sf::st_make_valid' by
#> 'lwgeom::st_make_valid' when loading 'tmap'
#> Numeric values of test_map_mask interpreted as RGB values with max.value = 255. Run tm_shape(test_map_mask) + tm_raster() to visualize the data.

reprex package(v0.3.0)于2020-04-04创建

旧答案

您可能要检查getPngLayer中的pngLayercartography,基本上,您需要创建一个png文件作为用sf遮盖的栅格,并进行打印。 / p>

如果您的问题是绘图,pngLayer基本上是raster::plotRGB的包装。

额外的好处是,您也可以在getTiles上使用cartography来获得Stamen Terrain,但要确保安装了最新版本(今天在CRAN上发布)。

代表:

library(sf)
library(cartography)
library(dplyr)

HB_map <- spData::nz %>% getTiles(type = "Stamen.Terrain")
class(HB_map)
#> [1] "RasterBrick"
#> attr(,"package")
#> [1] "raster"

Regional_boundary <- spData::nz  
st_crs(HB_map)$proj4string
#> [1] "+proj=tmerc +lat_0=0 +lon_0=173 +k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs "
st_crs(Regional_boundary)$proj4string
#> [1] "+proj=tmerc +lat_0=0 +lon_0=173 +k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs "

library(raster)

test_map_mask <-
  raster::mask(
    HB_map,
    Regional_boundary,
  )
#> Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
#> that

par(mar=c(0,0,0,0))
tilesLayer(test_map_mask)
plot(st_geometry(Regional_boundary), border = "red", lwd=2, add = TRUE)

reprex package(v0.3.0)于2020-04-04创建

小插图:https://dieghernan.github.io/cartographyvignette/

源代码:https://github.com/riatelab/cartography/blob/master/R/tilesLayer.R

enter image description here

答案 1 :(得分:0)

感谢dieghrnan的回答,我现在可以获得一些示例数据,下面的内容对我来说很好

library(sf)
library(cartography)
library(raster) 
library(spData) 

nz <- spData::nz
HB <- cartography::getTiles(nz, type = "Stamen.Terrain")
HB_mask <- raster::mask(HB, nz)
plotRGB(HB_mask)

如果要投影:

test <- projectRaster(HB_mask, crs ="+init=epsg:27200")
# or perhaps
# test <- projectRaster(HB_mask, method="ngb", crs ="+init=epsg:27200")
plotRGB(test)

旧答案:

您在哪一步松开颜色?我的猜测是在projectRaster上?我无法使rJava正常工作,因此无法运行它,但我会尝试

colortable(test_map_mask) <- colortable(HB_map)
tmap::qtm(test_map_mask)