Skip to content

Commit

Permalink
gpkg_write: handle SpatVectorProxy input
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Feb 13, 2024
1 parent c527a1e commit 8f3cae4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions R/gpkg-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
37 changes: 30 additions & 7 deletions misc/gpkg-raster.R
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 8f3cae4

Please sign in to comment.