Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to write new SpatRaster to existing Geopackage crashes R #300

Closed
gtitov opened this issue Aug 6, 2021 · 1 comment
Closed

Attempt to write new SpatRaster to existing Geopackage crashes R #300

gtitov opened this issue Aug 6, 2021 · 1 comment

Comments

@gtitov
Copy link

gtitov commented Aug 6, 2021

I'm trying to write raster layers to Geopackage. It works perfectly fine when I create a new Geopackage. But when I try to write next SpatRaster to the existing Geopackage R crashes. I tried it in RStudio and in RGui with the same result.
image

It's worth noting that terra still writes second SpatRaster to the Geopackage and this layer works fine. So R probably crashes after the function executed.

I hope it makes sense..

Here is an example

library(terra)
r1 = terra::rast(ncols=10, nrows=10)
values(r1) = 1:ncell(r1)
r2 = terra::rast(ncols=10, nrows=10)
values(r2) = 1:ncell(r2)

writeRaster(
  r1,
  "test.gpkg",
  filetype = "GPKG",
  gdal = c("APPEND_SUBDATASET=YES", "RASTER_TABLE=r1"),
  datatype = 'INT1U'
)

writeRaster(
  r2,
  "test.gpkg",
  filetype = "GPKG",
  gdal = c("APPEND_SUBDATASET=YES", "RASTER_TABLE=r2"),
  datatype = 'INT1U',
  overwrite=TRUE
)

And here is my R version:

R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

I guess my issue is connected to this one somehow.

Thanks and kind regards!

@rhijmans
Copy link
Member

Thank you very much for reporting this and apologies for the delay in addressing it.
The example now works:

library(terra)
#terra 1.5.8

r1 <- rast(ncols=10, nrows=10, vals=1:100)
r2 <- rast(ncols=10, nrows=10, vals=100:1)

f <-  "test.gpkg"
writeRaster(r1, f, gdal="RASTER_TABLE=r1", datatype ="INT1U", overwrite=TRUE)
x <- writeRaster(r2, f, gdal=c("APPEND_SUBDATASET=YES", "RASTER_TABLE=r2"), datatype="INT1U")
head(values(x))
#     r1_1 r1_2 r1_3 r1_4 r2_1 r2_2 r2_3 r2_4
#[1,]    1    1    1  255  100  100  100  255
#[2,]    2    2    2  255   99   99   99  255
#[3,]    3    3    3  255   98   98   98  255
#[4,]    4    4    4  255   97   97   97  255
#[5,]    5    5    5  255   96   96   96  255
#[6,]    6    6    6  255   95   95   95  255

x
#class       : SpatRaster 
#dimensions  : 10, 10, 8  (nrow, ncol, nlyr)
#resolution  : 36, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#sources     : test.gpkg:r1  (4 layers) 
#              test.gpkg:r2  (4 layers) 
#varnames    : test 
#              test 
#names       : r1_1, r1_2, r1_3, r1_4, r2_1, r2_2, ... 
 
sds(f)
#class       : SpatRasterDataset 
#subdatasets : 2 
#dimensions  : 10, 10 (nrow, ncol)
#nlyr        : 4, 4 
#resolution  : 36, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source(s)   : test.gpkg 
#names       : r1, r2 
 
describe(f, sds=TRUE)
#  id              name var    desc nrow ncol nlyr
#1  1 GPKG:test.gpkg:r1  r1 r1 - r1   10   10    4
#2  2 GPKG:test.gpkg:r2  r2 r2 - r2   10   10    4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants