Skip to content

Commit 438cfa1

Browse files
committed
Fixed Add expression to use GT behavior; created new BiasedAdd for Aggregate use.
1 parent b307809 commit 438cfa1

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

core/src/main/scala/org/locationtech/rasterframes/expressions/aggregates/LocalMeanAggregate.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
package org.locationtech.rasterframes.expressions.aggregates
2323

2424
import org.locationtech.rasterframes.expressions.UnaryRasterAggregate
25-
import org.locationtech.rasterframes.expressions.localops.{Add => AddTiles, Divide => DivideTiles}
25+
import org.locationtech.rasterframes.expressions.localops.{BiasedAdd, Divide => DivideTiles}
2626
import org.locationtech.rasterframes.expressions.transformers.SetCellType
2727
import geotrellis.raster.Tile
2828
import geotrellis.raster.mapalgebra.local
@@ -59,16 +59,16 @@ case class LocalMeanAggregate(child: Expression) extends UnaryRasterAggregate {
5959
override lazy val updateExpressions: Seq[Expression] = Seq(
6060
If(IsNull(count),
6161
SetCellType(Defined(child), Literal("int32")),
62-
If(IsNull(child), count, AddTiles(count, Defined(child)))
62+
If(IsNull(child), count, BiasedAdd(count, Defined(child)))
6363
),
6464
If(IsNull(sum),
6565
SetCellType(child, Literal("float64")),
66-
If(IsNull(child), sum, AddTiles(sum, child))
66+
If(IsNull(child), sum, BiasedAdd(sum, child))
6767
)
6868
)
6969
override val mergeExpressions: Seq[Expression] = Seq(
70-
AddTiles(count.left, count.right),
71-
AddTiles(sum.left, sum.right)
70+
BiasedAdd(count.left, count.right),
71+
BiasedAdd(sum.left, sum.right)
7272
)
7373
override lazy val evaluateExpression: Expression = DivideTiles(sum, count)
7474
}

core/src/main/scala/org/locationtech/rasterframes/expressions/localops/Add.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import org.apache.spark.sql.{Column, TypedColumn}
3030
import org.locationtech.rasterframes._
3131
import org.locationtech.rasterframes.expressions.BinaryLocalRasterOp
3232
import org.locationtech.rasterframes.expressions.DynamicExtractors.tileExtractor
33-
import org.locationtech.rasterframes.util.DataBiasedOp.BiasedAdd
3433

3534
@ExpressionDescription(
3635
usage = "_FUNC_(tile, rhs) - Performs cell-wise addition between two tiles or a tile and a scalar.",
@@ -48,9 +47,9 @@ import org.locationtech.rasterframes.util.DataBiasedOp.BiasedAdd
4847
case class Add(left: Expression, right: Expression) extends BinaryLocalRasterOp
4948
with CodegenFallback {
5049
override val nodeName: String = "rf_local_add"
51-
override protected def op(left: Tile, right: Tile): Tile = BiasedAdd(left, right)
52-
override protected def op(left: Tile, right: Double): Tile = BiasedAdd(left, right)
53-
override protected def op(left: Tile, right: Int): Tile = BiasedAdd(left, right)
50+
override protected def op(left: Tile, right: Tile): Tile = left.localAdd(right)
51+
override protected def op(left: Tile, right: Double): Tile = left.localAdd(right)
52+
override protected def op(left: Tile, right: Int): Tile = left.localAdd(right)
5453

5554
override def eval(input: InternalRow): Any = {
5655
if(input == null) null
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

pyrasterframes/src/main/python/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ def initialize_options(self):
152152
'numpy>=1.7',
153153
'shapely',
154154
'pandas',
155-
'rasterio'
155+
'rasterio',
156+
'boto3'
156157
],
157158
packages=[
158159
'pyrasterframes',

0 commit comments

Comments
 (0)