-
Notifications
You must be signed in to change notification settings - Fork 695
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
jiayuasu
merged 20 commits into
apache:master
from
iGN5117:develop_Nilesh_BandNoDataValue
Aug 11, 2023
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9944bd1
Refactor AddBandAsArray to expose adding a custom noDataValue
iGN5117 44c2129
Refactor RS_AddBandFromArray to allow adding, editing and removing no…
iGN5117 ed15df5
Added a new raster tiff with noDataValues
iGN5117 132a321
Slightly change test since there are multiple rasters inside raster f…
iGN5117 3167952
Fix mistake
iGN5117 d93f6b7
Add acutal tiff image
iGN5117 c25bb5e
Fix accidental push
iGN5117 f9f776d
Refactored code with comments to enhance readability
iGN5117 5d7082f
Fix mistake in testing
iGN5117 9471cde
change location of test5.tiff to fix failing tests
iGN5117 b6ec079
Merge branch 'sedona-master' into develop_Nilesh_AddBand_Refactor
iGN5117 d1cbff1
Add RS_BandNoDataValue
iGN5117 9faf105
Add no nodatavalue example in docs
iGN5117 92bb000
Merge branch 'sedona-master' into develop_Nilesh_BandNoDataValue
iGN5117 f312dc2
fix filepath name for tests
iGN5117 9f1d49b
rename test file
iGN5117 29350a6
Revert "fix filepath name for tests"
iGN5117 0b8b807
Merge branch 'sedona-master' into develop_Nilesh_BandNoDataValue
iGN5117 9b04fbd
Add docs for RS_BandNoDataValue
iGN5117 ef79006
Add license
iGN5117 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()); | ||
} | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
...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,13 @@ | ||
package org.apache.spark.sql.sedona_sql.expressions.raster | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file must have a Apache header. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test5
has a file extensionTIFF
but you specifiedtiff
here. Please change the file extension of the original image totiff
.There was a problem hiding this comment.
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