繁体   English   中英

plot shapefile 在多面板栅格上使用 terra

[英]plot shapefile over multipanel raster using terra

我有四个栅格和一个 shapefile。 我想 plot shapefile 在单个面板中的 4 个栅格之上

  library(terra)
  r1 <- r2 <- r3 <- r4 <- rast(ncol=10, nrow=10, xmin=-150, xmax=-80, ymin=20, ymax=60)
  
  values(r1) <- runif(ncell(r1))
  values(r2) <- runif(ncell(r2))
  values(r3) <- runif(ncell(r3))
  values(r4) <- runif(ncell(r4))

  rr <- c(r1, r2, r3, r4)
  
  # shapefile 
  lon <- c(-116.8, -114.2, -112.9, -111.9, -114.2, -115.4, -117.7)
  lat <- c(41.3, 42.9, 42.4, 39.8, 37.6, 38.3, 37.6)
  pols <- vect(lonlat, type="polygons", crs="+proj=longlat +datum=WGS84")
  
  terra::plot(rr)      
  terra::plot(pols, add = T)      

但是,shapefile 并没有被添加到栅格中,而是被绘制在中间。

在此处输入图像描述

您的示例数据

library(terra)
r <- rast(ncol=10, nrow=10, nlyr=4, xmin=-150, xmax=-80, ymin=20, ymax=60) 
values(r) <- 1:size(r)
# polygons
lon <- c(-116.8, -114.2, -112.9, -111.9, -114.2, -115.4, -117.7)
lat <- c(41.3, 42.9, 42.4, 39.8, 37.6, 38.3, 37.6)
pols <- vect(cbind(lon, lat), type="polygons", crs="+proj=longlat +datum=WGS84")
  

解决方案

plot(r, fun=function()lines(pols))

暂无
暂无

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

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