|
1 | 1 | package com.gaia3d.terrain.tile.raster; |
2 | 2 |
|
| 3 | +import com.gaia3d.command.GlobalOptions; |
3 | 4 | import com.gaia3d.terrain.tile.core.*; |
4 | 5 | import com.gaia3d.terrain.tile.elevation.*; |
5 | 6 | import com.gaia3d.terrain.tile.generation.*; |
|
8 | 9 |
|
9 | 10 | import org.junit.jupiter.api.Tag; |
10 | 11 | import org.junit.jupiter.api.Test; |
11 | | - |
| 12 | +import org.eclipse.imagen.PlanarImage; |
| 13 | +import org.eclipse.imagen.RasterFactory; |
| 14 | +import org.eclipse.imagen.TiledImage; |
| 15 | +import org.eclipse.imagen.media.range.NoDataContainer; |
| 16 | +import org.geotools.coverage.grid.GridCoverage2D; |
| 17 | +import org.geotools.coverage.grid.GridCoverageFactory; |
| 18 | +import org.geotools.coverage.util.CoverageUtilities; |
| 19 | +import org.geotools.geometry.jts.ReferencedEnvelope; |
| 20 | +import org.geotools.referencing.crs.DefaultGeographicCRS; |
| 21 | + |
| 22 | +import java.awt.image.DataBuffer; |
| 23 | +import java.awt.image.WritableRaster; |
12 | 24 | import java.io.IOException; |
13 | 25 | import java.nio.ByteBuffer; |
14 | 26 | import java.nio.file.Files; |
15 | 27 | import java.nio.file.Path; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
16 | 30 |
|
17 | 31 | import static org.junit.jupiter.api.Assertions.*; |
18 | 32 |
|
19 | 33 | class TerrainRasterReaderWriterTest { |
20 | 34 |
|
| 35 | + @Test |
| 36 | + @Tag("default") |
| 37 | + void coverageWriterPreservesDoubleNoDataMarkerForComparison() throws Exception { |
| 38 | + Path tempDir = Files.createTempDirectory("terrain-raster-double-nodata-"); |
| 39 | + Path rasterPath = tempDir.resolve("tile" + TerrainRasterFormat.EXTENSION); |
| 40 | + GlobalOptions globalOptions = GlobalOptions.getInstance(); |
| 41 | + double previousNoData = globalOptions.getNoDataValue(); |
| 42 | + globalOptions.setNoDataValue(-9999.0); |
| 43 | + GridCoverage2D coverage = createDoubleCoverage( |
| 44 | + new double[]{-Double.MAX_VALUE, -9999.0, Double.NaN, 123.5}, -Double.MAX_VALUE); |
| 45 | + |
| 46 | + try { |
| 47 | + new TerrainRasterWriter().write(rasterPath, coverage); |
| 48 | + TerrainRasterData read = new TerrainRasterReader().read(rasterPath); |
| 49 | + |
| 50 | + assertTrue(Float.isNaN(read.noDataValue())); |
| 51 | + assertTrue(Float.isNaN(read.getElevation(0, 0))); |
| 52 | + assertTrue(Float.isNaN(read.getElevation(1, 0))); |
| 53 | + assertTrue(Float.isNaN(read.getElevation(2, 0))); |
| 54 | + assertEquals(123.5f, read.getElevation(3, 0)); |
| 55 | + } finally { |
| 56 | + coverage.dispose(true); |
| 57 | + globalOptions.setNoDataValue(previousNoData); |
| 58 | + deleteRecursively(tempDir); |
| 59 | + } |
| 60 | + } |
| 61 | + |
21 | 62 | @Test |
22 | 63 | @Tag("default") |
23 | 64 | void writerAndReaderRoundTripFloat32TerrainRaster() throws Exception { |
@@ -108,6 +149,21 @@ void publicRasterDataConstructorRetainsDefensiveCopy() { |
108 | 149 | assertEquals(10.0f, data.getElevation(0, 0)); |
109 | 150 | } |
110 | 151 |
|
| 152 | + private GridCoverage2D createDoubleCoverage(double[] values, double noDataValue) { |
| 153 | + WritableRaster raster = RasterFactory.createBandedRaster(DataBuffer.TYPE_DOUBLE, values.length, 1, 1, null); |
| 154 | + for (int x = 0; x < values.length; x++) { |
| 155 | + raster.setSample(x, 0, 0, values[x]); |
| 156 | + } |
| 157 | + |
| 158 | + TiledImage image = new TiledImage(0, 0, values.length, 1, 0, 0, |
| 159 | + raster.getSampleModel(), PlanarImage.createColorModel(raster.getSampleModel())); |
| 160 | + image.setData(raster); |
| 161 | + Map<String, Object> properties = new HashMap<>(); |
| 162 | + CoverageUtilities.setNoDataProperty(properties, new NoDataContainer(noDataValue)); |
| 163 | + ReferencedEnvelope envelope = new ReferencedEnvelope(0.0, values.length, 0.0, 1.0, DefaultGeographicCRS.WGS84); |
| 164 | + return new GridCoverageFactory().create("double-dem", image, envelope, null, null, properties); |
| 165 | + } |
| 166 | + |
111 | 167 | private void deleteRecursively(Path path) throws IOException { |
112 | 168 | if (!Files.exists(path)) { |
113 | 169 | return; |
|
0 commit comments