2

My ultimate goal is to convert landcover raster (.tif) objects to an sf object representing the raster's grid and the original values of each cell within each geometry. I have been able to do this for smaller rasters doing the following:

library(sf) 
library(stars)

# import raster using stars
landcover_stars <- read_stars(my_raster.tif)

# convert to sf object using st_as_sf
landcover_grid_sf <- st_as_sf(landcover_stars)

In larger rasters (e.g. my largest raster is currently 11482x12607 cells), however, the read_stars() function imports the raster as a "stars proxy", which is a step taken to handle large raster datasets by the package. While stars proxy objects are not accepted by the st_as_sf function, it is possible to set "proxy = FALSE" in the function. If I do this in my largest dataset, however, running st_as_sf(landcover_stars) with the resulting object will crash my laptop {16 GB RAM, i7 2.70GHz processor}.

Is there a way I can proceed to ease the load on my machine when converting very large star objects to sf?

In addition - could it be that it is actually the newly generated sf object what is depleting my machine?

Here is a dummy raster in case youd like to test it, with integer values randomly generated ranging from 1 to 10:

raster(nrows=12000, ncols=12000, xmn=0, xmx=10, vals = floor(runif(12000*12000, min=0, max=11)))
  • 1
    The resulting `sf` layer from the 12000*12000 raster is not going to fit in 16 RAM. For example, when I convert just the first 1000 raster rows (`r[,1:1000,,]`) to `sf`, the object size is about 9 GB! You could run a loop to convert just 1000 rows to polygons each time, say saving the result to a Shapefile. But you will not be able to load all parts into R at once. So I wonder what do you need to do with the polygons? Can this be done in parts, or do you need them loaded in R all together? – Michael Dorman Nov 05 '20 at 22:34
  • 1
    Thank you for your comment @MichaelDorman. I know - it sounds like an overkill. As for now, we are using this method to generate a gridded .shp in which each cells represents an area with green land cover. This method aims to bypass a limitation that a model we are using for runoff modelling has, which is that it is unable to handle geometries with holes in them. By reducing each geometry to a set of cells with the same resolution as the DEM used by the model, we can introduce the green areas, but it leads to a HUGE increase in the size of the layer. – Pablo Herreros Cantis Nov 09 '20 at 18:02
  • 1
    I am thinking about opening a new question that actually asks for advice in handling entities with holes in them. If I could automatize a way to cut through geometries with holes in them, then this step would not be required. – Pablo Herreros Cantis Nov 09 '20 at 18:04

0 Answers0