## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>",
                      error = FALSE, warning = FALSE, message = FALSE)

# Gate the live spatial examples on the DuckDB spatial extension being available, so
# the vignette still builds where it cannot be obtained (e.g. offline dev machines);
# CI / a networked build renders them for real.
.spatial_ok <- tryCatch({
    con <- DuckDBDataFrame::acquireDuckDBConn()
    DBI::dbGetQuery(con,
        "SELECT ST_Area(ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))')) AS a")
    TRUE
}, error = function(e) FALSE)

## ----spatial-unavailable-note, echo=FALSE, results='asis', eval=!.spatial_ok----
# cat("> **Note:** DuckDB's `spatial` extension could not be loaded in this build ",
#     "environment, so the live examples below are shown but not evaluated.\n", sep = "")

## ----load---------------------------------------------------------------------
library(DuckDBSpatial)
library(sf)

## ----sql-demo, eval=.spatial_ok-----------------------------------------------
spatial_path <- system.file("extdata", "spatial", package = "DuckDBSpatial")
df <- DuckDBDataFrame(spatial_path)
df <- df[which(!is.na(df$type)), ]

# df[["geometry"]] is an sf-style geometry column (a list, one WKB-encoded
# shape per row); see the "Lazy spatial columns" section of the introduction
# vignette for what that means if `sf` is new to you.
area <- st_area(df[["geometry"]])   # records ST_Area(geometry); nothing computed yet
class(area)
head(as.vector(area))               # pulled from DuckDB on demand

## ----geoparquet, eval=.spatial_ok && requireNamespace("nanoparquet", quietly=TRUE)----
pts_sf <- st_sf(id = 1:2, geometry = st_sfc(st_point(0:1), st_point(2:3)))
path <- tempfile(fileext = ".parquet")
writeGeoParquet(pts_sf, path)
readGeoParquet(path)
unlink(path)

## ----sessioninfo--------------------------------------------------------------
sessionInfo()

