% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ddbs_union.R
\name{ddbs_union_funs}
\alias{ddbs_union_funs}
\alias{ddbs_union}
\alias{ddbs_combine}
\alias{ddbs_union_agg}
\title{Union and combine geometries}
\usage{
ddbs_union(
  x,
  y = NULL,
  by_feature = FALSE,
  conn = NULL,
  conn_x = NULL,
  conn_y = NULL,
  name = NULL,
  mode = NULL,
  overwrite = FALSE,
  quiet = FALSE
)

ddbs_combine(
  x,
  conn = NULL,
  name = NULL,
  mode = NULL,
  overwrite = FALSE,
  quiet = FALSE
)

ddbs_union_agg(
  x,
  by,
  mem = FALSE,
  conn = NULL,
  name = NULL,
  mode = NULL,
  overwrite = FALSE,
  quiet = FALSE
)
}
\arguments{
\item{x}{Input spatial data. Can be:
\itemize{
\item A \code{duckspatial_df} object (lazy spatial data frame via dbplyr)
\item An \code{sf} object
\item A \code{tbl_lazy} from dbplyr
\item A character string naming a table/view in \code{conn}
}
Data is returned from this object.}

\item{y}{Input spatial data. Can be:
\itemize{
\item \code{NULL} (default): performs only the union of \code{x}
\item A \code{duckspatial_df} object (lazy spatial data frame via dbplyr)
\item An \code{sf} object
\item A \code{tbl_lazy} from dbplyr
\item A character string naming a table/view in \code{conn}
}}

\item{by_feature}{Logical. When \code{y} is provided:
\itemize{
\item \code{FALSE} (default) - Union all geometries from both \code{x} and \code{y} into a single geometry
\item \code{TRUE} - Perform row-by-row union between matching features from \code{x} and \code{y} (requires same number of rows)
}}

\item{conn}{A connection object to a DuckDB database. If \code{NULL}, the function
runs on a temporary DuckDB database.}

\item{conn_x}{A \code{DBIConnection} object to a DuckDB database for the input \code{x}.
If \code{NULL} (default), it is resolved from \code{conn} or extracted from \code{x}.}

\item{conn_y}{A \code{DBIConnection} object to a DuckDB database for the input \code{y}.
If \code{NULL} (default), it is resolved from \code{conn} or extracted from \code{y}.}

\item{name}{A character string of length one specifying the name of the table,
or a character string of length two specifying the schema and table
names. If \code{NULL} (the default), the function returns the result as an
\code{sf} object}

\item{mode}{Character. Controls the return type. Options:
\itemize{
\item \code{"duckspatial"} (default): Lazy spatial data frame backed by dbplyr/DuckDB
\item \code{"sf"}: Eagerly collected sf object (uses memory)
}
Can be set globally via \code{\link{ddbs_options}(mode = "...")} or
per-function via this argument. Per-function overrides global setting.}

\item{overwrite}{Boolean. whether to overwrite the existing table if it exists. Defaults
to \code{FALSE}. This argument is ignored when \code{name} is \code{NULL}.}

\item{quiet}{A logical value. If \code{TRUE}, suppresses any informational messages.
Defaults to \code{FALSE}.}

\item{by}{Character vector specifying one or more column names to
group by when computing unions. Geometries will be unioned within each group.
Default is \code{NULL}}

\item{mem}{Logical. If \code{TRUE}, uses \code{ST_MemUnion_Agg()} instead of
\code{ST_Union_Agg()} — slower but more memory efficient. Default is
\code{FALSE}. Only applies to \code{ddbs_union_agg()}.}
}
\value{
Depends on the \code{mode} argument (or global preference set by \code{\link{ddbs_options}}):
\itemize{
\item \code{duckspatial} (default): A \code{duckspatial_df} (lazy spatial data frame) backed by dbplyr/DuckDB.
\item \code{sf}: An eagerly collected object in R memory, that will return the same data type as the
\code{sf} equivalent (e.g. \code{sf} or \code{units} vector).
}
When \code{name} is provided, the result is also written as a table or view in DuckDB and the function returns \code{TRUE} (invisibly).
}
\description{
Perform union and combine operations on spatial geometries in DuckDB.
\itemize{
\item \code{ddbs_union()} - Union all geometries into one, or perform pairwise union between two datasets
\item \code{ddbs_union_agg()} - Union geometries grouped by one or more columns
\item \code{ddbs_combine()} - Combine geometries into a MULTI-geometry without dissolving boundaries
}
}
\details{
\subsection{ddbs_union(x, y, by_feature)}{

Performs geometric union operations that dissolve internal boundaries:
\itemize{
\item When \code{y = NULL}: Unions all geometries in \code{x} into a single geometry
\item When \code{y != NULL} and \code{by_feature = FALSE}: Unions all geometries from both \code{x} and \code{y} into a single geometry
\item When \code{y != NULL} and \code{by_feature = TRUE}: Performs row-wise union, pairing the first geometry from \code{x} with the first from \code{y}, second with second, etc.
}
}

\subsection{ddbs_union_agg(x, by)}{

Groups geometries by one or more columns, then unions geometries within each group.
Useful for dissolving boundaries between features that share common attributes.
Set \code{mem = TRUE} to use \code{ST_MemUnion_Agg()} when memory is a constraint.
}

\subsection{ddbs_combine(x)}{

Combines all geometries into a single MULTI-geometry (e.g., MULTIPOLYGON, MULTILINESTRING)
without dissolving shared boundaries. This is faster than union but preserves all
original geometry boundaries.
}
}
\examples{
\dontrun{
## load packages
library(dplyr)
library(duckspatial)

## create a duckdb database in memory (with spatial extension)
conn <- ddbs_create_conn(dbdir = "memory")

## read data
countries_ddbs <- ddbs_open_dataset(
  system.file("spatial/countries.geojson", 
  package = "duckspatial")
) |> 
  filter(ISO3_CODE != "ATA")

rivers_ddbs <- ddbs_open_dataset(
  system.file("spatial/rivers.geojson", 
  package = "duckspatial")
) |> 
 ddbs_transform("EPSG:4326")

## combine countries into a single MULTI-geometry
## (without solving boundaries)
combined_countries_ddbs <- ddbs_combine(countries_ddbs)

## combine countries into a single MULTI-geometry
## (solving boundaries)
union_countries_ddbs <- ddbs_union(countries_ddbs)

## union of geometries of two objects, into 1 geometry
union_countries_rivers_ddbs <- ddbs_union(countries_ddbs, rivers_ddbs)
}

}
