2

Does the terra package supports the exportation of stack raster to GeoPackage?

I'm trying export a stack raster to GeoPackage using the terra package but I'm only able to for one layer manually:

library(terra)

# Create a SpatRaster object with 3 layers
r <- rast(ncols=5, nrows=5, nlyrs=3, vals = rnorm(5*5*3))

# This doesn't work:
writeRaster(r, "rast.gpkg", filetype = "GPKG", overwrite = TRUE)

Error: [writeRaster] failed writing GPKG file In addition: Warning message: Only single band dataset supported for non Byte datatype (GDAL error 6)

# This doesn't work either because it replaces the layer in GPKG file:
writeRaster(r[[1]], "rast.gpkg", filetype = "GPKG", overwrite = TRUE)
writeRaster(r[[2]], "rast.gpkg", filetype = "GPKG", overwrite = TRUE)
writeRaster(r[[3]], "rast.gpkg", filetype = "GPKG", overwrite = TRUE)

Any ideas?

2
  • 1
    mutate doesn't work for me on a terra raster object (error: no applicable method for 'mutate' applied to an object of class "SpatRaster") but if I otherwise construct a 3-layer raster I get error: Error: [writeRaster] failed writing GPKG file and In addition: Warning message: Only single band dataset supported for non Byte datatype (GDAL error 6). Is that what you get? You don't say.
    – Spacedman
    Commented Apr 27, 2023 at 12:48
  • Because I Using tidterra. I'll update in a minute
    – mferreira
    Commented Apr 27, 2023 at 12:49

1 Answer 1

2

The GDAL driver for GeoPackages has been mostly designed to store map image tiles rather than data, so it only currently implements multi-layer rasters for single-byte data. This means typically RGB or RGBA data with one byte per pixel-colour ranging from 0 to 255.

So if your data can be squashed into integers from 0-255 then you can do it, by specifying the byte data type:

r[] = 1:ncell(r)
writeRaster(r, "/tmp/bytes.gpkg", datatype="INT1U", overwrite = TRUE)

In a round trip this comes back as a four layer raster by default, since its encoding an A channel (transparency) too.

rr = rast("/tmp/bytes.gpkg")
rr[]
      bytes_1 bytes_2 bytes_3 bytes_4
 [1,]       1       1       1     255
 [2,]       2       2       2     255
 [3,]       3       3       3     255

But you won't be able to store multi-layer floating point data at the moment, and I'm surprised single-layer floats work given that the docs say "GeoPackage rasters only support Byte data type." https://gdal.org/drivers/raster/gpkg.html

4
  • Thank you. Do you have an alternative to store and send Geographic information with vector and raster formats?
    – mferreira
    Commented Apr 27, 2023 at 13:24
  • 1
    GDAL supports also single-band "gridded coverage data" gdal.org/drivers/raster/gpkg.html#tiled-gridded-coverage-data. "Since GDAL 2.3, tiled gridded coverage data can be stored using PNG unsigned 16bit tiles (with potential offset and scaling so as to be able to represent floating point data) or TIFF 32-bit floating-point LZW compressed tiles."
    – user30184
    Commented Apr 27, 2023 at 13:39
  • I suspect the line about "rasters only support Byte data type" is from an older driver doc but hasn't been editing out...
    – Spacedman
    Commented Apr 27, 2023 at 19:22
  • If you want to send vectors and rasters, the solution might be to put the vectors in a geopackage and put it with a folder of rasters into a zip file.
    – Spacedman
    Commented Apr 27, 2023 at 19:24

Your Answer

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

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