Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SEDONA-353] Add RS_BandNoDataValue #958

Merged
merged 20 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sedona.common.raster;

import org.geotools.coverage.GridSampleDimension;
import org.geotools.coverage.grid.GridCoverage2D;

public class RasterBandAccessors {

public static Double getBandNoDataValue(GridCoverage2D raster, int band) {
if (band > raster.getNumSampleDimensions()) {
throw new IllegalArgumentException("Provided band index is not present in the raster");
}
GridSampleDimension bandSampleDimension = raster.getSampleDimension(band - 1);
if (bandSampleDimension.getNoDataValues() == null) return null;
return raster.getSampleDimension(band - 1).getNoDataValues()[0];
}

public static Double getBandNoDataValue(GridCoverage2D raster) {
return getBandNoDataValue(raster, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.sedona.common.raster;

import org.geotools.coverage.grid.GridCoverage2D;
import org.junit.Test;
import org.opengis.referencing.FactoryException;

import java.io.IOException;

import static org.junit.Assert.*;

public class RasterBandAccessorsTest extends RasterTestBase {

@Test
public void testBandNoDataValueCustomBand() throws FactoryException {
int width = 5, height = 10;
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, width, height, 53, 51, 1, 1, 0, 0, 4326);
double[] values = new double[width * height];
for (int i = 0; i < values.length; i++) {
values[i] = i + 1;
}
emptyRaster = MapAlgebra.addBandFromArray(emptyRaster, values, 2, 1d);
assertNotNull(RasterBandAccessors.getBandNoDataValue(emptyRaster, 2));
assertEquals(1, RasterBandAccessors.getBandNoDataValue(emptyRaster, 2), 1e-9);
assertNull(RasterBandAccessors.getBandNoDataValue(emptyRaster));
}

@Test
public void testBandNoDataValueDefaultBand() throws FactoryException {
int width = 5, height = 10;
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, width, height, 53, 51, 1, 1, 0, 0, 4326);
double[] values = new double[width * height];
for (int i = 0; i < values.length; i++) {
values[i] = i + 1;
}
emptyRaster = MapAlgebra.addBandFromArray(emptyRaster, values, 1, 1d);
assertNotNull(RasterBandAccessors.getBandNoDataValue(emptyRaster));
assertEquals(1, RasterBandAccessors.getBandNoDataValue(emptyRaster), 1e-9);
}

@Test
public void testBandNoDataValueDefaultNoData() throws FactoryException {
int width = 5, height = 10;
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, width, height, 53, 51, 1, 1, 0, 0, 4326);
double[] values = new double[width * height];
for (int i = 0; i < values.length; i++) {
values[i] = i + 1;
}
assertNull(RasterBandAccessors.getBandNoDataValue(emptyRaster, 1));
}

@Test
public void testBandNoDataValueIllegalBand() throws FactoryException, IOException {
GridCoverage2D raster = rasterFromGeoTiff(resourceFolder + "raster/raster_with_no_data/test5.tiff");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test5 has a file extension TIFF but you specified tiff here. Please change the file extension of the original image to tiff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That didn't seem to matter for my local tests. However, I've renamed the file extension to tiff and pushed now

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> RasterBandAccessors.getBandNoDataValue(raster, 2));
assertEquals("Provided band index is not present in the raster", exception.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ object Catalog {
function[RS_Contains](),
function[RS_WorldToRasterCoord](),
function[RS_WorldToRasterCoordX](),
function[RS_WorldToRasterCoordY]()
function[RS_WorldToRasterCoordY](),
function[RS_BandNoDataValue]()
)

val aggregateExpressions: Seq[Aggregator[Geometry, Geometry, Geometry]] = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.spark.sql.sedona_sql.expressions.raster

import org.apache.sedona.common.raster.{GeometryFunctions, RasterAccessors}
import org.apache.sedona.common.raster.RasterAccessors
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.sedona_sql.expressions.InferrableFunctionConverter._
import org.apache.spark.sql.sedona_sql.expressions.InferredExpression
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.apache.spark.sql.sedona_sql.expressions.raster
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file must have a Apache header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, pushed


import org.apache.sedona.common.raster.RasterBandAccessors
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.sedona_sql.expressions.InferrableFunctionConverter._
import org.apache.spark.sql.sedona_sql.expressions.InferredExpression

case class RS_BandNoDataValue(inputExpressions: Seq[Expression]) extends InferredExpression(inferrableFunction2(RasterBandAccessors.getBandNoDataValue), inferrableFunction1(RasterBandAccessors.getBandNoDataValue)) {
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.sedona.sql

import org.apache.spark.sql.functions.{collect_list, expr}
import org.geotools.coverage.grid.GridCoverage2D
import org.junit.Assert.assertEquals
import org.junit.Assert.{assertEquals, assertNull}
import org.locationtech.jts.geom.{Coordinate, Geometry}
import org.scalatest.{BeforeAndAfter, GivenWhenThen}

Expand Down Expand Up @@ -662,5 +662,27 @@ class rasteralgebraTest extends TestBaseScala with BeforeAndAfter with GivenWhen
assert(sparkSession.sql("SELECT RS_WITHIN(RS_MakeEmptyRaster(1, 20, 20, 2, 22, 1), ST_GeomFromWKT('POLYGON ((0 0, 0 50, 100 50, 100 0, 0 0))'))").first().getBoolean(0))
assert(!sparkSession.sql("SELECT RS_WITHIN(RS_MakeEmptyRaster(1, 100, 100, 0, 50, 1), ST_GeomFromWKT('POLYGON ((2 2, 2 25, 20 25, 20 2, 2 2))'))").first().getBoolean(0))
}

it("Passed RS_BandNoDataValue - noDataValueFor for raster from geotiff - default band") {
var df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/raster_with_no_data/test5.tiff")
df = df.selectExpr("RS_FromGeoTiff(content) as raster")
val result = df.selectExpr("RS_BandNoDataValue(raster)").first().getDouble(0)
assertEquals(0, result, 1e-9)
}

it("Passed RS_BandNoDataValue - null noDataValueFor for raster from geotiff") {
var df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/test1.tiff")
df = df.selectExpr("RS_FromGeoTiff(content) as raster")
val result = df.selectExpr("RS_BandNoDataValue(raster)").first().get(0)
assertNull(result)
}

it("Passed RS_BandNoDataValue - noDataValueFor for raster from geotiff - explicit band") {
var df = sparkSession.read.format("binaryFile").load(resourceFolder + "raster/raster_with_no_data/test5.tiff")
df = df.selectExpr("RS_FromGeoTiff(content) as raster")
val result = df.selectExpr("RS_BandNoDataValue(raster, 1)").first().getDouble(0)
assertEquals(0, result, 1e-9)
}

}
}
Loading