Skip to content

Commit 1d73e9d

Browse files
authored
Merge pull request #460 from s22s/feature/local_sqrt
Add rf_sqrt square root
2 parents e6864df + 01b9014 commit 1d73e9d

File tree

9 files changed

+78
-0
lines changed

9 files changed

+78
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,4 @@ case class ExpM1(child: Expression) extends UnaryLocalRasterOp with CodegenFallb
112112
object ExpM1{
113113
def apply(tile: Column): Column = new Column(ExpM1(tile.expr))
114114
}
115+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This software is licensed under the Apache 2 license, quoted below.
3+
*
4+
* Copyright 2020 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+
24+
import geotrellis.raster.Tile
25+
import org.apache.spark.sql.Column
26+
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
27+
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionDescription}
28+
import org.apache.spark.sql.types.DataType
29+
import org.locationtech.rasterframes.expressions.{UnaryLocalRasterOp, fpTile}
30+
31+
@ExpressionDescription(
32+
usage = "_FUNC_(tile) - Perform cell-wise square root",
33+
arguments = """
34+
Arguments:
35+
* tile - input tile
36+
""",
37+
examples =
38+
"""
39+
Examples:
40+
> SELECT _FUNC_(tile)
41+
... """
42+
)
43+
case class Sqrt(child: Expression) extends UnaryLocalRasterOp with CodegenFallback {
44+
override val nodeName: String = "rf_sqrt"
45+
override protected def op(tile: Tile): Tile = fpTile(tile).localPow(0.5)
46+
override def dataType: DataType = child.dataType
47+
}
48+
object Sqrt {
49+
def apply(tile: Column): Column = new Column(Sqrt(tile.expr))
50+
}

core/src/main/scala/org/locationtech/rasterframes/expressions/package.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ package object expressions {
107107
registry.registerExpression[Exp10]("rf_exp10")
108108
registry.registerExpression[Exp2]("rf_exp2")
109109
registry.registerExpression[ExpM1]("rf_expm1")
110+
registry.registerExpression[Sqrt]("rf_sqrt")
110111
registry.registerExpression[Resample]("rf_resample")
111112
registry.registerExpression[TileToArrayDouble]("rf_tile_to_array_double")
112113
registry.registerExpression[TileToArrayInt]("rf_tile_to_array_int")

core/src/main/scala/org/locationtech/rasterframes/functions/LocalFunctions.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ trait LocalFunctions {
299299
/** Exponential of cell values, less one*/
300300
def rf_expm1(tileCol: Column): Column = ExpM1(tileCol)
301301

302+
/** Square root of cell values */
303+
def rf_sqrt(tileCol: Column): Column = Sqrt(tileCol)
304+
302305
/** Return the incoming tile untouched. */
303306
def rf_identity(tileCol: Column): Column = Identity(tileCol)
304307
}

core/src/test/scala/org/locationtech/rasterframes/functions/LocalFunctionsSpec.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,15 @@ class LocalFunctionsSpec extends TestEnvironment with RasterMatchers {
298298

299299
}
300300

301+
it("should take square root") {
302+
checkDocs("rf_sqrt")
303+
304+
val df = Seq(three).toDF("tile")
305+
assertEqual(
306+
df.select(rf_sqrt(rf_local_multiply($"tile", $"tile"))).as[ProjectedRasterTile].first(),
307+
three
308+
)
309+
}
310+
301311
}
302312
}

docs/src/main/paradox/reference.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,13 @@ Performs cell-wise logarithm with base 2.
559559

560560
Performs natural logarithm of cell values plus one. Inverse of @ref:[`rf_expm1`](reference.md#rf-expm1).
561561

562+
563+
### rf_sqrt
564+
565+
Tile rf_sqrt(Tile tile)
566+
567+
Perform cell-wise square root.
568+
562569
## Tile Statistics
563570

564571
The following functions compute a statistical summary per row of a `tile` column. The statistics are computed across the cells of a single `tile`, within each DataFrame Row.

docs/src/main/paradox/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Add `rf_local_min`, `rf_local_max`, and `rf_local_clip` functions.
2929
* Add cell value scaling functions `rf_rescale` and `rf_standardize`.
3030
* Add `rf_where` function, similar in spirit to numpy's `where`, or a cell-wise version of Spark SQL's `when` and `otherwise`.
31+
* Add `rf_sqrt` function to compute cell-wise square root.
3132

3233

3334
## 0.8.x

pyrasterframes/src/main/python/pyrasterframes/rasterfunctions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,10 @@ def rf_expm1(tile_col: Column_type) -> Column:
773773
return _apply_column_function('rf_expm1', tile_col)
774774

775775

776+
def rf_sqrt(tile_col: Column_type) -> Column:
777+
"""Performs cell-wise square root"""
778+
return _apply_column_function('rf_sqrt', tile_col)
779+
776780
def rf_identity(tile_col: Column_type) -> Column:
777781
"""Pass tile through unchanged"""
778782
return _apply_column_function('rf_identity', tile_col)

pyrasterframes/src/main/python/tests/RasterFunctionsTests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def test_general(self):
102102
.withColumn('log', rf_log('tile')) \
103103
.withColumn('exp', rf_exp('tile')) \
104104
.withColumn('expm1', rf_expm1('tile')) \
105+
.withColumn('sqrt', rf_sqrt('tile')) \
105106
.withColumn('round', rf_round('tile')) \
106107
.withColumn('abs', rf_abs('tile'))
107108

0 commit comments

Comments
 (0)