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_Union_Aggr expects the following input, if not satisfied then will throw an IllegalArgumentException:
Indexes to be in an arithmetic sequence without any gaps.
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_Union_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.