简体   繁体   English

R 中两个栅格的匹配范围

[英]match extent of two rasters in R

I have two raster:我有两个栅格:

  raster1
  
  class       : SpatRaster 
  dimensions  : 21600, 43200, 1  (nrow, ncol, nlyr)
  resolution  : 0.008333333, 0.008333333  (x, y)
  extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)

  raster2
  
  class       : SpatRaster 
  dimensions  : 720, 1440, 1  (nrow, ncol, nlyr)
  resolution  : 0.25, 0.25  (x, y)
  extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
  coord. ref. : lon/lat WGS 84 

I want to run zonal stastics to calculate sum of smaller raster raster1 on bigger raster raster2 :我想运行区域统计来计算较大栅格raster1上较小栅格 raster1 的raster2

  terra::zonal(raster1, raster2, fun = sum, as.raster=T, filename = 'zonal.tif') 
  Error: [zonal] dimensions and/or extent do not match
  

I wasn't sure why the extent are not matching until I did this在我这样做之前,我不确定为什么范围不匹配

  terra::ext(raster1)
  SpatExtent : -180.000001017276, 180.000001017276, -90.0000010172997, 90.0000010172997 (xmin, xmax, ymin, ymax)
  
  terra::ext(raster2)
  SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
  

which shows that raster1 extent have some imprecision.这表明raster1范围有一些不精确性。 What are different ways I can fix this?有什么不同的方法可以解决这个问题?

EDIT: I tried the suggestion in the comment编辑:我尝试了评论中的建议

terra::crs(raster2) <- sf::st_crs(4326)$wkt
terra::crs(raster1) <- sf::st_crs(4326)$wkt

terra::ext(raster2)
SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)

terra::ext(raster1)
SpatExtent : -180.000001017276, 180.000001017276, -90.0000010172997, 90.0000010172997 (xmin, xmax, ymin, ymax)
  

But my extent are still not matching但我的程度仍然不匹配

The error message is错误信息是

Error: [zonal] dimensions and/or extent do not match错误:[区域] 尺寸和/或范围不匹配

In this case, that clearly refers to the difference is dimensions, which are在这种情况下,这显然是指差异是尺寸,它们是

#dimensions  : 21600, 43200, 1  (nrow, ncol, nlyr)
#dimensions  : 720, 1440, 1  (nrow, ncol, nlyr)

I do not think the small difference in the extent is relevant at all.我认为程度的微小差异根本不相关。

To make the dimensions match you can use disagg or aggregate with a factor of 30.要使维度匹配,您可以使用disaggaggregate系数为 30。

If the crs of raster2 is different from that of raster1 you can (in this case, where you clearly have two global lon/lat rasters) fix that with如果 raster2 的 crs 与 raster1 的 crs 不同,您可以(在这种情况下,您显然有两个全局 lon/lat 栅格)使用

crs(raster2) <- crs(raster1)

And for good measure, you could in this case also do而且,在这种情况下,您也可以这样做

ext(raster2) <- ext(raster1)

Mismatching extents don't stop zonal working, so I think that error message is wrong.不匹配的范围不会停止zonal工作,所以我认为错误消息是错误的。 Here's two rasters that only differ in their CRS:这是两个仅在 CRS 上有所不同的栅格:

> library(terra)
terra 1.5.21
> raster1 = rast()
> raster1[]=1:360
> raster2 = rast()
> raster2[]=1:360
> crs(raster1) = ""
> zonal(raster1, raster2)
Error: [zonal] dimensions and/or extent do not match

I guess it could be argued that a different CRS means a different extent in the same way that "23" is different to "23 km".我想可以说不同的CRS意味着不同的程度,就像“23”与“23 km”不同一样。 But the extent and the dimensions are all the same according to these tests:但是根据这些测试,范围和尺寸都是相同的:

> ext(raster1) == ext(raster2)
[1] TRUE
> dim(raster1) == dim(raster2)
[1] TRUE TRUE TRUE

Rasters with a different extent work fine with zonal (once I've set the CRSs to match):具有不同程度的栅格可以很好地使用zonal (一旦我将 CRS 设置为匹配):

> ext(raster2) = c(-180.01, 180.01, -90.01, 90.01)
> crs(raster2) = ""
> z = zonal(raster1, raster2)
> 

If you do want to change an extent, you can use ext(r) =... as above, but you should try and figure out why extents don't match.如果您确实想更改范围,可以使用ext(r) =...如上所述,但您应该尝试找出范围不匹配的原因。 Likely its because you have data on points and you may have cells extending out from points...可能是因为您有关于点的数据,并且您可能有从点延伸出来的单元格......

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

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