如何堆叠 R 中范围几乎完全相同的栅格
How can I stack rasters that have nearly exactly the same extent in R
我正在尝试堆叠多波段栅格。它们是通过 ESA Sentinel 1 预处理工具 SNAP 创建的。每个 tif 文件都必须分层。
我加载了两个光栅堆栈并尝试堆叠它们:
rs1 <- raster::stack("example/rs1.tif")
rs2 <- raster::stack("example/rs2.tif")
rsstack <- stack(rs1,rs2)
然后我收到以下错误信息:
Error in compareRaster(x) : different extent
栅格堆栈的范围几乎相同:
> rs1
class : RasterBrick
dimensions : 2273, 2100, 4773300, 2 (nrow, ncol, ncell, nlayers)
resolution : 8.983153e-05, 8.983153e-05 (x, y)
extent : 8.183134, 8.37178, 48.49076, 48.69495 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : S1A_IW_GRDH_1SDV_20180110T053421_20180110T053446_020088_0223E7_69E7_10.1, S1A_IW_GRDH_1SDV_20180110T053421_20180110T053446_020088_0223E7_69E7_10.2
min values : 1.729380e-07, 1.077101e-06
max values : 11.63158, 109.76797
> rs2
class : RasterBrick
dimensions : 2273, 2100, 4773300, 2 (nrow, ncol, ncell, nlayers)
resolution : 8.983153e-05, 8.983153e-05 (x, y)
extent : 8.183171, 8.371817, 48.49071, 48.6949 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : S1A_IW_GRDH_1SDV_20180106T171534_20180106T171559_020037_02223E_CE2A_10.1, S1A_IW_GRDH_1SDV_20180106T171534_20180106T171559_020037_02223E_CE2A_10.2
min values : 8.244981e-08, 5.691331e-06
max values : 6.012002, 64.965996
如何将两者叠加在一起?我怎样才能将范围调整到另一个?
我尝试了什么:
我也有一个感兴趣的领域。所以我尝试将两个堆栈裁剪到那个感兴趣的区域并尝试再次堆叠它们:
shp <- readOGR(dsn=path.expand(example/area.shp)))
shp <- sp::spTransform(shp, CRS(proj4string(rs[[1]])))
rs1 <- raster::crop(rs1,shp)
rs2 <- raster::crop(rs2,shp)
rsstack <- stack(rsstack,r2)
Error in compareRaster(x) : different extent
shp
class : SpatialPolygonsDataFrame
features : 16
extent : 8.183144, 8.371817, 48.49075, 48.69491 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
variables : 14
names : fid, Area, BoundLen, CentX, CentY, AreaIncI, BoundNotIn, CentXNotIn, CentYNotIn, PointInPol, PointInPo1, CompactRat, CompactRa1, rast
min values : 1, 1440.64063, 168.67161, 3442255.57469, 5377418.66407, 1440.64063, 168.67161, 3442255.57469, 5377418.66407, 3442259.72286, 5377409.4208, 1.14669, 1.14669, 1
max values : 16, 76089100.06641, 89693.52095, 3451427.74745, 5393682.39749, 76858585.26953, 76662.07157, 3451427.74745, 5393682.39749, 3451416.96957, 5393687.75063, 2.57399, 2.90065, 1
有人有解决办法吗?
我可以看到您的栅格具有相同的坐标系 (crs : +proj=longlat +datum=WGS84 +no_defs) 只是范围不同。首先,你可以裁剪它,然后你可以使用 raster
包中的 resample
函数,比如
library(raster)
#Crop the raster
rs1 <- raster::crop(rs1,shp)
rs2 <- raster::crop(rs2,shp)
#Conversion of rasters into same extent
rs2_resampled <- resample(rs2, rs1, method='bilinear')
#Stack the rasters
rsstack <- stack(rs1,rs2_resampled)
我想这很容易实现。让我们试试:
library(caret)
setwd("Your File Path")
# Open your reference Raster
Reference_Raster <- list.files(paste(getwd()), pattern = "Reference.tif$")
Reference_Raster <- raster(Reference_Raster)
# Open the other raster
Raster <- list.files(paste(getwd()), pattern = "Raster.tif$")
Raster <- raster(Raster)
# Now Let's Resample
Raster <- resample(Raster , Reference_Raster, method = "ngb")
# Let's Check the extent of both rasters, They are supposed to be the same.
extent(Raster)
extent(Reference_Raster)
我正在尝试堆叠多波段栅格。它们是通过 ESA Sentinel 1 预处理工具 SNAP 创建的。每个 tif 文件都必须分层。
我加载了两个光栅堆栈并尝试堆叠它们:
rs1 <- raster::stack("example/rs1.tif")
rs2 <- raster::stack("example/rs2.tif")
rsstack <- stack(rs1,rs2)
然后我收到以下错误信息:
Error in compareRaster(x) : different extent
栅格堆栈的范围几乎相同:
> rs1
class : RasterBrick
dimensions : 2273, 2100, 4773300, 2 (nrow, ncol, ncell, nlayers)
resolution : 8.983153e-05, 8.983153e-05 (x, y)
extent : 8.183134, 8.37178, 48.49076, 48.69495 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : S1A_IW_GRDH_1SDV_20180110T053421_20180110T053446_020088_0223E7_69E7_10.1, S1A_IW_GRDH_1SDV_20180110T053421_20180110T053446_020088_0223E7_69E7_10.2
min values : 1.729380e-07, 1.077101e-06
max values : 11.63158, 109.76797
> rs2
class : RasterBrick
dimensions : 2273, 2100, 4773300, 2 (nrow, ncol, ncell, nlayers)
resolution : 8.983153e-05, 8.983153e-05 (x, y)
extent : 8.183171, 8.371817, 48.49071, 48.6949 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : S1A_IW_GRDH_1SDV_20180106T171534_20180106T171559_020037_02223E_CE2A_10.1, S1A_IW_GRDH_1SDV_20180106T171534_20180106T171559_020037_02223E_CE2A_10.2
min values : 8.244981e-08, 5.691331e-06
max values : 6.012002, 64.965996
如何将两者叠加在一起?我怎样才能将范围调整到另一个?
我尝试了什么: 我也有一个感兴趣的领域。所以我尝试将两个堆栈裁剪到那个感兴趣的区域并尝试再次堆叠它们:
shp <- readOGR(dsn=path.expand(example/area.shp)))
shp <- sp::spTransform(shp, CRS(proj4string(rs[[1]])))
rs1 <- raster::crop(rs1,shp)
rs2 <- raster::crop(rs2,shp)
rsstack <- stack(rsstack,r2)
Error in compareRaster(x) : different extent
shp
class : SpatialPolygonsDataFrame
features : 16
extent : 8.183144, 8.371817, 48.49075, 48.69491 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
variables : 14
names : fid, Area, BoundLen, CentX, CentY, AreaIncI, BoundNotIn, CentXNotIn, CentYNotIn, PointInPol, PointInPo1, CompactRat, CompactRa1, rast
min values : 1, 1440.64063, 168.67161, 3442255.57469, 5377418.66407, 1440.64063, 168.67161, 3442255.57469, 5377418.66407, 3442259.72286, 5377409.4208, 1.14669, 1.14669, 1
max values : 16, 76089100.06641, 89693.52095, 3451427.74745, 5393682.39749, 76858585.26953, 76662.07157, 3451427.74745, 5393682.39749, 3451416.96957, 5393687.75063, 2.57399, 2.90065, 1
有人有解决办法吗?
我可以看到您的栅格具有相同的坐标系 (crs : +proj=longlat +datum=WGS84 +no_defs) 只是范围不同。首先,你可以裁剪它,然后你可以使用 raster
包中的 resample
函数,比如
library(raster)
#Crop the raster
rs1 <- raster::crop(rs1,shp)
rs2 <- raster::crop(rs2,shp)
#Conversion of rasters into same extent
rs2_resampled <- resample(rs2, rs1, method='bilinear')
#Stack the rasters
rsstack <- stack(rs1,rs2_resampled)
我想这很容易实现。让我们试试:
library(caret)
setwd("Your File Path")
# Open your reference Raster
Reference_Raster <- list.files(paste(getwd()), pattern = "Reference.tif$")
Reference_Raster <- raster(Reference_Raster)
# Open the other raster
Raster <- list.files(paste(getwd()), pattern = "Raster.tif$")
Raster <- raster(Raster)
# Now Let's Resample
Raster <- resample(Raster , Reference_Raster, method = "ngb")
# Let's Check the extent of both rasters, They are supposed to be the same.
extent(Raster)
extent(Reference_Raster)