% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ddbs_dimensions.R
\name{ddbs_force_dim}
\alias{ddbs_force_dim}
\alias{ddbs_force_2d}
\alias{ddbs_force_3d}
\alias{ddbs_force_4d}
\title{Force geometry dimensions}
\usage{
ddbs_force_2d(
  x,
  conn = NULL,
  name = NULL,
  mode = NULL,
  overwrite = FALSE,
  quiet = FALSE
)

ddbs_force_3d(
  x,
  var,
  dim = "z",
  conn = NULL,
  name = NULL,
  mode = NULL,
  overwrite = FALSE,
  quiet = FALSE
)

ddbs_force_4d(
  x,
  var_z,
  var_m,
  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{conn}{A connection object to a DuckDB database. If \code{NULL}, the function
runs on a temporary DuckDB database.}

\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{var}{A numeric variable in \code{x} to be converted in the dimension specified
in the argument \code{dim}}

\item{dim}{The dimension to add: either \code{"z"} (default) for
elevation or \code{"m"} for measure values.}

\item{var_z}{A numeric variable in \code{x} to be convered in \code{Z} dimension}

\item{var_m}{A numeric variable in \code{x} to be convered in \code{M} dimension}
}
\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{
Functions to force geometries to have specific coordinate dimensions (X, Y, Z, M)
}
\details{
These functions modify the dimensionality of geometries:
\itemize{
\item \code{ddbs_force_2d()} removes Z and M coordinates from geometries, returning only
X and Y coordinates. This is useful for simplifying 3D or measured geometries
to 2D.
\item \code{ddbs_force_3d()} forces geometries to have three dimensions. When \code{dim = "z"}
(default), adds or retains Z coordinates (X, Y, Z). When \code{dim = "m"}, adds or
retains M coordinates (X, Y, M). Missing values are typically set to 0. If the
input geometry has a third dimension already, it will be replaced by the new one.
If the input geometry has 4 dimensions, it will drop the dimension that wasn't
specified.
\item \code{ddbs_force_4d()} forces geometries to have all four dimensions (X, Y, Z, M).
Missing Z or M values are typically set to 0.
}
}
\examples{
\dontrun{
## load packages
library(dplyr)
library(duckspatial)

## load data and add 2 numeric vars
countries_ddbs <- ddbs_open_dataset(
  system.file("spatial/countries.geojson", 
  package = "duckspatial")
) |> 
  dplyr::filter(ISO3_CODE != "ATA") |> 
  ddbs_area(new_column = "area") |> 
  ddbs_perimeter(new_column = "perim") 

## add a Z dimension
countries_z_ddbs <- ddbs_force_3d(countries_ddbs, "area")
ddbs_has_z(countries_z_ddbs)

## add a M dimension as 3D (removes current Z)
countries_m_ddbs <- ddbs_force_3d(countries_z_ddbs, "area", "m")
ddbs_has_z(countries_m_ddbs)
ddbs_has_m(countries_m_ddbs)

## add both Z and M
countries_zm_ddbs <- ddbs_force_4d(countries_ddbs, "area", "perim")
ddbs_has_z(countries_zm_ddbs)
ddbs_has_m(countries_zm_ddbs)

## drop both ZM
countries_drop_ddbs <- ddbs_force_2d(countries_zm_ddbs)
ddbs_has_z(countries_drop_ddbs)
ddbs_has_m(countries_drop_ddbs)
}

}
