def test_validationInputsNoMax(self): """ Test the GaiaProcess.validate() function - pass on no max input types """ raster_io1 = RasterFileIO(uri='/fake/path1') raster_io2 = RasterFileIO(uri='/fake/path2') try: geo.RasterMathProcess(inputs=[raster_io1, raster_io2], calc='A+B') except geo.GaiaException: self.fail("Multiple inputs should have passed validation")
def test_rastermath_add(self): """ Test adding two rasters together """ raster1_io = RasterFileIO(name='A', uri=os.path.join(testfile_path, 'globalairtemp.tif')) raster2_io = RasterFileIO(name='B', uri=os.path.join(testfile_path, 'globalprecip.tif')) calc = 'A + B' bands = [1, 1] process = geo.RasterMathProcess(inputs=[raster1_io, raster2_io], calc=calc, bands=bands) try: process.compute() self.assertTrue(os.path.exists(process.output.uri)) oraster, raster1, raster2 = [ x.read() for x in (process.output, raster1_io, raster2_io) ] # Output raster should be same dimensions as raster 1 self.assertEquals((oraster.RasterXSize, oraster.RasterYSize), (raster1.RasterXSize, raster1.RasterYSize)) orb, r1b, r2b = [ x.GetRasterBand(1) for x in (oraster, raster1, raster2) ] # Min value of output should be >= the max minimum of inputs self.assertGreaterEqual( orb.GetStatistics(False, True)[0], max( r1b.GetStatistics(False, True)[0], r2b.GetStatistics(False, True)[0])) # Max value of output >= max(minimum)+min(maximum) of inputs self.assertGreaterEqual( orb.GetStatistics(False, True)[1], max( r1b.GetStatistics(False, True)[0], r2b.GetStatistics(False, True)[0]) + min( r1b.GetStatistics(False, True)[1], r2b.GetStatistics(False, True)[1])) finally: if process: process.purge()
def test_rastermath_multiply_by_value(self): """ Test multiplying a raster by a value, and specifying output type (Float32) """ raster1_io = RasterFileIO(name='A', uri=os.path.join(testfile_path, 'globalprecip.tif')) calc = 'A * 2' output_type = 'Float32' process = geo.RasterMathProcess(inputs=[ raster1_io, ], calc=calc, output_type=output_type) try: process.compute() self.assertTrue(os.path.exists(process.output.uri)) oraster, raster1 = [x.read() for x in (process.output, raster1_io)] # Output raster should be same dimensions as raster 1 self.assertEquals((oraster.RasterXSize, oraster.RasterYSize), (raster1.RasterXSize, raster1.RasterYSize)) orb, r1b = [x.GetRasterBand(1) for x in (oraster, raster1)] # Maximum value of output should be 2x the max of input raster self.assertEqual( orb.GetStatistics(False, True)[1], r1b.GetStatistics(False, True)[1] * 2) # Datatype of band should be Float32 (== gdal.GDT_Float32 == 6) self.assertEquals(6, orb.DataType) self.assertEquals(1.175494351E-38, orb.GetNoDataValue()) # Each pixel of output raster should equal 2x input raster # unless it is a nodata value ora, r1a = [x.ReadAsArray() for x in (orb, r1b)] for x in range(raster1.RasterXSize): for y in range(raster1.RasterYSize): if r1a[y, x] != r1b.GetNoDataValue(): self.assertEquals(ora[y, x], r1a[y, x] * 2) finally: if process: process.purge()
def test_rastermath_logical_operators(self): """ Test creation of a masked raster based on logical operators """ raster1_io = RasterFileIO(name='A', uri=os.path.join(testfile_path, 'globalairtemp.tif')) calc = 'logical_or(logical_and(A >= 27000, A <= 28000), ' \ 'logical_and(A >= 30000, A <= 31000))' process = geo.RasterMathProcess(inputs=[ raster1_io, ], calc=calc) try: process.compute() self.assertTrue(os.path.exists(process.output.uri)) oraster, raster1 = [x.read() for x in (process.output, raster1_io)] # Output raster should be same dimensions as raster 1 self.assertEquals((oraster.RasterXSize, oraster.RasterYSize), (raster1.RasterXSize, raster1.RasterYSize)) orb, r1b = [x.GetRasterBand(1) for x in (oraster, raster1)] # Maximum value of output should be 1 self.assertEqual(orb.GetStatistics(False, True)[1], 1) # Minimum value of output should be 0 self.assertEqual(orb.GetStatistics(False, True)[0], 0) # Pixels should be 1 where source is between 27K-28K or 30-31K ora, r1a = [x.ReadAsArray() for x in (orb, r1b)] self.assertTrue(ora[90, 10] == 1 and r1a[90, 10] == 30083) self.assertTrue(ora[160, 10] == 1 and r1a[160, 10] == 27074) # Pixels should be 0 where source is not between 27K-28K or 30-31K ora, r1a = [x.ReadAsArray() for x in (orb, r1b)] self.assertTrue(ora[120, 10] == 0 and r1a[120, 10] == 29623) self.assertTrue(ora[175, 10] == 0 and r1a[175, 10] == 23928) finally: if process: process.purge()