|
| 1 | +/* |
| 2 | + * This software is licensed under the Apache 2 license, quoted below. |
| 3 | + * |
| 4 | + * Copyright 2019 Astraea, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 | + * use this file except in compliance with the License. You may obtain a copy of |
| 8 | + * the License at |
| 9 | + * |
| 10 | + * [http://www.apache.org/licenses/LICENSE-2.0] |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | + * License for the specific language governing permissions and limitations under |
| 16 | + * the License. |
| 17 | + * |
| 18 | + * SPDX-License-Identifier: Apache-2.0 |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package org.locationtech.rasterframes.expressions.localops |
| 23 | +import geotrellis.raster.Tile |
| 24 | +import org.apache.spark.sql.catalyst.InternalRow |
| 25 | +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback |
| 26 | +import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionDescription} |
| 27 | +import org.apache.spark.sql.functions.lit |
| 28 | +import org.apache.spark.sql.{Column, TypedColumn} |
| 29 | +import org.locationtech.rasterframes._ |
| 30 | +import org.locationtech.rasterframes.expressions.BinaryLocalRasterOp |
| 31 | +import org.locationtech.rasterframes.expressions.DynamicExtractors.tileExtractor |
| 32 | +import org.locationtech.rasterframes.util.DataBiasedOp |
| 33 | + |
| 34 | +@ExpressionDescription( |
| 35 | + usage = "_FUNC_(tile, rhs) - Performs cell-wise addition between two tiles or a tile and a scalar. " + |
| 36 | + "Unlike a regular 'add', this considers `<data> + <nodata> = <data>.", |
| 37 | + arguments = """ |
| 38 | + Arguments: |
| 39 | + * tile - left-hand-side tile |
| 40 | + * rhs - a tile or scalar value to add to each cell""", |
| 41 | + examples = """ |
| 42 | + Examples: |
| 43 | + > SELECT _FUNC_(tile, 1.5); |
| 44 | + ... |
| 45 | + > SELECT _FUNC_(tile1, tile2); |
| 46 | + ...""" |
| 47 | +) |
| 48 | +case class BiasedAdd(left: Expression, right: Expression) extends BinaryLocalRasterOp |
| 49 | + with CodegenFallback { |
| 50 | + override val nodeName: String = "rf_local_biased_add" |
| 51 | + override protected def op(left: Tile, right: Tile): Tile = DataBiasedOp.BiasedAdd(left, right) |
| 52 | + override protected def op(left: Tile, right: Double): Tile = DataBiasedOp.BiasedAdd(left, right) |
| 53 | + override protected def op(left: Tile, right: Int): Tile = DataBiasedOp.BiasedAdd(left, right) |
| 54 | + |
| 55 | + override def eval(input: InternalRow): Any = { |
| 56 | + if(input == null) null |
| 57 | + else { |
| 58 | + val l = left.eval(input) |
| 59 | + val r = right.eval(input) |
| 60 | + if (l == null && r == null) null |
| 61 | + else if (l == null) r |
| 62 | + else if (r == null && tileExtractor.isDefinedAt(right.dataType)) l |
| 63 | + else if (r == null) null |
| 64 | + else nullSafeEval(l, r) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | +object BiasedAdd { |
| 69 | + def apply(left: Column, right: Column): TypedColumn[Any, Tile] = |
| 70 | + new Column(BiasedAdd(left.expr, right.expr)).as[Tile] |
| 71 | + |
| 72 | + def apply[N: Numeric](tile: Column, value: N): TypedColumn[Any, Tile] = |
| 73 | + new Column(BiasedAdd(tile.expr, lit(value).expr)).as[Tile] |
| 74 | +} |
0 commit comments