terra icon indicating copy to clipboard operation
terra copied to clipboard

as.polygons() on large raster returns an empty SpatVector with no warning with aggregate=T, if aggregate=F it returns error

Open mschmidty opened this issue 1 year ago • 3 comments

When using as.polygons on a large raster, an empty SpatVector is returned if aggregate=TRUE. Here is a reproducible example:

r <- rast(ncol=10000, nrow=10000, xmin=-1000, xmax=1000, ymin=-100, ymax=900)

values(r)<-runif(ncell(r))

r$> r
class       : SpatRaster
dimensions  : 10000, 10000, 1  (nrow, ncol, nlyr)
resolution  : 0.2, 0.1  (x, y)
extent      : -1000, 1000, -100, 900  (xmin, xmax, ymin, ymax)
coord. ref. :
source      : spat_24b06688761a_9392.tif
name        : lyr.1
min value   :     1
max value   :     1 
r$>as.polygons(r)

class       : SpatVector
geometry    : none
dimensions  : 0, 0  (geometries, attributes)      
extent      : 0, 0, 0, 0  (xmin, xmax, ymin, ymax)
coord. ref. :
r$> as.polygons(r, aggregate=F)
Error: [as.polygons] the raster is too large

I would expect that the aggregate=T example should return an error as well if it just returns an empty SpatVector.

mschmidty avatar May 03 '24 15:05 mschmidty

I've had the same exact issue just now, but for some reason it produces a polygon when I restart R and run the code but not if I rerun the code within the same R session.

oshuwilson avatar Jul 30 '24 13:07 oshuwilson

Also ran into the same issue and it was solved by restarting the R session. May be some kind of default memory limit?

mvevans89 avatar Aug 13 '24 13:08 mvevans89

Same here, the issue seems to resolve after restarting R but confusing for a while!

richardneilbelcher avatar Oct 16 '24 10:10 richardneilbelcher

I believe this now works. At least it works for me (terra 1.8-5, on windows), and not only in a fresh session.

rhijmans avatar Dec 18 '24 05:12 rhijmans