| Title: | Spatial Data Operations for Database-Backed Geometries |
|---|---|
| Description: | Provides database-backed spatial geometry classes and methods for working with vector spatial data in 'DuckDB'. The package supports loading, converting, querying, joining, and measuring spatial geometries through familiar 'sf'-style interfaces while keeping geometry columns lazy inside the database. It integrates with 'dbProject' to preserve database paths, live connections, and spatial table metadata across interactive sessions. The package follows the Simple Features framework described by Pebesma (2018) <doi:10.32614/RJ-2018-009> and uses DuckDB's spatial extension <https://duckdb.org/docs/stable/core_extensions/spatial/overview.html>. |
| Authors: | Edward C. Ruiz [aut, cre] (ORCID: <https://orcid.org/0000-0002-9174-5387>), Jiaji George Chen [aut], Ruben Dries [aut, rev] |
| Maintainer: | Edward C. Ruiz <[email protected]> |
| License: | GPL-3 | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-05-27 22:38:06 UTC |
| Source: | https://github.com/cran/dbSpatial |
Extract a column from a dbSpatial object
## S4 method for signature 'dbSpatial' x$name## S4 method for signature 'dbSpatial' x$name
x |
A dbSpatial object |
name |
Column name to extract |
A vector of values from the specified column
dbSpatial objectCreate a dbSpatial object from an sf or terra object.
as_dbSpatial(rSpatial, conn, name, overwrite = FALSE, ...)as_dbSpatial(rSpatial, conn, name, overwrite = FALSE, ...)
rSpatial |
|
conn |
A DBI connection object, as returned by |
name |
|
overwrite |
|
... |
Additional arguments to be passed |
Writes the rSpatial object to a temporary DuckDB table and computes the
table in the database with the specified name and the geometry column as
geom.
A dbSpatial object backed by table name in conn, with the
geometry column stored as geom.
Other dbSpatial:
dbSpatial,
show,dbSpatial-method,
st_as_sf.dbSpatial(),
vect,dbSpatial-method
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) # Create a SpatVector from the data.frame dummy_spatvector <- terra::vect(dummy_data, geom = c("x", "y")) # Set db connection duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") dbSpatial <- as_dbSpatial(rSpatial = dummy_spatvector, conn = duckdb_conn, name = "dummy_spatvector", overwrite = TRUE) dbSpatial DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) # Create a SpatVector from the data.frame dummy_spatvector <- terra::vect(dummy_data, geom = c("x", "y")) # Set db connection duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") dbSpatial <- as_dbSpatial(rSpatial = dummy_spatvector, conn = duckdb_conn, name = "dummy_spatvector", overwrite = TRUE) dbSpatial DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
dbSpatial object with geometry data typeConstructor function to ingest diverse spatial data sources and create
a dbSpatial object containing a geometry data type based
on the Simple Features
standard.
If x_colName and y_colName are both provided, a POINT geometry
will be constructed based on these columns.
dbSpatial( value, name, conn, x_colName = NULL, y_colName = NULL, geomName = "geom", overwrite = FALSE, ... )dbSpatial( value, name, conn, x_colName = NULL, y_colName = NULL, geomName = "geom", overwrite = FALSE, ... )
value |
|
name |
Table name. |
conn |
A DBI connection object. |
x_colName |
|
y_colName |
|
geomName |
|
overwrite |
|
... |
Additional arguments to be passed |
For list of files supported see link below. https://DuckDB.org/docs/extensions/spatial.html#st_read—read-spatial-value-from-files
dbSpatial object.
Other dbSpatial:
as_dbSpatial(),
show,dbSpatial-method,
st_as_sf.dbSpatial(),
vect,dbSpatial-method
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { # create in-memory DuckDB db duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") # test value test_data = data.frame(x = 1:10, y = 1:10, id = 1:10) test_file <- tempfile(fileext = ".csv") write.csv(test_data, test_file, row.names = FALSE) # read data.frame and create point geometry dbSpatial(conn = duckdb_conn, name = "test_points", value = test_data, x_colName = "x", y_colName = "y", overwrite = TRUE) # read csv dbSpatial(conn = duckdb_conn, name = "test_points", value = test_file, x_colName = "x", y_colName = "y", overwrite = TRUE) unlink(test_file) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { # create in-memory DuckDB db duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") # test value test_data = data.frame(x = 1:10, y = 1:10, id = 1:10) test_file <- tempfile(fileext = ".csv") write.csv(test_data, test_file, row.names = FALSE) # read data.frame and create point geometry dbSpatial(conn = duckdb_conn, name = "test_points", value = test_data, x_colName = "x", y_colName = "y", overwrite = TRUE) # read csv dbSpatial(conn = duckdb_conn, name = "test_points", value = test_file, x_colName = "x", y_colName = "y", overwrite = TRUE) unlink(test_file) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Global options to control package behavior.
Use options() to set the below options.
dbSpatial.max_print: integer. Max characters for WKT in show method (default 30).
dbSpatial.max_mem_convert: numeric. Max bytes for implicit coercion (default 8GB).
dbSpatial.verbose: logical. Print info messages during coercion (default TRUE).
options(dbSpatial.max_print = 50) options(dbSpatial.max_mem_convert = 16 * 1024^3) options(dbSpatial.verbose = FALSE)options(dbSpatial.max_print = 50) options(dbSpatial.max_mem_convert = 16 * 1024^3) options(dbSpatial.verbose = FALSE)
head method for dbSpatial
## S4 method for signature 'dbSpatial' head(x, n = 6L, ...)## S4 method for signature 'dbSpatial' head(x, n = 6L, ...)
x |
A |
n |
Number of rows to return. |
... |
Additional arguments. |
A dbSpatial object containing the first n rows of x.
Other dbData:
tail,dbSpatial-method
Install and/or load DuckDB spatial extension
loadSpatial(conn)loadSpatial(conn)
conn |
duckdb connection |
No return value, called for side effects. Installs and loads the
DuckDB spatial extension for conn.
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") loadSpatial(conn = duckdb_conn) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") loadSpatial(conn = duckdb_conn) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Show method for dbSpatial
## S4 method for signature 'dbSpatial' show(object)## S4 method for signature 'dbSpatial' show(object)
object |
A |
Other dbSpatial:
as_dbSpatial(),
dbSpatial,
st_as_sf.dbSpatial(),
vect,dbSpatial-method
Returns the area of the geometry column.
x |
|
geomName |
|
... |
additional arguments passed to methods |
dbSpatial object (lazy tibble with area column)
Other measurements:
st_length(),
st_perimeter()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { square <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_polygon(list(rbind( c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0) ))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(square, conn = duckdb_conn, name = "square", overwrite = TRUE) st_area(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { square <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_polygon(list(rbind( c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0) ))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(square, conn = duckdb_conn, name = "square", overwrite = TRUE) st_area(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Returns the GeoJSON representation of the geometry.
st_as_geojson(x, ...) ## S3 method for class 'dbSpatial' st_as_geojson(x, ...)st_as_geojson(x, ...) ## S3 method for class 'dbSpatial' st_as_geojson(x, ...)
x |
|
... |
additional arguments |
dbSpatial object with GeoJSON column
st_as_geojson(dbSpatial): Method for dbSpatial objects
Other constructors:
st_as_text()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_as_geojson(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_as_geojson(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
S3 method implementation for converting dbSpatial objects to sf objects.
## S3 method for class 'dbSpatial' st_as_sf(x, geomName = "geom", select = tidyselect::everything(), ...)## S3 method for class 'dbSpatial' st_as_sf(x, geomName = "geom", select = tidyselect::everything(), ...)
x |
A |
geomName |
|
select |
Columns to retain in output (default: all columns) |
... |
Additional arguments passed to |
This method handles conversion of dbSpatial to sf::sf objects using:
Dynamic column selection via tidyselect semantics
Automatic geometry column preservation
SQL-level column subsetting for efficiency
An sf::sf object
Other dbSpatial:
as_dbSpatial(),
dbSpatial,
show,dbSpatial-method,
vect,dbSpatial-method
Returns the Well-Known Text (WKT) representation of the geometry.
x |
|
... |
additional arguments |
dbSpatial object with WKT column
Other constructors:
st_as_geojson()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_as_text(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_as_text(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Returns the bounding box of the geometry column.
## S3 method for class 'dbSpatial' st_bbox(obj, ...)## S3 method for class 'dbSpatial' st_bbox(obj, ...)
obj |
|
... |
additional arguments passed to methods |
bbox object (named numeric vector with xmin, ymin, xmax, ymax)
Other geom_summary:
st_xmax(),
st_ymax()
Returns a geometry that represents all points whose distance from this Geometry is less than or equal to distance.
x |
|
dist |
numeric distance |
... |
additional arguments |
dbSpatial object with buffered geometry
Other geometry_ops:
st_centroid(),
st_simplify()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(0, 0)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_buffer(x, dist = 1) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(0, 0)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_buffer(x, dist = 1) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Returns the geometric center of a geometry.
x |
|
... |
additional arguments |
dbSpatial object with centroid geometry
Other geometry_ops:
st_buffer(),
st_simplify()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { square <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_polygon(list(rbind( c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0) ))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(square, conn = duckdb_conn, name = "square", overwrite = TRUE) st_centroid(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { square <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_polygon(list(rbind( c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0) ))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(square, conn = duckdb_conn, name = "square", overwrite = TRUE) st_centroid(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Returns the geometry type for each row in a dbSpatial object using DuckDB's
spatial function ST_GeometryType().
st_geometrytype(dbSpatial, geomName = "geom", collect = FALSE, n = NULL, ...)st_geometrytype(dbSpatial, geomName = "geom", collect = FALSE, n = NULL, ...)
dbSpatial |
A dbSpatial object. |
geomName |
Geometry column name. Default: |
collect |
Logical (default = |
n |
Optional integer. If provided alongside |
... |
Additional arguments (ignored). |
If collect = FALSE (default), a lazy tibble with a single column
geom_type. If collect = TRUE, a character vector of geometry type(s).
Performs a spatial join between two dbSpatial objects using a specified spatial predicate function.
## S3 method for class 'dbSpatial' st_join(x, y, join = st_intersects, suffix = c(".x", ".y"), ...)## S3 method for class 'dbSpatial' st_join(x, y, join = st_intersects, suffix = c(".x", ".y"), ...)
x |
A |
y |
A |
join |
Spatial predicate function to use for the join. Default is
|
suffix |
Character vector of length 2. Suffixes to add to duplicate
column names from x and y. Default is |
... |
Additional arguments passed to the internal join function. |
This function follows the sf pattern where spatial predicates are passed
as the join argument.
A dbSpatial object containing the spatial join result.
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { con <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") df1 <- data.frame(id = 1:3, x = c(0, 10, 20), y = c(0, 10, 20)) pts1 <- dbSpatial(conn = con, name = "pts1", value = df1, x_colName = "x", y_colName = "y", overwrite = TRUE) df2 <- data.frame(id = 4:6, x = c(0, 15, 25), y = c(0, 15, 25)) pts2 <- dbSpatial(conn = con, name = "pts2", value = df2, x_colName = "x", y_colName = "y", overwrite = TRUE) # Spatial join using intersection result <- sf::st_join(pts1, pts2, join = sf::st_intersects) # Spatial join using within predicate result <- sf::st_join(pts1, pts2, join = sf::st_within) DBI::dbDisconnect(con, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { con <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") df1 <- data.frame(id = 1:3, x = c(0, 10, 20), y = c(0, 10, 20)) pts1 <- dbSpatial(conn = con, name = "pts1", value = df1, x_colName = "x", y_colName = "y", overwrite = TRUE) df2 <- data.frame(id = 4:6, x = c(0, 15, 25), y = c(0, 15, 25)) pts2 <- dbSpatial(conn = con, name = "pts2", value = df2, x_colName = "x", y_colName = "y", overwrite = TRUE) # Spatial join using intersection result <- sf::st_join(pts1, pts2, join = sf::st_intersects) # Spatial join using within predicate result <- sf::st_join(pts1, pts2, join = sf::st_within) DBI::dbDisconnect(con, shutdown = TRUE) }
Returns the length of the geometry column.
x |
|
geomName |
|
... |
additional arguments passed to methods |
dbSpatial object (lazy tibble with length column)
Other measurements:
st_area(),
st_perimeter()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { line <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_linestring(rbind(c(0, 0), c(1, 1), c(2, 1))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(line, conn = duckdb_conn, name = "line", overwrite = TRUE) st_length(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { line <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_linestring(rbind(c(0, 0), c(1, 1), c(2, 1))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(line, conn = duckdb_conn, name = "line", overwrite = TRUE) st_length(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Returns the number of points in a geometry.
st_npoints(x, ...) ## S4 method for signature 'dbSpatial' st_npoints(x, ...)st_npoints(x, ...) ## S4 method for signature 'dbSpatial' st_npoints(x, ...)
x |
|
... |
additional arguments |
dbSpatial object with npoints column
st_npoints(dbSpatial): Method for dbSpatial objects
Other accessors:
st_x(),
st_y()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { line <- sf::st_sf( id = 1, geom = sf::st_sfc(sf::st_linestring(rbind(c(0, 0), c(1, 1), c(2, 1)))) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(line, conn = duckdb_conn, name = "line", overwrite = TRUE) st_npoints(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { line <- sf::st_sf( id = 1, geom = sf::st_sfc(sf::st_linestring(rbind(c(0, 0), c(1, 1), c(2, 1)))) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(line, conn = duckdb_conn, name = "line", overwrite = TRUE) st_npoints(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Returns the perimeter of the geometry column.
x |
|
geomName |
|
... |
additional arguments passed to methods |
dbSpatial object (lazy tibble with perimeter column)
Other measurements:
st_area(),
st_length()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { square <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_polygon(list(rbind( c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0) ))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(square, conn = duckdb_conn, name = "square", overwrite = TRUE) st_perimeter(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { square <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_polygon(list(rbind( c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0) ))) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(square, conn = duckdb_conn, name = "square", overwrite = TRUE) st_perimeter(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
Returns a simplified version of the given geometry using the Douglas-Peucker algorithm.
x |
|
dTolerance |
numeric tolerance |
... |
additional arguments |
dbSpatial object with simplified geometry
Other geometry_ops:
st_buffer(),
st_centroid()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { line <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_linestring(rbind( c(0, 0), c(0.5, 0.2), c(1, 0), c(1.5, 0.1), c(2, 0) )) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(line, conn = duckdb_conn, name = "line", overwrite = TRUE) st_simplify(x, dTolerance = 0.1) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { line <- sf::st_sf( id = 1, geom = sf::st_sfc( sf::st_linestring(rbind( c(0, 0), c(0.5, 0.2), c(1, 0), c(1.5, 0.1), c(2, 0) )) ) ) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(line, conn = duckdb_conn, name = "line", overwrite = TRUE) st_simplify(x, dTolerance = 0.1) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
This function translates point geometries by the specified delta x and delta y values.
st_translate(dbSpatial, geomName = "geom", dx, dy, ...) ## S4 method for signature 'dbSpatial' st_translate(dbSpatial, geomName = "geom", dx, dy, ...)st_translate(dbSpatial, geomName = "geom", dx, dy, ...) ## S4 method for signature 'dbSpatial' st_translate(dbSpatial, geomName = "geom", dx, dy, ...)
dbSpatial |
|
geomName |
|
dx |
|
dy |
|
... |
additional arguments passed to methods |
dbSpatial object
st_translate(dbSpatial): Method for dbSpatial object
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { con = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) points <- dbSpatial(conn = con, name = "points", value = dummy_data, overwrite = TRUE, x_colName = "x", y_colName = "y") points points_translated <- st_translate(dbSpatial = points, dx = 100, dy = -20) points_translated DBI::dbDisconnect(con, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { con = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) points <- dbSpatial(conn = con, name = "points", value = dummy_data, overwrite = TRUE, x_colName = "x", y_colName = "y") points points_translated <- st_translate(dbSpatial = points, dx = 100, dy = -20) points_translated DBI::dbDisconnect(con, shutdown = TRUE) }
Returns the X coordinate of a point.
st_x(x, ...) ## S4 method for signature 'dbSpatial' st_x(x, ...)st_x(x, ...) ## S4 method for signature 'dbSpatial' st_x(x, ...)
x |
|
... |
additional arguments |
dbSpatial object with X coordinate column
st_x(dbSpatial): Method for dbSpatial objects
Other accessors:
st_npoints(),
st_y()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_x(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_x(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
This function returns the maximum x coordinate in each geometry in
the specified dbSpatial object.
st_xmax(dbSpatial, geomName = "geom", ...) ## S4 method for signature 'dbSpatial' st_xmax(dbSpatial, geomName = "geom", ...)st_xmax(dbSpatial, geomName = "geom", ...) ## S4 method for signature 'dbSpatial' st_xmax(dbSpatial, geomName = "geom", ...)
dbSpatial |
|
geomName |
|
... |
additional arguments passed to methods |
numerical column vector in database
st_xmax(dbSpatial): Method for dbSpatial object
Other geom_summary:
st_bbox(),
st_ymax()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { # Create a data.frame with x and y coordinates and attributes coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) # Create a duckdb connection con = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") # Create a duckdb table with spatial points db_points = dbSpatial(conn = con, value = dummy_data, x_colName = "x", y_colName = "y", name = "foo", overwrite = TRUE) st_xmax(dbSpatial = db_points) DBI::dbDisconnect(con, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { # Create a data.frame with x and y coordinates and attributes coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) # Create a duckdb connection con = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") # Create a duckdb table with spatial points db_points = dbSpatial(conn = con, value = dummy_data, x_colName = "x", y_colName = "y", name = "foo", overwrite = TRUE) st_xmax(dbSpatial = db_points) DBI::dbDisconnect(con, shutdown = TRUE) }
Returns the Y coordinate of a point.
st_y(x, ...) ## S4 method for signature 'dbSpatial' st_y(x, ...)st_y(x, ...) ## S4 method for signature 'dbSpatial' st_y(x, ...)
x |
|
... |
additional arguments |
dbSpatial object with Y coordinate column
st_y(dbSpatial): Method for dbSpatial objects
Other accessors:
st_npoints(),
st_x()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_y(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point <- sf::st_sf(id = 1, geom = sf::st_sfc(sf::st_point(c(1, 2)))) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") x <- as_dbSpatial(point, conn = duckdb_conn, name = "point", overwrite = TRUE) st_y(x) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }
This function returns the maximum y coordinate of the geometries in the specified dbSpatial object.
st_ymax(dbSpatial, geomName = "geom", ...) ## S4 method for signature 'dbSpatial' st_ymax(dbSpatial, geomName = "geom", ...)st_ymax(dbSpatial, geomName = "geom", ...) ## S4 method for signature 'dbSpatial' st_ymax(dbSpatial, geomName = "geom", ...)
dbSpatial |
|
geomName |
|
... |
additional arguments passed to methods |
numerical column vector in database
st_ymax(dbSpatial): Method for dbSpatial object
Other geom_summary:
st_bbox(),
st_xmax()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { # Create a data.frame with x and y coordinates and attributes coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) # Create a duckdb connection con = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") # Create a duckdb table with spatial points db_points = dbSpatial(conn = con, value = dummy_data, x_colName = "x", y_colName = "y", name = "foo", overwrite = TRUE) st_ymax(dbSpatial = db_points) DBI::dbDisconnect(con, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { # Create a data.frame with x and y coordinates and attributes coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700)) attributes <- data.frame(id = 1:3, name = c("A", "B", "C")) # Combine the coordinates and attributes dummy_data <- cbind(coordinates, attributes) # Create a duckdb connection con = DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(con, "SET threads = 1") # Create a duckdb table with spatial points db_points = dbSpatial(conn = con, value = dummy_data, x_colName = "x", y_colName = "y", name = "foo", overwrite = TRUE) st_ymax(dbSpatial = db_points) DBI::dbDisconnect(con, shutdown = TRUE) }
tail method for dbSpatial
## S4 method for signature 'dbSpatial' tail(x, n = 6L, ...)## S4 method for signature 'dbSpatial' tail(x, n = 6L, ...)
x |
A |
n |
Number of rows to return. |
... |
Additional arguments. |
A dbSpatial object containing the last n rows of x.
Other dbData:
head,dbSpatial-method
Create SpatVector objects
## S4 method for signature 'dbSpatial' vect(x, select = tidyselect::everything(), ...)## S4 method for signature 'dbSpatial' vect(x, select = tidyselect::everything(), ...)
x |
A |
select |
Columns to retain in output (default: all columns) |
... |
Additional arguments passed to |
A terra::SpatVector containing the selected columns and geometry
materialized from x.
Other dbSpatial:
as_dbSpatial(),
dbSpatial,
show,dbSpatial-method,
st_as_sf.dbSpatial()
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point_data <- data.frame(x = c(100, 200), y = c(500, 600), id = 1:2) point_vect <- terra::vect(point_data, geom = c("x", "y")) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") dbs <- as_dbSpatial(point_vect, conn = duckdb_conn, name = "point_vect", overwrite = TRUE) terra::vect(dbs) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }if (interactive() && requireNamespace("duckdb", quietly = TRUE)) { point_data <- data.frame(x = c(100, 200), y = c(500, 600), id = 1:2) point_vect <- terra::vect(point_data, geom = c("x", "y")) duckdb_conn <- DBI::dbConnect(duckdb::duckdb(), ":memory:") DBI::dbExecute(duckdb_conn, "SET threads = 1") dbs <- as_dbSpatial(point_vect, conn = duckdb_conn, name = "point_vect", overwrite = TRUE) terra::vect(dbs) DBI::dbDisconnect(duckdb_conn, shutdown = TRUE) }