rarcgisr-sfterrar-stars

Convert raster to geodatabase (gdb) file


I am trying to convert a raster to geodatabase (gdb) file using R and then read this gdb file back into R. The gdb file contains a column (gridcode) that I need for other calculations within my R script. In ArcGIS this conversion can be done with the 'raster to Polygon' tool but so far I can't find a way to do the same in R. Does anyone know of an R package that can do this?

library(terra)
library(stars)
library(sf)

f <- system.file("extdata/asia.tif", package = "tidyterra")
x <- rast(f)

 gdb <- as.gdb(x)  ##dummy
 
 mygdb <- st_read('gdb', layer = "first Layer")
  st_layers()
  
  plot(st_geometry(mygdb))

Solution

  • What you want to do is a bad idea. It is very inefficient to store raster data as polygons. It is (almost?) never necessary in R. The "gridcode" is (implicitly) available for the raster. But since you do not describe your goal, it is not possible to help you further.

    You can transform raster data to polygons like this

    library(terra)
    f <- system.file("ex/elev.tif", package="terra")
    r <- rast(f)
    p <- as.polygons(r)
    

    You can write to a GDB database like this:

    writeVector(v, "test.gdb", filetype="OpenFileGDB")