Skip to contents

This function writes a Simple Features (SF) object into a DuckDB database as a new table. The table is created in the specified schema of the DuckDB database.

Usage

ddbs_write_vector(conn, data, name, overwrite = FALSE)

Arguments

conn

a connection object to a DuckDB database

data

a sf object to write to the DuckDB database, or a local file

name

a character string of length one specifying the name of the table, or a character string of length two specifying the schema and table names.

overwrite

whether to overwrite the existing table if it exists

Value

TRUE (invisibly) for successful import

Examples

if (FALSE) { # interactive()
## load packages
library(duckdb)
library(duckspatial)
library(sf)

## connect to in memory database
conn <- dbConnect(duckdb::duckdb())

## install the spatial exntesion
ddbs_install(conn)
ddbs_load(conn)

## create random points
random_points <- data.frame(
  id = 1:5,
  x = runif(5, min = -180, max = 180),  # Random longitude values
  y = runif(5, min = -90, max = 90)     # Random latitude values
)

## convert to sf
sf_points <- st_as_sf(random_points, coords = c("x", "y"), crs = 4326)

## insert data into the database
ddbs_write_vector(conn, sf_points, "points")

## read data back into R
ddbs_read_vector(conn, "points", crs = 4326)

## disconnect from db
dbDisconnect(conn)
}