0

I am working exclusively on the terra package.

spatRaster details: Carbon flux data of South America (~500 GB)

spatVector details: Protected Areas database of South America (~70 MB)

Goal: To compute zonal statistics

Error: While performing zonal statistics operation, the extents do not overlap.

Possible reason for error: Raster files have only the terrestrial extent. Polygons, however, extend up to marine regions, thus have terrestrial, marine, and terrestrial-marine combined extents.

Question: Is it possible to extend the raster file up to the extent of Polygon without losing any values for this much large file? Is there any efficient way to solve this kind of problem?

PS: I am new here, and if anyone finds out that the similar question is answered already, please help me out. I am sorry that I couldn’t make a reproducible example for this problem.

1 Answers1

1

There is no "'SpatRaster,SpatVector" method for zonal, so your question has no answer. I assume you use rasterize first and make a mistake there. Here is a minimal, self-contained reproducible example, that suggests that there is no need to do what you think you need to do:

library(terra)
#terra version 1.1.16
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)

# remove part of raster, such that polygons have larger extent 
r <- crop(r, v[1:5])

z <- rasterize(v, r, "NAME_1")    
zonal(r, z, mean, na.rm=TRUE)

#        NAME_1 elevation
#1     Diekirch  403.4237
#2 Grevenmacher  329.4595
#3   Luxembourg  315.2139
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thank you @robert for your response. I may have used wrong terminologies here. I followed processing routine as you specified i.e. `crop`, `rasterize` & `zonal`, it worked great for sample polygon. But testing with big file size data as I mentioned in question above, I got the extent overlapping error. Summary of my problem is that, raster have only the terrestrial extent, but polygon have terrestrial and marine both. So, extent of raster is smaller than that of polygon. Is there any way to increase the extent of raster to match with the polygon's? – Om Bhandari Apr 12 '21 at 07:39
  • My example shows that it is not necessary to have overlap. That suggests your assertion about the cause of the error is wrong (also because you do not provide any evidence for it). – Robert Hijmans Apr 12 '21 at 23:35
  • 1
    Now, I understood what you did in the example code above in the `crop` part. Thank you for your clear illustration. – Om Bhandari Apr 13 '21 at 05:55