|
| 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 astraea.spark.rasterframes.expressions.localops |
| 23 | + |
| 24 | +import astraea.spark.rasterframes._ |
| 25 | +import astraea.spark.rasterframes.expressions.DynamicExtractors.tileExtractor |
| 26 | +import astraea.spark.rasterframes.expressions.BinaryLocalRasterOp |
| 27 | +import geotrellis.raster.Tile |
| 28 | +import geotrellis.raster.resample.NearestNeighbor |
| 29 | +import org.apache.spark.sql.rf._ |
| 30 | +import org.apache.spark.sql.catalyst.InternalRow |
| 31 | +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback |
| 32 | +import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionDescription} |
| 33 | +import org.apache.spark.sql.functions.lit |
| 34 | +import org.apache.spark.sql.{Column, TypedColumn} |
| 35 | + |
| 36 | +@ExpressionDescription( |
| 37 | + usage = "_FUNC_(tile, factor) - Resample tile to different size based on scalar factor or tile whose dimension to match. Scalar less than one will downsample tile; greater than one will upsample. Uses nearest-neighbor value.", |
| 38 | + arguments = """ |
| 39 | + Arguments: |
| 40 | + * tile - tile |
| 41 | + * rhs - scalar or tile to match dimension""", |
| 42 | + examples = """ |
| 43 | + Examples: |
| 44 | + > SELECT _FUNC_(tile, 2.0); |
| 45 | + ... |
| 46 | + > SELECT _FUNC_(tile1, tile2); |
| 47 | + ...""" |
| 48 | +) |
| 49 | +case class Resample(left: Expression, right: Expression) extends BinaryLocalRasterOp |
| 50 | + with CodegenFallback { |
| 51 | + override val nodeName: String = "resample" |
| 52 | + override protected def op(left: Tile, right: Tile): Tile = left.resample(right.cols, right.rows, NearestNeighbor) |
| 53 | + override protected def op(left: Tile, right: Double): Tile = left.resample((left.cols * right).toInt, |
| 54 | + (left.rows * right).toInt, NearestNeighbor) |
| 55 | + override protected def op(left: Tile, right: Int): Tile = op(left, right.toDouble) |
| 56 | + |
| 57 | + override def eval(input: InternalRow): Any = { |
| 58 | + if(input == null) null |
| 59 | + else { |
| 60 | + val l = left.eval(input) |
| 61 | + val r = right.eval(input) |
| 62 | + if (l == null && r == null) null |
| 63 | + else if (l == null) r |
| 64 | + else if (r == null && tileExtractor.isDefinedAt(right.dataType)) l |
| 65 | + else if (r == null) null |
| 66 | + else nullSafeEval(l, r) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +object Resample{ |
| 71 | + def apply(left: Column, right: Column): TypedColumn[Any, Tile] = |
| 72 | + new Column(Resample(left.expr, right.expr)).as[Tile] |
| 73 | + |
| 74 | + def apply[N: Numeric](tile: Column, value: N): TypedColumn[Any, Tile] = |
| 75 | + new Column(Resample(tile.expr, lit(value).expr)).as[Tile] |
| 76 | +} |
0 commit comments