This function combines multiple rasters into a single multiband raster by stacking the bands of each input raster sequentially. The function arranges the bands in the output raster according to the order specified by the index column in the input. It is typically used in scenarios where rasters are grouped by certain criteria (e.g., time and/or location) and an aggregated raster output is desired.
RS_Stack_Aggr expects the following input, if not satisfied then will throw an IllegalArgumentException:
Indexes to be in an arithmetic sequence without any gaps.
Indexes to be unique and not repeated.
Rasters should be of the same shape.
RS_Stack_Aggr replaces RS_Union_Aggr. It stacks bands from rasters with matching shapes; it does not mosaic spatially adjacent raster grids. The old function name is removed. See Geometry and raster API migration.
Signatures
RS_Stack_Aggr(A: rasterColumn, B: indexColumn)
Parameters
Raster
required
The raster column containing the input rasters to be combined.
Integer
required
The index column specifying the band order for stacking.
Return type
The resulting raster.
Example
First, we enrich the dataset with time-based grouping columns and index the rasters based on time intervals:
// Add yearly and quarterly time interval columns for groupingdf = df .withColumn("year", year($"timestamp")) .withColumn("quarter", quarter($"timestamp"))// Define window specs for quarterly indexing within each geometry-year groupwindowSpecQuarter = Window.partitionBy("geometry", "year", "quarter").orderBy("timestamp")indexedDf = df.withColumn("index", row_number().over(windowSpecQuarter))indexedDf.show()
The indexed rasters will appear as follows, showing that each raster is tagged with a sequential index (ordered by timestamp) within its group (grouped by geometry, year and quarter).
To create a stacked raster by grouping on geometry.
indexedDf.createOrReplaceTempView("indexedDf")sedona.sql(''' SELECT geometry, year, quarter, RS_Stack_Aggr(raster, index) AS aggregated_raster FROM indexedDf WHERE index <= 4 GROUP BY geometry, year, quarter''').show()
The query yields rasters grouped by geometry, year and quarter, each containing the first four time steps combined into a single multiband raster, where each band represents one time step.