% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ddbs_quadkey.R
\name{ddbs_quadkey}
\alias{ddbs_quadkey}
\title{Convert point geometries to QuadKey tiles}
\usage{
ddbs_quadkey(
  x,
  level = 10,
  field = NULL,
  fun = "mean",
  background = NA,
  conn = NULL,
  name = NULL,
  output = "polygon",
  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{level}{An integer specifying the zoom level for QuadKey generation (1-23).
Higher values provide finer spatial resolution. Default is 10.}

\item{field}{Character string specifying the field name for aggregation.}

\item{fun}{aggregation function for when there are multiple quadkeys (e.g. "mean",
"min", "max", "sum").}

\item{background}{numeric. Default value in raster cells without values. Only used when
\code{output = "raster"}}

\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{output}{Character string specifying output format. One of:
\itemize{
\item \code{"polygon"} - Returns QuadKey tile boundaries as \code{duckspatial_df} (default)
\item \code{"raster"} - Returns QuadKey values as a \code{SpatRaster}
\item \code{"tilexy"} - Returns tile XY coordinates as a \code{tibble}
}}

\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}.}
}
\value{
Depends on the output argument
\itemize{
\item \code{polygon} (default): A lazy spatial data frame backed by dbplyr/DuckDB.
\item \code{raster}: An eagerly collected \code{SpatRaster} object in R memory.
\item \code{tilexy}: An eagerly collected \code{tibble} without geometry in R memory.
}
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{
Transforms point geometries into QuadKey identifiers at a specified zoom level,
a hierarchical spatial indexing system used by mapping services.
}
\details{
QuadKeys divide the world into a hierarchical grid of tiles, where each tile
is subdivided into four smaller tiles at the next zoom level. This function
wraps DuckDB's ST_QuadKey spatial function to generate these tiles from input geometries.

Note that creating a table inside the connection will generate a non-spatial table, and
therefore, it cannot be read with \link{ddbs_read_table}.
}
\examples{
\dontrun{
## load packages
library(duckspatial)
library(sf)
library(terra)

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

## create random points in Argentina
argentina_sf <- st_read(system.file("spatial/argentina.geojson", package = "duckspatial"))
rand_sf <- st_sample(argentina_sf, 100) |> st_as_sf()
rand_sf["var"] <- runif(100)

## store in duckdb
ddbs_write_vector(conn, rand_sf, "rand_sf")

## generate QuadKey polygons at zoom level 8
qkey_ddbs <- ddbs_quadkey(conn = conn, "rand_sf", level = 8, output = "polygon")

## generate QuadKey raster with custom field name
qkey_rast <- ddbs_quadkey(conn = conn, "rand_sf", level = 6, output = "raster", field = "var")

## generate Quadkey XY tiles
qkey_tiles_tbl <- ddbs_quadkey(conn = conn, "rand_sf", level = 10, output = "tilexy")
}
}
