Skip to content

Commit

Permalink
feat: move to java, use InferredExpression, and test java for RS_Squa…
Browse files Browse the repository at this point in the history
…reRoot
  • Loading branch information
furqaankhan committed Sep 19, 2023
1 parent 3af7aa8 commit 8b1193d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ public static double[] modulo(double[] band, double dividend) {
return result;
}

/**
* @param band band values
* @return an array, where each pixel has been applied square root operation.
*/
public static double[] squareRoot(double[] band) {
double[] result = new double[band.length];
for (int i = 0; i < band.length; i++) {
result[i] = (double) Math.round(Math.sqrt(band[i]) * 100) / 100;
}

return result;
}

/**
* Throws an IllegalArgumentException if the lengths of the bands are not the same.
* @param band1 length of band values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ public void testModulo() {
assertArrayEquals(expected, actual, 0.1d);
}

@Test
public void testSquareRoot() {
double[] band = new double[] {8.0, 16.0, 24.0};
double[] actual = MapAlgebra.squareRoot(band);
double[] expected = new double[] {2.83, 4.0, 4.9};
assertArrayEquals(expected, actual, 0.01d);
}

@Test
public void testMapAlgebra() throws FactoryException {
Random random = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,32 +391,7 @@ case class RS_Modulo(inputExpressions: Seq[Expression]) extends InferredExpressi
}

// Square root of values in a band
case class RS_SquareRoot(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback with UserDataGeneratator {
assert(inputExpressions.length == 1)

override def nullable: Boolean = false

override def eval(inputRow: InternalRow): Any = {
val band = inputExpressions(0).eval(inputRow).asInstanceOf[ArrayData].toDoubleArray()
new GenericArrayData(squareRoot(band))

}

private def squareRoot(band: Array[Double]):Array[Double] = {

val result = new Array[Double](band.length)
for(i<-0 until band.length) {
result(i) = (Math.sqrt(band(i))*100).round/100.toDouble
}
result

}

override def dataType: DataType = ArrayType(DoubleType)

override def children: Seq[Expression] = inputExpressions

case class RS_SquareRoot(inputExpressions: Seq[Expression]) extends InferredExpression(MapAlgebra.squareRoot _) {
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)
}
Expand Down

0 comments on commit 8b1193d

Please sign in to comment.