---
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)
```


# 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:eNp9U-9r2zAQ_VeEPu2Hq0WzYreiDAqFfRqMpmwwDOFsyYlaWQqSPJaZ_O-TnTSN1jb6cr73jnfHu_OAGysk5rjR4P2tgpWDrjKVMdBJv4FGotub-xs0jBiKb6pDkQkK9LJVWvphz4zvIyJ-vUnylbQP3poEI2Sf7lJVtzjI2vpBNiEVDtI54Hzs_COS1r1B3oGP2Sn5VdkQLOerKX63ygR_jtfblTVJhW-jdvvqzMpcdLKzbpsMKyA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