if (FALSE) {
# load packages
library(duckspatial)
library(sf)
# read polygons data
countries_sf <- sf::st_read(system.file("spatial/countries.geojson", package = "duckspatial"))
# create points data
n <- 100
points_sf <- data.frame(
id = 1:n,
x = runif(n, min = -180, max = 180),
y = runif(n, min = -90, max = 90)
) |>
sf::st_as_sf(coords = c("x", "y"), crs = 4326)
# option 1: passing sf objects
output1 <- duckspatial::ddbs_join(
x = points_sf,
y = countries_sf,
join = "within"
)
plot(output1["CNTR_NAME"])
## option 2: passing the names of tables in a duckdb db
# creates a duckdb
conn <- duckspatial::ddbs_create_conn()
# write sf to duckdb
ddbs_write_vector(conn, points_sf, "points", overwrite = TRUE)
ddbs_write_vector(conn, countries_sf, "countries", overwrite = TRUE)
# spatial join
output2 <- ddbs_join(
conn = conn,
x = "points",
y = "countries",
join = "within"
)
plot(output2["CNTR_NAME"])
}
Run the code above in your browser using DataLab