-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SEDONA-439] Add RS_Union_Aggr (#1140)
- Loading branch information
1 parent
fa76ed5
commit 691a1fa
Showing
7 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
common/src/main/java/org/apache/sedona/common/raster/PixelRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
## RS_Union_Aggr | ||
|
||
Introduction: Returns a raster containing bands by specified indexes from all rasters in the provided column. Extracts the first bands from each raster and combines them into the output raster based on the input index values. | ||
|
||
!!!Note | ||
RS_Union_Aggr can take multiple banded rasters as input but it would only extract the first band to the resulting raster. 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. | ||
- Indexes to be unique and not repeated. | ||
- Rasters should be of the same shape. | ||
|
||
Format: `RS_Union_Aggr(A: rasterColumn, B: indexColumn)` | ||
|
||
Since: `v1.5.1` | ||
|
||
Spark SQL Example: | ||
|
||
Contents of `raster_table`. | ||
|
||
``` | ||
+------------------------------+-----+ | ||
| raster|index| | ||
+------------------------------+-----+ | ||
|GridCoverage2D["geotiff_cov...| 1| | ||
|GridCoverage2D["geotiff_cov...| 2| | ||
|GridCoverage2D["geotiff_cov...| 3| | ||
|GridCoverage2D["geotiff_cov...| 4| | ||
|GridCoverage2D["geotiff_cov...| 5| | ||
+------------------------------+-----+ | ||
``` | ||
|
||
``` | ||
SELECT RS_Union_Aggr(raster, index) FROM raster_table | ||
``` | ||
|
||
Output: | ||
|
||
This output raster contains the first band of each raster in the `raster_table` at specified index. | ||
|
||
``` | ||
GridCoverage2D["geotiff_coverage", GeneralEnvel... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...rc/main/scala/org/apache/spark/sql/sedona_sql/expressions/raster/AggregateFunctions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.spark.sql.sedona_sql.expressions.raster | ||
|
||
import org.apache.sedona.common.raster.{RasterAccessors, RasterBandAccessors} | ||
import org.apache.sedona.common.utils.RasterUtils | ||
import org.apache.spark.sql.Encoder | ||
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
import org.apache.spark.sql.expressions.Aggregator | ||
import org.geotools.coverage.GridSampleDimension | ||
import org.geotools.coverage.grid.GridCoverage2D | ||
|
||
import java.awt.image.WritableRaster | ||
import javax.media.jai.RasterFactory | ||
import scala.collection.mutable | ||
import scala.collection.mutable.ArrayBuffer | ||
|
||
case class BandData(var bandInt: Array[Int], var bandDouble: Array[Double], var index: Int, var isIntegral: Boolean) | ||
|
||
/** | ||
* Return a raster containing bands at given indexes from all rasters in a given column | ||
*/ | ||
class RS_Union_Aggr extends Aggregator[(GridCoverage2D, Int), ArrayBuffer[BandData], GridCoverage2D] { | ||
|
||
var width: Int = -1 | ||
|
||
var height: Int = -1 | ||
|
||
var referenceRaster: GridCoverage2D = _ | ||
|
||
var gridSampleDimension: mutable.Map[Int, GridSampleDimension] = new mutable.HashMap() | ||
|
||
def zero: ArrayBuffer[BandData] = ArrayBuffer[BandData]() | ||
|
||
/** | ||
* Valid raster shape to be the same in the given column | ||
*/ | ||
def checkRasterShape(raster: GridCoverage2D): Boolean = { | ||
// first iteration | ||
if (width == -1 && height == -1) { | ||
width = RasterAccessors.getWidth(raster) | ||
height = RasterAccessors.getHeight(raster) | ||
referenceRaster = raster | ||
true | ||
} else { | ||
val widthNewRaster = RasterAccessors.getWidth(raster) | ||
val heightNewRaster = RasterAccessors.getHeight(raster) | ||
|
||
width == widthNewRaster && height == heightNewRaster | ||
} | ||
} | ||
|
||
def reduce(buffer: ArrayBuffer[BandData], input: (GridCoverage2D, Int)): ArrayBuffer[BandData] = { | ||
val raster = input._1 | ||
if (!checkRasterShape(raster)) { | ||
throw new IllegalArgumentException("Rasters provides should be of the same shape.") | ||
} | ||
if (gridSampleDimension.contains(input._2)) { | ||
throw new IllegalArgumentException("Indexes shouldn't be repeated. Index should be in an arithmetic sequence.") | ||
} | ||
|
||
val rasterData = RasterUtils.getRaster(raster.getRenderedImage) | ||
val isIntegral = RasterUtils.isDataTypeIntegral(rasterData.getDataBuffer.getDataType) | ||
|
||
val bandData = if (isIntegral) { | ||
val band = rasterData.getSamples(0, 0, width, height, 0, null.asInstanceOf[Array[Int]]) | ||
BandData(band, null, input._2, isIntegral) | ||
} else { | ||
val band = rasterData.getSamples(0, 0, width, height, 0, null.asInstanceOf[Array[Double]]) | ||
BandData(null, band, input._2, isIntegral) | ||
} | ||
gridSampleDimension = gridSampleDimension + (input._2 -> raster.getSampleDimension(0)) | ||
|
||
buffer += bandData | ||
} | ||
|
||
def merge(buffer1: ArrayBuffer[BandData], buffer2: ArrayBuffer[BandData]): ArrayBuffer[BandData] = { | ||
ArrayBuffer.concat(buffer1, buffer2) | ||
} | ||
|
||
def finish(merged: ArrayBuffer[BandData]): GridCoverage2D = { | ||
val sortedMerged = merged.sortBy(_.index) | ||
val numBands = sortedMerged.length | ||
val rasterData = RasterUtils.getRaster(referenceRaster.getRenderedImage) | ||
val dataTypeCode = rasterData.getDataBuffer.getDataType | ||
val resultRaster: WritableRaster = RasterFactory.createBandedRaster(dataTypeCode, width, height, numBands, null) | ||
val gridSampleDimensions: Array[GridSampleDimension] = new Array[GridSampleDimension](numBands) | ||
var indexCheck = 1 | ||
|
||
for (bandData: BandData <- sortedMerged) { | ||
if (bandData.index != indexCheck) { | ||
throw new IllegalArgumentException("Indexes should be in a valid arithmetic sequence.") | ||
} | ||
indexCheck += 1 | ||
gridSampleDimensions(bandData.index - 1) = gridSampleDimension(bandData.index) | ||
if(RasterUtils.isDataTypeIntegral(dataTypeCode)) | ||
resultRaster.setSamples(0, 0, width, height, (bandData.index - 1), bandData.bandInt) | ||
else | ||
resultRaster.setSamples(0, 0, width, height, bandData.index - 1, bandData.bandDouble) | ||
|
||
} | ||
val noDataValue = RasterBandAccessors.getBandNoDataValue(referenceRaster) | ||
RasterUtils.clone(resultRaster, referenceRaster.getGridGeometry, gridSampleDimensions, referenceRaster, noDataValue, true) | ||
} | ||
|
||
val serde = ExpressionEncoder[GridCoverage2D] | ||
|
||
val bufferSerde = ExpressionEncoder[ArrayBuffer[BandData]] | ||
|
||
def outputEncoder: ExpressionEncoder[GridCoverage2D] = serde | ||
|
||
def bufferEncoder: Encoder[ArrayBuffer[BandData]] = bufferSerde | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters