首页 > 解决方案 > 如何裁剪栅格并更改其像元大小和范围以匹配模板栅格?

问题描述

我有一组生物气候栅格,我想对其进行裁剪和更改以匹配我的模板栅格。这意味着更改投影、像元大小(模板栅格具有更大的像元大小)范围和 NA 像元,以便模板栅格上的每个非 NA 像元在生物气候栅格上具有匹配的像元。

我不想更改任何单元格值(放大单元格大小的必要更改除外)。

任何人都可以建议 R 中的工作流程吗?我查看了“光栅”包,但无法找出我 100% 确定的工作流程

标签: r

解决方案


我认为您所追求的是包中的resample()功能raster。这是执行此任务的工作流的可重现示例。第一个代码块主要用于设置数据以实现可重复性并获得您所询问的特定场景:

##Loading Necessary Packages##
library(raster)
library(tmap)
library(sf)
library(sp)
library(spData)

##Getting Example Datasets##
data("land")
data("us_states")

##Converting the Elevation layer in the "land" dataset to a rasterLayer##
Elev<-raster(xmx=180, xmn=-180, ymx=90, ymn=-90, t(land$elevation), crs=crs("+init=epsg:4326"))

##Looking at the dataset##
tmap_mode("plot")
tm_shape(Elev)+
  tm_raster(palette = terrain.colors(10))+
  tm_layout(legend.outside = TRUE)

##Getting necessary projections for spatial transformations##
WGS84<-CRS("+init=epsg:4326")#World Geographical System of 1984
ORLAMB<-CRS("+init=epsg:2992")#Oregon Statewide Lambert Projection

##Creating a fake raster for the US state of Oregon##
OR<-as_Spatial(us_states[us_states$NAME=="Oregon",])
OR_sp<-spTransform(OR, CRSobj=WGS84)
OR_lamb<-spTransform(OR, CRSobj=ORLAMB)
EXT<-extent(OR_lamb)
OR_blank<-raster(EXT, resolution=c(3000,3000), crs=ORLAMB)
values(OR_blank)<-runif(369238, 100, 200)

现在,在我们将大型世界范围的高程数据集投影到 WGS84 以及美国俄勒冈州的更高分辨率的假栅格之后,我们有了特定的场景。以下部分是您询问的工作流程的主要部分

OR_ELEV_tmp<-crop(Elev, OR_sp) #Crop the worldwide elevation data to the state of ORegon
ORELEVLAMB<-projectRaster(OR_ELEV_tmp, crs=ORLAMB)#Project into Oregon Statewide Lambert
ELEV_rsmpld<-mask(resample(ORELEVLAMB, OR_blank),OR_lamb)#Resample to the resolution of our fake Oregon raster and mask to the shape of the state

##View the final output
tmap_mode("plot")
tm_shape(ELEV_rsmpld)+
  tm_raster(palette = terrain.colors(10))+
  tm_layout(legend.outside = TRUE)


推荐阅读