【问题标题】:How to keep same extent?如何保持相同的程度?
【发布时间】:2020-12-01 11:00:04
【问题描述】:

我有 10 个光栅文件。我想把它们堆叠起来。如何保持这 10 个文件的范围相同?

library(raster)

rasterfile <- list.files(file.path("E:/NDVI RESULT FINAL"), full.names = T, pattern = '.tif$')

f1 <- "E:/NDVI RESULT FINAL/NDVI converted 2009.tif"
r1 <- raster(f1)
f2 <- "E:/NDVI RESULT FINAL/NDVI converted 2010.tif"
r2 <- raster(f2)
f3 <- "E:/NDVI RESULT FINAL/NDVI converted 2011.tif"
r3 <- raster(f3)
# and so on

extent(rasterf1) 
#class : Extent xmin : 72.23081 xmax : 77.79537 ymin : 34.26277 ymax : 37.35659 
extent(rasterf2)
#class : Extent xmin : 72.51037 xmax : 77.68996 ymin : 34.51186 ymax : 37.098
extent(rasterf3) 
#class : Extent xmin : 72.2514 xmax : 77.94309 ymin : 34.20765 ymax : 37.40778
extent(rasterf4)
#class : Extent xmin : 72.23081 xmax : 77.79537 ymin : 34.26277 ymax : 37.35659

等等

【问题讨论】:

  • 我简化了您的代码,但您能否编辑您的问题以显示您有什么或什么不起作用?
  • 先生,我告诉过你很多次,范围是不同的。当我堆叠它们时,它会说不同的程度。上面提到的是它们的范围。我应该如何将它们以相同的范围堆叠在一起。?
  • 当您提出问题时,您应该显示什么不起作用,也许是为什么。我已经在我的回答中做到了这一点——并且我提供了一个解决方案。

标签: r raster projection extent


【解决方案1】:

通常情况下,你会这样做

ff <- list.files(file.path("E:/NDVI RESULT FINAL"), full.names = T, pattern = '.tif$')
s <- stack(ff)

这不起作用吗?如果是这样,您可以执行以下操作来查看范围是什么。

# example data 
f <- (system.file("external/rlogo.grd", package="raster")) 
ff <- c(f, f, f)

x <- lapply(ff, raster)
t(sapply(x, function(i) as.vector(extent(i))))
#    [,1] [,2] [,3] [,4]
#[1,]    0  101    0   77
#[2,]    0  101    0   77
#[3,]    0  101    0   77

稍后:

您现在已经表明所有范围都不同。第一个要问的问题是为什么?这些文件是如何生成的?它们可能应该重新生成,以便它们具有相同的程度。您也可以使用重采样,但这样会有一些数据质量损失。

示例数据

library(raster)
r1 <- raster(ncol=10, nrow=10, xmn=0, xmx=1, ymn=0, ymx=1, vals=1:100)
r2 <- shift(r1, .5)
r3 <- shift(r1, .5, .5)
r4 <- shift(r1, 0, -.5)

问题

stack(r1, r2)
#Error in compareRaster(x) : different extent

解决方案

rsts <- list(r1, r2, r3, r4)
for (i in 2:length(rsts)) {
     rsts[[i]] <- resample(rsts[[i]], r1)
} 
s <- stack(rsts)
s
#class      : RasterStack 
#dimensions : 10, 10, 100, 4  (nrow, ncol, ncell, nlayers)
#resolution : 0.1, 0.1  (x, y)
#extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs 
#names      : layer.1, layer.2, layer.3, layer.4 
#min values :       1,       1,      51,       1 
#max values :     100,      95,      95,      50 

【讨论】:

  • 感谢罗伯特的友好回复。它说 Error in compare Raster(rasters) : different extent.
  • 示例数据 f
  • 在这种情况下我有 10 个栅格,我将如何制作相同的范围?你能帮我写吗
  • 您需要提供更多信息以便其他人能够帮助您。例如,显示栅格的范围 --- 我向您展示了如何做到这一点。
  • >范围(raster1a)类:范围xmin:72.23081 xmin:77.79537 ymin:34.26277 Ymax:37.35659>范围(raster2a)类:范围xmin:72.51037 xmax:77.68996 ymin:34.51186 Ymax:37.098>范围(raster3a)类:范围xmin:72.2514 xmax:77.94309 ymin:34.20765 ymax:37.40778 > >范围(raster4a)类:范围xmin:72.23081 xmax:77.79537 ymin:34.26277 ymax我的范围:3.735659
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 2013-09-11
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多