Search code examples
rpolygonrasterr-raster

Trouble converting a large raster to polygon using R


I have a rather large raster (384 MB) that I am trying to convert to a polygon shapefile in R. The rastertoPolygons function from the raster package doesn't seem to be able to handle this, as I tried running it but gave up after it was going for 7+ hours.

I also tried to use gdal_polygonize.py from GDAL in python via this function by John Baumgartner but after letting the function run for 30+ minutes I still have nothing. Am I simply not letting it run long enough? I was under the impression that gdal_polyonize.py was supposed to be very quick, i.e. seconds.

Here's a link to my raster file.

Any guidance would be greatly appreciated.


Solution

  • terra does this much faster than raster (but not faster than GDAL because that is what it uses)

    library(terra)
    r <- rast("top6loss.tif")
    

    Note that you have 22 billion cells (that is a lot by most standards, and that is why it takes a while):

    ncell(r)
    #[1] 21989436765
    

    It finishes in 10 minutes on my laptop

    system.time(p <- as.polygons(r))
    #   user  system elapsed 
    # 562.34    3.54  568.77 
    
    p
    #class       : SpatVector 
    #geometry    : polygons 
    #dimensions  : 6, 1  (geometries, attributes)
    #extent      : -13.54777, 12.33558, -6.134633, 9.781491  (xmin, xmax, ymin, ymax)
    #coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
    

    These are the 6 values

    as.data.frame(p)
    #  top6loss
    #1     2254
    #2     5418
    #3    13623
    #4    14344
    #5    15885
    #6    19654
    

    You can save the file with

    writeVector(p, "cells.shp")