brownag / gpkg

Utilities for the Open Geospatial Consortium (OGC) 'GeoPackage' Format in R
http://humus.rocks/gpkg/
Creative Commons Zero v1.0 Universal
18 stars 0 forks source link

Add spatial view support #12

Closed brownag closed 9 months ago

brownag commented 9 months ago
brownag commented 9 months ago

For example a spatial view that calculates Voronoi diagrams for a subset features of the terra Luxemburg dataset.

library(gpkg)
library(terra)
#> terra 1.7.55

gpkg_tmp <- tempfile(fileext = ".gpkg")

if (file.exists(gpkg_tmp))
  file.remove(gpkg_tmp)

v <- vect(system.file("ex", "lux.shp", package = "terra"))

gpkg_write(list(lux = v), destfile = gpkg_tmp, append = TRUE)

g <- geopackage(gpkg_tmp, connect = TRUE)

gpkg_create_spatial_view(g, "my_vor", "SELECT lux.fid AS OGC_FID, 
                                              lux.fid AS fid,
                                              ST_VoronojDiagram(geom) AS geom2 
                                       FROM lux WHERE ID_2 <= 3", 
                         geom_column = "geom2", 
                         geometry_type_name = "MULTIPOLYGON") 
                         # NB: terra::vect() sensitive to geom type
gpkg_contents(g)
#>   table_name data_type identifier description              last_change   min_x
#> 1        lux  features        lux             2023-12-07T05:23:01.564Z 5.74414
#> 2     my_vor  features     my_vor             2023-12-07T05:23:01.973Z      NA
#>      min_y    max_x    max_y srs_id
#> 1 49.44781 6.528252 50.18162   4326
#> 2       NA       NA       NA   4326

plot(gpkg_vect(g, "my_vor"))