You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ji...@apache.org on 2026/09/22 05:02:26 UTC
(sedona) branch master updated: [GH-3372] Allow NaN nodata in RS_AsRaster (#3384)
This is an automated email from the ASF dual-hosted git repository.
jiayuasu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sedona.git
The following commit(s) were added to refs/heads/master by this push:
new 86fbb82b33e [GH-3372] Allow NaN nodata in RS_AsRaster (#3384)
86fbb82b33e is described below
commit 86fbb82b33ef22a0e19fcd62eac7eb20a103e91f
Author: Minh Dau <mt...@bucknell.edu>
AuthorDate: Tue Sep 22 01:02:19 2026 -0400
[GH-3372] Allow NaN nodata in RS_AsRaster (#3384)
---
.../apache/sedona/common/utils/RasterUtils.java | 20 +++---
.../common/raster/RasterConstructorsTest.java | 80 +++++++++++++++++++---
docs/api/sql/Raster-Operators/RS_AsRaster.md | 2 +-
3 files changed, 83 insertions(+), 19 deletions(-)
diff --git a/common/src/main/java/org/apache/sedona/common/utils/RasterUtils.java b/common/src/main/java/org/apache/sedona/common/utils/RasterUtils.java
index 03f3fdd05d2..09dd281095e 100644
--- a/common/src/main/java/org/apache/sedona/common/utils/RasterUtils.java
+++ b/common/src/main/java/org/apache/sedona/common/utils/RasterUtils.java
@@ -745,9 +745,9 @@ public class RasterUtils {
/**
* Verifies that {@code noDataValue} can be stored in the pixel type named by {@code pixelType}
- * without silent coercion, returning it unchanged when it can. Delegates to {@link
- * #assertRepresentable(double, String, String)} with the argument name {@code "noDataValue"} so
- * the nodata path keeps its existing error messages.
+ * without silent coercion, returning it unchanged when it can. NaN is supported as a nodata value
+ * on floating-point bands. Other values are checked by {@link #assertRepresentable(double,
+ * String, String, boolean)} with the argument name {@code "noDataValue"}.
*
* @param noDataValue the candidate nodata / background value
* @param pixelType a Sedona pixel type string accepted by {@link #getDataTypeCode(String)} (for
@@ -757,6 +757,9 @@ public class RasterUtils {
* type
*/
public static double assertNoDataValueRepresentable(double noDataValue, String pixelType) {
+ if (Double.isNaN(noDataValue) && !isDataTypeIntegral(getDataTypeCode(pixelType))) {
+ return noDataValue;
+ }
// A nodata value is a sentinel: it must be stored without any coercion (an exact 32-bit float
// round-trip, and no negative-zero collapse on integer bands) so the stored background always
// matches the recorded nodata metadata.
@@ -832,15 +835,12 @@ public class RasterUtils {
description = "signed 32-bit";
break;
case DataBuffer.TYPE_FLOAT:
- // NaN and infinite values cannot be used on a float/double band: NaN is the codebase's
- // internal "no nodata" sentinel, so it cannot be stored as a distinct nodata value and
- // would be silently dropped; an infinite value is not a valid GeoTools category bound and
- // crashes deep in GeoTools with "Range [Infinity .. Infinity] is not valid". Reject both
- // up front with a clear error (fail loud) rather than dropping them or crashing later.
+ // NaN nodata is handled by assertNoDataValueRepresentable. Burn values must remain finite,
+ // and infinity cannot be used as a GeoTools nodata category bound.
if (!Double.isFinite(value)) {
throw new IllegalArgumentException(
String.format(
- "%s %s is not supported for pixel type '%s' (NaN and infinite values are not supported); use a finite value",
+ "%s %s is not supported for pixel type '%s'; use a finite value",
argName, value, pixelType));
}
// A nodata sentinel must round-trip exactly through the band's 32-bit float storage —
@@ -862,7 +862,7 @@ public class RasterUtils {
if (!Double.isFinite(value)) {
throw new IllegalArgumentException(
String.format(
- "%s %s is not supported for pixel type '%s' (NaN and infinite values are not supported); use a finite value",
+ "%s %s is not supported for pixel type '%s'; use a finite value",
argName, value, pixelType));
}
return value;
diff --git a/common/src/test/java/org/apache/sedona/common/raster/RasterConstructorsTest.java b/common/src/test/java/org/apache/sedona/common/raster/RasterConstructorsTest.java
index 37e342f2cbe..0c3138950d7 100644
--- a/common/src/test/java/org/apache/sedona/common/raster/RasterConstructorsTest.java
+++ b/common/src/test/java/org/apache/sedona/common/raster/RasterConstructorsTest.java
@@ -601,20 +601,84 @@ public class RasterConstructorsTest extends RasterTestBase {
}
@Test
- public void testAsRasterRejectsNonFiniteNoDataValue() throws FactoryException, ParseException {
- // NaN and +/-Infinity are rejected as a nodata value on float ('F') and double ('D') bands.
- // NaN is the codebase's internal "no nodata" sentinel, so it would be silently dropped rather
- // than recorded (leaving the NaN-filled background reading back as data); an infinite value is
- // not a valid GeoTools category bound and previously crashed deep in GeoTools with
- // "Range [Infinity .. Infinity] is not valid". Both are now rejected up front with a clear
- // error that names the offending value and pixel type.
+ public void testAsRasterAcceptsNaNNoDataValue() throws FactoryException, ParseException {
+ GridCoverage2D raster = RasterConstructors.makeEmptyRaster(1, "D", 3, 3, 0, 3, 1, -1, 0, 0, 0);
+ Geometry geom =
+ Constructors.geomFromWKT(
+ "POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1))", 0);
+
+ for (String pixelType : new String[] {"F", "D"}) {
+ for (boolean useGeometryExtent : new boolean[] {true, false}) {
+ GridCoverage2D result =
+ RasterConstructors.asRaster(
+ geom, raster, pixelType, false, 1d, Double.NaN, useGeometryExtent);
+ assertNaNNoDataBackground(result);
+ }
+ }
+ }
+
+ @Test
+ public void testAsRasterInheritsNaNNoDataValue() throws FactoryException, ParseException {
+ GridCoverage2D raster = RasterConstructors.makeEmptyRaster(1, "D", 3, 3, 0, 3, 1, -1, 0, 0, 0);
+ GridCoverage2D reference = RasterBandEditors.setBandNoDataValue(raster, 1, Double.NaN);
+ Geometry geom =
+ Constructors.geomFromWKT(
+ "POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1))", 0);
+
+ for (String pixelType : new String[] {"F", "D"}) {
+ assertNaNNoDataBackground(RasterConstructors.asRaster(geom, reference, pixelType));
+ }
+ for (String pixelType : new String[] {"B", "US", "S", "I"}) {
+ Assert.assertThrows(
+ IllegalArgumentException.class,
+ () -> RasterConstructors.asRaster(geom, reference, pixelType));
+ }
+ }
+
+ private static void assertNaNNoDataBackground(GridCoverage2D raster) {
+ assertArrayEquals(
+ new double[] {1, 1, 1, 1, Double.NaN, 1, 1, 1, 1}, MapAlgebra.bandAsArray(raster, 1), 0d);
+ assertEquals(Double.valueOf(Double.NaN), RasterBandAccessors.getBandNoDataValue(raster, 1));
+ assertEquals(8L, RasterBandAccessors.getCount(raster, 1, true));
+ }
+
+ @Test
+ public void testAsRasterRejectsInfiniteNoDataValue() throws FactoryException, ParseException {
+ GridCoverage2D raster = RasterConstructors.makeEmptyRaster(1, "D", 3, 3, 0, 3, 1, -1, 0, 0, 0);
+ Geometry geom = Constructors.geomFromWKT("POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0))", 0);
+ for (String pixelType : new String[] {"F", "D"}) {
+ for (double noData : new double[] {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}) {
+ Assert.assertThrows(
+ IllegalArgumentException.class,
+ () -> RasterConstructors.asRaster(geom, raster, pixelType, false, 1d, noData, false));
+ }
+ }
+ }
+
+ @Test
+ public void testAsRasterRejectsNonFiniteBurnValue() throws FactoryException, ParseException {
+ GridCoverage2D raster = RasterConstructors.makeEmptyRaster(1, "D", 3, 3, 0, 3, 1, -1, 0, 0, 0);
+ Geometry geom = Constructors.geomFromWKT("POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0))", 0);
+ for (String pixelType : new String[] {"F", "D"}) {
+ for (double value :
+ new double[] {Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}) {
+ Assert.assertThrows(
+ IllegalArgumentException.class,
+ () -> RasterConstructors.asRaster(geom, raster, pixelType, false, value, 0d, false));
+ }
+ }
+ }
+
+ @Test
+ public void testAsRasterRejectsNonFiniteNoDataValueOnIntegerBand()
+ throws FactoryException, ParseException {
GridCoverage2D raster =
RasterConstructors.makeEmptyRaster(1, "d", 7, 6, 100, 500, 2, -2, 0, 0, 0);
Geometry geom =
Constructors.geomFromWKT(
"POLYGON ((100.5 499.5, 113.5 499.5, 113.5 488.5, 100.5 488.5, 100.5 499.5))", 0);
- for (String pixelType : new String[] {"F", "D"}) {
+ for (String pixelType : new String[] {"B", "US", "S", "I"}) {
for (double nonFinite :
new double[] {Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}) {
IllegalArgumentException error =
diff --git a/docs/api/sql/Raster-Operators/RS_AsRaster.md b/docs/api/sql/Raster-Operators/RS_AsRaster.md
index 6564731e489..facd0650108 100644
--- a/docs/api/sql/Raster-Operators/RS_AsRaster.md
+++ b/docs/api/sql/Raster-Operators/RS_AsRaster.md
@@ -26,7 +26,7 @@ Introduction: `RS_AsRaster` converts a vector geometry into a raster dataset by
* `pixelType`: Defines data type of the output raster. This can be one of the following, D (double), F (float), I (integer), S (short), US (unsigned short) or B (byte).
* `allTouched` (Since: `v1.7.1`): Decides the pixel selection criteria. If set to `true`, the function selects all pixels touched by the geometry, else, selects only pixels whose centroids intersect the geometry. Defaults to `false`. For lines and polygon boundaries, when a vertex maps exactly to a pixel grid line, a neighboring pixel contacted only at that endpoint is not selected unless the segment enters it. `POINT` geometries instead burn every pixel whose extent contains the point, [...]
* `value`: The value to be used for assigning pixels covered by the geometry. Defaults to using `1.0` if not provided.
-* `noDataValue`: The no data value of the resultant raster. Every pixel the geometry does not cover (including interior holes) is filled with the resolved no data value, which is also recorded as the output band's no data metadata. The value must be representable in `pixelType`: an out-of-range or fractional value for an integer `pixelType`, or a value that is not exactly representable in a floating-point `pixelType` (for example `0.1` in a 32-bit float band), is rejected with an error r [...]
+* `noDataValue`: The no data value of the resultant raster. Every pixel the geometry does not cover (including interior holes) is filled with the resolved no data value, which is also recorded as the output band's no data metadata. The value must be representable in `pixelType`: an out-of-range or fractional value for an integer `pixelType`, or a value that is not exactly representable in a floating-point `pixelType` (for example `0.1` in a 32-bit float band), is rejected with an error r [...]
* `useGeometryExtent`: Defines the extent of the resultant raster. When set to `true`, it corresponds to the extent of `geom`, and when set to false, it corresponds to the extent of `raster`. Default value is `true` if not set.
Format: