---
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

title: RS_BandToDim
description: Collapses all bands into a single band with a new dimension.
kernels:
  - returns: raster
    args:
    - raster
    - name: dim_name
      type: utf8
      description: Name for the new dimension (e.g., 'time', 'band').
---

::: callout-warning
**Experimental.** This function is experimental; its behavior may change without notice.
:::

## Description

Collapses all bands in a raster into a single band by introducing a new
dimension. The new dimension is prepended to the existing dimensions, with
size equal to the number of bands. Band data is concatenated in band order.

All bands must have identical dimension names, shapes, data types, and
nodata values. If any of these differ, an error is returned — collapsing
bands with different nodata sentinels would silently lose information
about which sentinel applies where.

The new dimension name must not already exist on the input bands. For
example, `RS_BandToDim(raster_with_y_x, 'y')` is rejected because the
output would carry duplicate `y` dimensions and the round-trip through
[RS_DimToBand](rs_dimtoband.qmd) cannot recover the original layout.

This is the inverse of [RS_DimToBand](rs_dimtoband.qmd). A round-trip
`RS_BandToDim(RS_DimToBand(raster, 'dim'), 'dim')` recovers the original
data layout.

This is useful for converting GeoTIFF-style multi-band rasters into a single
N-dimensional chunk for export to formats like Zarr.

## Examples

```sql
-- Collapse RS_Example()'s bands into a single band carrying a new 'band'
-- dimension, e.g. [y, x] bands become one [band, y, x] band.
SELECT RS_BandToDim(RS_Example(), 'band');
```

```sql
-- The inverse RS_DimToBand recovers the original bands, so the round-trip
-- returns to the starting layout.
SELECT RS_DimToBand(RS_BandToDim(RS_Example(), 'band'), 'band');
```
