---
title: "Class Structure"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Class Structure}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
has_duckdb <- requireNamespace("duckdb", quietly = TRUE)
knitr::opts_chunk$set(
  eval = has_duckdb && !identical(Sys.getenv("NOT_CRAN"), "false")
)
```


# Class Structure
The `dbSpatial` package is designed to work with spatial geometries in a DuckDB table. The package builds on the [DuckDB spatial extension](https://duckdb.org/docs/extensions/spatial.html).

`dbSpatial` objects adopt the S4 class system and represent spatial `geometries` in a DuckDB table. The `{dbSpatial}` package consists of methods for constructing `dbSpatial` objects from various data sources, as well as various spatial operations for working with these data.

`dbSpatial` provides methods for reading/writing `dbSpatial` objects from/to data.frames, S3 and S4 spatial-related objects (e.g. objects from `{sf}` or `{terra}`), and [several other spatial files](https://duckdb.org/docs/extensions/spatial.html#st_read--read-spatial-data-from-files) that are supported by the DuckDB spatial extension.

The runnable example below is evaluated only when the `duckdb` package is
available in the check environment.

## Minimal example

```{r}
library(dbSpatial)

con <- DBI::dbConnect(duckdb::duckdb(), ":memory:")
DBI::dbExecute(con, "SET threads = 1")
coords <- data.frame(id = 1:3, x = c(0, 1, 2), y = c(0, 1, 2))

pts <- dbSpatial(
  conn = con,
  name = "class_structure_points",
  value = coords,
  x_colName = "x",
  y_colName = "y",
  overwrite = TRUE
)

class(pts)
pts

DBI::dbDisconnect(con, shutdown = TRUE)
```

[![''](https://mermaid.ink/img/pako: ?type=png)](https://mermaid.live/edit#pako: )

