-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SEDONA-353] Add RS_BandNoDataValue (#958)
- Loading branch information
Showing
8 changed files
with
205 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
common/src/main/java/org/apache/sedona/common/raster/RasterBandAccessors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
common/src/test/java/org/apache/sedona/common/raster/RasterBandAccessorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> RasterBandAccessors.getBandNoDataValue(raster, 2)); | ||
assertEquals("Provided band index is not present in the raster", exception.getMessage()); | ||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...c/main/scala/org/apache/spark/sql/sedona_sql/expressions/raster/RasterBandAccessors.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.spark.sql.sedona_sql.expressions.raster | ||
|
||
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters