Convert an sf or terra object to a dbSpatial
object
as_dbSpatial.Rd
Create a dbSpatial
object from an sf
or terra
object.
Arguments
- rSpatial
sf
orterra
object.- conn
A
DBIConnection
object, as returned byDBI::dbConnect
.- name
a character string
with the unquoted DBMS table name, e.g. "table_name"- overwrite
logical
. Overwrite existing table. default = FALSE.- ...
Additional arguments to be passed
Details
Writes out the rSpatial
object to temporary .parquet file and
computes the VIEW in the database with the specified name
and the geometry
column as geom
.
Examples
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:")
dbSpatial <- as_dbSpatial(rSpatial = dummy_spatvector,
conn = duckdb_conn,
name = "dummy_spatvector",
overwrite = TRUE)
dbSpatial
#> # Class: dbSpatial
#> # Source: table<dummy_spatvector> [3 x 4]
#> # Database: DuckDB v1.0.0 [unknown@Linux 6.5.0-1025-azure:R 4.4.1/:memory:]
#> id name geometry geom
#> <int> <chr> <list> <list>
#> 1 1 A <raw [21]> <raw [32]>
#> 2 2 B <raw [21]> <raw [32]>
#> 3 3 C <raw [21]> <raw [32]>