0

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))
Salvador
  • 1,229
  • 1
  • 11
  • 19

1 Answers1

0

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")
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • I was given a gdb file that was converted from raster to gdb file using ArcGIS. That gdb file has a gridcode field that I need to perform some depth calculations on my R script. Following your advice about not converting raster to polygons, how can I get the gridcode field from the raster? – Salvador Apr 28 '23 at 17:11
  • Can you expand your question (create a small example) or ask a new question? Why do you need that "grid-code"? Most likely you do not. I have added some code that shows how to get cell numbers from a raster. – Robert Hijmans Apr 28 '23 at 17:19
  • Ok @Robert Hijmans, I just need one last clarification, is the raster cell number the same as the gridcode value after is converted to gdb? On the gdb file that I was given, they performed calculations such as: `gridcode*(10-2), gridcode/7.5` etc. – Salvador Apr 28 '23 at 17:52
  • In that case what you call "gridcode" is just the raster cell value. And you can do `r / 10` etc. Or use a function like `app`. That is, work with the raster data directly, that is much more efficient. – Robert Hijmans Apr 28 '23 at 18:38