Skip to content

Commit

Permalink
Add gpkg_create_spatial_view() for #6
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Nov 21, 2023
1 parent 882565b commit a165fef
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: gpkg
Type: Package
Title: Utilities for the Open Geospatial Consortium 'GeoPackage' Format
Version: 0.0.7
Version: 0.0.8
Authors@R: person(given="Andrew", family="Brown", email="[email protected]", role = c("aut", "cre"))
Maintainer: Andrew Brown <[email protected]>
Description: Build Open Geospatial Consortium 'GeoPackage' files (<https://www.geopackage.org/>). 'GDAL' utilities for reading and writing spatial data are provided by the 'terra' package. Additional 'GeoPackage' and 'SQLite' features for attributes and tabular data are implemented with the 'RSQLite' package.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(gpkg_connect)
export(gpkg_contents)
export(gpkg_create_contents)
export(gpkg_create_dummy_features)
export(gpkg_create_spatial_view)
export(gpkg_delete_contents)
export(gpkg_disconnect)
export(gpkg_execute)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# gpkg 0.0.8

- Added `gpkg_create_spatial_view()` for creating spatial views, which dynamic layers that are accessible as if they were typical static geometry layers (for #6).

# gpkg 0.0.7

- Added `gpkg_bbox()` as an application of `gpkg_ogr_query()`
Expand Down
32 changes: 32 additions & 0 deletions R/gpkg-view.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Create a Spatial View
#'
#' @param g a `geopackage`
#' @param viewname _character_. Name of view.
#' @param viewquery _character_. Query for view contents.
#' @param geom_column _character_. Column name of view geometry. Default: `"geom"`
#' @param geometry_type_name _character_. View geometry type. Default: `"GEOMETRY"`
#' @param data_type _character_. View data type. Default `"features"`
#' @param srs_id _integer_. Spatial Reference System ID. Default: `4326` (WGS84)
#' @param z _integer_. Default: `0`
#' @param m _integer_. Default: `0`
#'
#' @return _integer_. Returns `1` if a new record in `gpkg_geometry_columns` is successfully made.
#' @export
#'
gpkg_create_spatial_view <- function(g,
viewname,
viewquery,
geom_column = "geom",
geometry_type_name = "GEOMETRY",
data_type = "features",
srs_id = 4326,
z = 0,
m = 0) {
gpkg_execute(g, sprintf("CREATE VIEW %s AS %s", viewname, viewquery))
gpkg_execute(g, sprintf("INSERT INTO gpkg_contents (table_name, identifier, data_type, srs_id)
VALUES ('%s', '%s', '%s', %s)",
viewname, viewname, data_type, srs_id))
gpkg_execute(g, sprintf("INSERT INTO gpkg_geometry_columns (table_name, column_name, geometry_type_name, srs_id, z, m)
VALUES ('%s', '%s', '%s', %s, %s, %s)",
viewname, geom_column, geometry_type_name, srs_id, z, m))

Check warning on line 31 in R/gpkg-view.R

View check run for this annotation

Codecov / codecov/patch

R/gpkg-view.R#L25-L31

Added lines #L25 - L31 were not covered by tests
}
43 changes: 43 additions & 0 deletions man/gpkg_create_spatial_view.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions misc/gpkg-view-voronoi.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
library(gpkg)
library(terra)

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)

plot(gpkg_vect(g, "my_vor"))
gpkg_vect(g, "lux") |>
subset(ID_2 <= 3, NSE = TRUE) |>
as.lines() |>
plot(col = "BLUE", lwd = 2, add = TRUE)

# compare to:
plot(sf::st_read(g$dsn, layer = "my_vor"))
plot(svc(g$dsn, layer = "my_vor")[1])
plot(query(vect(g$dsn, layer = "my_vor", proxy = TRUE)))

0 comments on commit a165fef

Please sign in to comment.