gpkg_table_pragma(): Get information on a table in a GeoPackage (without returning the whole table).

gpkg_table(): Access a specific table (by name) and get a "lazy" dbplyr tbl_SQLiteConnection object referencing that table

gpkg_collect(): Alias for gpkg_table(..., collect=TRUE)

gpkg_tbl(): Alias for gpkg_table(..., collect=FALSE)(default) that always returns a tbl_SQLiteConnection object.

gpkg_rast(): Get a SpatRaster object corresponding to the specified table_name

gpkg_vect(): Get a SpatVector object corresponding to the specified table_name

gpkg_sf(): Get a sf-tibble object corresponding to the specified table_name

gpkg_table_pragma(x, table_name = NULL, ...)

# S3 method for class 'character'
gpkg_table_pragma(x, table_name = NULL, ...)

# S3 method for class 'SQLiteConnection'
gpkg_table_pragma(x, table_name, ...)

# S3 method for class 'geopackage'
gpkg_table_pragma(x, table_name = NULL, ...)

gpkg_table(
  x,
  table_name,
  collect = FALSE,
  column_names = "*",
  query_string = FALSE,
  ...
)

# Default S3 method
gpkg_table(
  x,
  table_name,
  collect = FALSE,
  column_names = "*",
  query_string = FALSE,
  ...
)

gpkg_collect(x, table_name, query_string = FALSE, ...)

gpkg_tbl(x, table_name, ...)

gpkg_rast(x, table_name = NULL, ...)

gpkg_vect(x, table_name, ...)

gpkg_sf(x, table_name, ...)

Arguments

x

A geopackage object or character path to GeoPackage file

table_name

character. One or more table names; for gpkg_table_pragma() if table_name=NULL returns a record for each table. gpkg_table() requires table_name be specified

...

Additional arguments. In gpkg_table() arguments in ... are passed to dplyr::tbl(). For gpkg_table_pragma(), ... arguments are (currently) not used. For gpkg_rast() additional arguments are passed to terra::rast(). For gpkg_vect() additional arguments (such as proxy=TRUE) are passed to terra::vect().

collect

logical. Materialize a data.frame object in memory? Default: FALSE requires 'dbplyr' package. TRUE uses 'RSQLite'.

column_names

character. Used only when collect=TRUE. A character vector of column names to select from table_name.

query_string

logical. Return SQLite query rather than executing it? Default: FALSE

Value

gpkg_table(): A 'dbplyr' object of class tbl_SQLiteConnection

gpkg_collect(): An object of class data.frame

gpkg_tbl(): An object of class tbl_SQLiteConnection

gpkg_rast(): A 'terra' object of class SpatRaster

gpkg_vect(): A 'terra' object of class SpatVector (may not contain geometry columns)

gpkg_sf()): An sf-tibble object of class "sf", "tbl_df". If the table contains no geometry column the result is a "tbl_df".

Examples


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

r <- terra::rast(system.file("extdata", "dem.tif", package = "gpkg"))

gpkg_write(r,
           destfile = tf,
           RASTER_TABLE = "DEM1",
           FIELD_NAME = "Elevation")

gpkg_write(r,
           destfile = tf,
           append = TRUE,
           RASTER_TABLE = "DEM2",
           FIELD_NAME = "Elevation")

g <- geopackage(tf, connect = TRUE)

# inspect gpkg_contents table
gpkg_table(g, "gpkg_contents")
#> # Source:   table<`gpkg_contents`> [2 x 10]
#> # Database: sqlite 3.47.0 [/tmp/RtmpLWuSH9/file1ac81b52b11.gpkg]
#>   table_name data_type      identifier description last_change min_x min_y max_x
#>   <chr>      <chr>          <chr>      <chr>       <chr>       <dbl> <dbl> <dbl>
#> 1 DEM1       2d-gridded-co… DEM1       ""          2024-11-30…  6.01  49.7  6.27
#> 2 DEM2       2d-gridded-co… DEM2       ""          2024-11-30…  6.01  49.7  6.27
#> # ℹ 2 more variables: max_y <dbl>, srs_id <int>

gpkg_vect(g, "gpkg_contents")
#>  class       : SpatVector 
#>  geometry    : none 
#>  dimensions  : 0, 10  (geometries, attributes)
#>  extent      : 0, 0, 0, 0  (xmin, xmax, ymin, ymax)
#>  source      : file1ac81b52b11.gpkg (SELECT)
#>  coord. ref. :  
#>  names       : table_name data_type identifier description last_change min_x
#>  type        :      <chr>     <chr>      <chr>       <chr>       <chr> <num>
#>  min_y max_x max_y srs_id
#>  <num> <num> <num>  <int>

# materialize a data.frame from gpkg_2d_gridded_tile_ancillary
library(dplyr, warn.conflicts = FALSE)

gpkg_table(g, "gpkg_2d_gridded_tile_ancillary") %>% 
  dplyr::filter(tpudt_name == "DEM2") %>% 
  dplyr::select(mean, std_dev) %>% 
  dplyr::collect()
#> # A tibble: 1 × 2
#>    mean std_dev
#>   <dbl>   <dbl>
#> 1  324.    58.5

gpkg_disconnect(g)