# Class Structure

The `dbSpatial` package is designed to work with spatial geometries in a DuckDB table. The package builds on the DuckDB spatial extension.

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

    library(dbSpatial)

    con <- DBI::dbConnect(duckdb::duckdb(), ":memory:")
    DBI::dbExecute(con, "SET threads = 1")
    #> [1] 0
    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)
    #> [1] "dbSpatial"
    #> attr(,"package")
    #> [1] "dbSpatial"
    pts
    #> # Class:    dbSpatial
    #> # Source:   SQL [?? x 4]
    #> # Database: DuckDB 1.4.2 [unknown@Linux 4.18.0-553.124.1.el8_10.x86_64:R 4.5.2/:memory:]
    #>      id     x     y geom
    #>   <int> <dbl> <dbl> <chr>
    #> 1     1     0     0 POINT (0 0)
    #> 2     2     1     1 POINT (1 1)
    #> 3     3     2     2 POINT (2 2)

    DBI::dbDisconnect(con, shutdown = TRUE)