% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/as_dbSpatial.R
\name{as_dbSpatial}
\alias{as_dbSpatial}
\title{Convert an sf or terra object to a \code{dbSpatial} object}
\usage{
as_dbSpatial(rSpatial, conn, name, overwrite = FALSE, ...)
}
\arguments{
\item{rSpatial}{\code{sf} or \code{terra} object.}

\item{conn}{A DBI connection object, as returned by \code{DBI::dbConnect()}.}

\item{name}{\code{a character string} with the unquoted DBMS table name, e.g. "table_name"}

\item{overwrite}{\code{logical}. Overwrite existing table. default = FALSE.}

\item{...}{Additional arguments to be passed}
}
\value{
A \code{\link{dbSpatial}} object backed by table \code{name} in \code{conn}, with the
geometry column stored as \code{geom}.
}
\description{
Create a \code{\link{dbSpatial}} object from an \code{sf} or \code{terra} object.
}
\details{
Writes the \code{rSpatial} object to a temporary DuckDB table and computes the
table in the database with the specified \code{name} and the geometry column as
\code{geom}.
}
\examples{
if (interactive() && requireNamespace("duckdb", quietly = TRUE)) {
  coordinates <- data.frame(x = c(100, 200, 300), y = c(500, 600, 700))
  attributes <- data.frame(id = 1:3, name = c("A", "B", "C"))

  # Combine the coordinates and attributes
  dummy_data <- cbind(coordinates, attributes)

  # Create a SpatVector from the data.frame
  dummy_spatvector <- terra::vect(dummy_data, geom = c("x", "y"))

  # Set db connection
  duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:")
  DBI::dbExecute(duckdb_conn, "SET threads = 1")

  dbSpatial <- as_dbSpatial(rSpatial = dummy_spatvector,
                           conn = duckdb_conn,
                           name = "dummy_spatvector",
                           overwrite = TRUE)
  dbSpatial
  DBI::dbDisconnect(duckdb_conn, shutdown = TRUE)
}
}
\seealso{
Other dbSpatial: 
\code{\link{dbSpatial}},
\code{\link{show,dbSpatial-method}},
\code{\link{st_as_sf.dbSpatial}()},
\code{\link{vect,dbSpatial-method}}
}
\concept{dbSpatial}
