Skip to content

Commit

Permalink
Add ArcGrid Raster set value tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jericks committed Jun 2, 2018
1 parent 33fb915 commit 2e55d6a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
114 changes: 113 additions & 1 deletion src/test/groovy/geoscript/layer/ArcGridTestCase.groovy
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

}

}
2 changes: 0 additions & 2 deletions src/test/groovy/geoscript/layer/RasterTestCase.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 2e55d6a

Please sign in to comment.