From 8f3cae479dec4d0f8166ab51926893fe3b4658f0 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Tue, 13 Feb 2024 06:28:53 -0800 Subject: [PATCH] gpkg_write: handle SpatVectorProxy input --- R/gpkg-io.R | 4 ++++ misc/gpkg-raster.R | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/R/gpkg-io.R b/R/gpkg-io.R index 72dc2d7..fe905dd 100644 --- a/R/gpkg-io.R +++ b/R/gpkg-io.R @@ -291,6 +291,10 @@ gpkg_write <- function(x, gdal_options <- unique(c(gdal_options, .lut_gpkg_creation(...))) + if (inherits(x, 'SpatVectorProxy')) { + x <- terra::query(x) + } + if (!inherits(x, 'SpatVector')) { x <- terra::vect(x) } diff --git a/misc/gpkg-raster.R b/misc/gpkg-raster.R index 0611c23..a8fc2f4 100644 --- a/misc/gpkg-raster.R +++ b/misc/gpkg-raster.R @@ -1,31 +1,54 @@ library(soilDB) library(terra) library(gpkg) + b <- c(-119.747629, -119.67935, 36.912019, 36.944987) + bbox.sp <- sf::st_as_sf(wk::rct( xmin = b[1], xmax = b[2], ymin = b[3], ymax = b[4], crs = sf::st_crs(4326) )) + ssurgo.geom <- SDA_spatialQuery( bbox.sp, what = 'mupolygon', db = 'SSURGO', geomIntersection = TRUE ) + sf::write_sf(ssurgo.geom, "test.shp") + x <- mukey.wcs(ssurgo.geom) + lback <- levels(x) + plot(x) levels(x) <- NULL -r <- writeRaster(x, "test.tif", datatype = "FLT4S", overwrite = TRUE) + +r <- terra::writeRaster(x, "test.tif", datatype = "FLT4S", overwrite = TRUE) plot(r) -r <- classify(r, matrix(c(463442, -9999), ncol=2), filename="foo.tif", overwrite = TRUE) -gpkg_write(list(bar = rast("foo.tif")), NoData = -9999, destfile = "test.gpkg", overwrite = TRUE) -gpkg_write(list(foo = terra::vect("test.shp", proxy = TRUE)), destfile = "test.gpkg", append = TRUE) -x2 <- rast("test.gpkg") + +r <- terra::classify(r, + matrix(c(463442, -9999), ncol = 2), + filename = "foo.tif", + overwrite = TRUE) + +gpkg_write( + list(bar = terra::rast("foo.tif")), + NoData = -9999, + destfile = "test.gpkg", + overwrite = TRUE +) + +gpkg_write(list(foo = terra::vect("test.shp", proxy = TRUE)), + destfile = "test.gpkg", + append = TRUE) + +x2 <- terra::rast("test.gpkg") ux2 <- unique(x2) levels(x2) <- data.frame(value = ux2, category = ux2) plot(x2) -x <- vect("test.gpkg") -plot(project(x, x2), add=T) + +x <- terra::vect("test.gpkg") +plot(terra::project(x, x2), add = TRUE) plot(is.na(x2))