From 2e55d6a0cc39f00594c74aeb125916e4080189e3 Mon Sep 17 00:00:00 2001 From: jericks Date: Sat, 2 Jun 2018 10:51:08 -0700 Subject: [PATCH] Add ArcGrid Raster set value tests --- .../geoscript/layer/ArcGridTestCase.groovy | 114 +++++++++++++++++- .../geoscript/layer/RasterTestCase.groovy | 2 - 2 files changed, 113 insertions(+), 3 deletions(-) diff --git a/src/test/groovy/geoscript/layer/ArcGridTestCase.groovy b/src/test/groovy/geoscript/layer/ArcGridTestCase.groovy index afa9a6ab..2b344515 100644 --- a/src/test/groovy/geoscript/layer/ArcGridTestCase.groovy +++ b/src/test/groovy/geoscript/layer/ArcGridTestCase.groovy @@ -1,6 +1,7 @@ package geoscript.layer import geoscript.AssertUtil +import geoscript.geom.Bounds import geoscript.proj.Projection import org.apache.commons.io.input.ReaderInputStream import org.geotools.factory.GeoTools @@ -9,7 +10,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder -import static org.junit.Assert.assertNotNull +import static org.junit.Assert.* /** * The ArcGrid Unit Test @@ -141,4 +142,115 @@ COLS: 4 13.0 5.0 1.0 * """, str) } + + @Test void setValues() { + + // Create a simple Raster + Bounds bounds = new Bounds(0, 0, 7, 5, "EPSG:4326") + List data = [ + [0,0,0,0,0,0,0], + [0,1,1,1,1,1,0], + [0,1,2,3,2,1,0], + [0,1,1,1,1,1,0], + [0,0,0,0,0,0,0] + ] + Raster raster = new Raster(data, bounds) + + // Create a File to hold the Raster + File file = folder.newFile("dem.asc") + file.delete() + + // Write the Raster to a ArcGrid File + Format.getFormat(file).write(raster) + + // Read the Raster from the ArcGrid File + raster = Format.getFormat(file).read() + + // Check existing value, set new value, check new value + assertEquals 1, raster.eval([1,1])[0], 0.1 + raster.setValue([1,1],5) + assertEquals 5, raster.eval([1,1])[0], 0.1 + + // Check existing value, set new value, check new value + assertEquals 3, raster[[3,2]][0], 0.1 + raster[[3,2]] = 10 + assertEquals 10, raster[[3,2]][0], 0.1 + + // Write the modified Raster back to File + Format.getFormat(file).write(raster) + + // Read the Raster from the ArcGrid File + raster = Format.getFormat(file).read() + + // Check existing values + assertEquals 5, raster.eval([1,1])[0], 0.1 + assertEquals 10, raster[[3,2]][0], 0.1 + } + + @Test void rasterMath() { + + // Create DSM Raster + File dsmFile = folder.newFile("DSM_SolarTirol_small.asc") + dsmFile.delete() + Bounds bounds = new Bounds(0, 0, 7, 5, "EPSG:4326") + List data = [ + [10,10,10,10,10,10,10], + [10,11,11,11,11,11,10], + [10,11,12,13,12,11,10], + [10,11,11,11,11,11,10], + [10,10,10,10,10,10,10] + ] + Raster dsmRaster = new Raster(data, bounds) + Format.getFormat(dsmFile).write(dsmRaster) + + // Create DTM Raster + File dtmFile = folder.newFile("DTM_SolarTirol_small.asc") + dtmFile.delete() + bounds = new Bounds(0, 0, 7, 5, "EPSG:4326") + data = [ + [0,0,0,0,0,0,0], + [0,1,1,1,1,1,0], + [0,1,2,3,2,1,0], + [0,1,1,1,1,1,0], + [0,0,0,0,0,0,0] + ] + Raster dtmRaster = new Raster(data, bounds) + Format.getFormat(dtmFile).write(dtmRaster) + + // Create the output CHM Raster + File chmPath = folder.newFile("CHM.asc") + chmPath.delete() + + // Read the Rasters + dtmRaster = Format.getFormat(dtmFile).read() + dsmRaster = Format.getFormat(dsmFile).read() + Raster outChmRaster = Format.getFormat(dsmFile).read() + + int cols = dtmRaster.cols + int rows = dtmRaster.rows + double NO_VALUE = -9999.0 + + for ( row in 0..(rows-1)) { + for ( col in 0..(cols-1)) { + List position = [col, row] + double dtmValue = dtmRaster.getValue(position) + double dsmValue = dsmRaster.getValue(position) + if(dtmValue != NO_VALUE && dsmValue != NO_VALUE){ + double chmValue = dsmValue - dtmValue + outChmRaster.setValue(position, chmValue) + assertEquals(chmValue, outChmRaster.getValue(position), 0.1) + } + } + } + + Format.getFormat(chmPath).write(outChmRaster) + outChmRaster = Format.getFormat(chmPath).read() + assertEquals(10.0, outChmRaster.getValue([1,1]), 0.1) + assertEquals(10.0, outChmRaster.getValue([3,3]), 0.1) + assertEquals(10.0, outChmRaster.getValue([1,4]), 0.1) + assertEquals(10.0, outChmRaster.getValue([2,3]), 0.1) + assertEquals(10.0, outChmRaster.getValue([4,2]), 0.1) + + } + } diff --git a/src/test/groovy/geoscript/layer/RasterTestCase.groovy b/src/test/groovy/geoscript/layer/RasterTestCase.groovy index 36ffe222..b9361b5a 100644 --- a/src/test/groovy/geoscript/layer/RasterTestCase.groovy +++ b/src/test/groovy/geoscript/layer/RasterTestCase.groovy @@ -13,8 +13,6 @@ import static org.junit.Assert.* import geoscript.workspace.Memory import geoscript.feature.Field -import static org.junit.Assert.assertEquals - /** * The Raster unit test */