Spatial Operations

library(dbSpatial)
library(sf)
library(dplyr)

Setup

con <- DBI::dbConnect(duckdb::duckdb(), ":memory:")
DBI::dbExecute(con, "SET threads = 1")

# Sample points
df <- data.frame(id = 1:3, x = c(0, 10, 20), y = c(0, 10, 20))
pts <- dbSpatial(
  conn = con, name = "pts", value = df,
  x_colName = "x", y_colName = "y", overwrite = TRUE
)
pts

Geometry Operations

# Buffer
st_buffer(pts, dist = 5)

# Centroid
st_centroid(pts)

# Simplify
st_simplify(pts, dTolerance = 1)

Measurements

# Bounding box
st_bbox(pts)

# Check validity
st_is_valid(pts)

Spatial Joins

Use st_join() to perform spatial joins with various predicates:

# Self-join using intersection predicate (returns joined table)
st_join(pts, pts, join = st_intersects)

Convert to sf

sf_pts <- st_as_sf(pts)
sf_pts