1

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)))
7
  • Does it have to be with stars and sf? There's the package fasterize or the gdal command polygonize, which could be easier solutions
    – JonasV
    Oct 8, 2020 at 19:06
  • Thank you for your comment @JonasV. I understand that fasterize converts polygon to raster, while what I am looking for is a method to do the opposite - raster to polygon. I am aware of the absurdity of this given the impact in memory use and loss of efficiency provided by raster, but this is an intermediate step in a larger process which I am happy to discuss if you think we can come up with a better method. Oct 8, 2020 at 19:10
  • There is GDAL utility gdal_polygonize gdal.org/programs/gdal_polygonize.html but PostGIS raster postgis.net/docs/RT_reference.html could be a good alternative.
    – user30184
    Oct 8, 2020 at 19:35
  • @user30184 can I run gdal_polygonize or postgis in R? Or do I need python / the osgeo shell in qgis? I am not super familiar with these but they seem like an option indeed. Oct 8, 2020 at 19:55
  • Sorry, I know that R exists but not much more.
    – user30184
    Oct 8, 2020 at 19:57

1 Answer 1

2

The stars package (latest version) has it's own version of st_as_sf with two parameters that might help here: merge = TRUE and as_points = FALSE. So you might try:

stars::st_as_sf(landcover_stars, merge = TRUE, as_points = FALSE)

BTW, your example with random distribution of values will not be so helpful since I expect that there are very few clusters of equal values, as you might expect in a real landcover raster. There will be almost 144 million separate tiny polygons.

1
  • Thank you for your answer Micha - unfortunately, the real challenge is that I actually need to keep the polygon as 144M separate polygonized grid cells. But this might be what is giving me memory usage problems. Is it possible to have an sf object with this many entities without it being fully loaded in memory? Oct 9, 2020 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.