def test_basic_creation(self): """Verify we can create a raster with basic parameters.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_COLOMBIA pixels = numpy.ones((4, 4), numpy.byte) nodata = 0 reference = SRS_COLOMBIA filename = os.path.join(self.workspace, 'foo.tif') create_raster_on_disk([pixels], reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype=gdal.GDT_Byte, format='GTiff', filename=filename) self.assertTrue(os.path.exists(filename)) dataset = gdal.Open(filename) self.assertEqual(dataset.RasterXSize, 4) self.assertEqual(dataset.RasterYSize, 4) band = dataset.GetRasterBand(1) band_nodata = band.GetNoDataValue() self.assertEqual(band_nodata, nodata) dataset_sr = osr.SpatialReference() dataset_sr.ImportFromWkt(dataset.GetProjection()) source_sr = osr.SpatialReference() source_sr.ImportFromWkt(reference.projection) self.assertTrue(dataset_sr.IsSame(source_sr))
def test_multi_bands(self): """Verify that we can create multi-band rasters.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_WILLAMETTE pixels = [ numpy.ones((5, 5)), numpy.zeros((5, 5)), numpy.multiply(numpy.ones((5, 5)), 3), ] nodata = 0 reference = SRS_WILLAMETTE file_handle, filename = tempfile.mkstemp() os.close(file_handle) create_raster_on_disk(pixels, reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename) # check that the three bands have been written properly. dataset = gdal.Open(filename) for band_num, input_matrix in zip(range(1, 4), pixels): band = dataset.GetRasterBand(band_num) written_matrix = band.ReadAsArray() numpy.testing.assert_almost_equal(input_matrix, written_matrix)
def test_bad_driver(self): """Verify failure when a bad driver string is used.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_COLOMBIA reference = SRS_COLOMBIA with self.assertRaises(RuntimeError): create_raster_on_disk( [numpy.ones((4, 4))], reference.origin, reference.projection, 0, reference.pixel_size(30), format='foo')
def test_raster_bad_matrix_iterable_input(self): """Verify TypeError raised when band_matrices not a list.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_WILLAMETTE pixels = set([1]) nodata = None reference = SRS_WILLAMETTE filename = pygeoprocessing.temporary_filename() with self.assertRaises(TypeError): create_raster_on_disk( pixels, reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename)
def test_raster_multiple_dtypes(self): """Verify TypeError raised when matrix band dtypes are mismatched.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_WILLAMETTE pixels = [numpy.array([[0]], dtype=numpy.int), numpy.array([[0]], dtype=numpy.float)] nodata = None reference = SRS_WILLAMETTE filename = pygeoprocessing.temporary_filename() with self.assertRaises(TypeError): create_raster_on_disk( pixels, reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename)
def test_raster_nodata_notset(self): """When nodata=None, a nodata value should not be set.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_WILLAMETTE pixels = [numpy.array([[0]])] nodata = None reference = SRS_WILLAMETTE filename = pygeoprocessing.temporary_filename() create_raster_on_disk( pixels, reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename) set_nodata_value = pygeoprocessing.get_nodata_from_uri(filename) self.assertEqual(set_nodata_value, None)
def test_mark_transition_type(self): """Coastal Blue Carbon: Test mark_transition_type.""" from natcap.invest.coastal_blue_carbon import preprocessor args = _get_preprocessor_args(1, self.workspace_dir) band_matrices_zero = [numpy.zeros((2, 2))] srs = pygeotest.sampledata.SRS_WILLAMETTE raster_zeros = pygeotest.create_raster_on_disk( band_matrices_zero, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join( self.workspace_dir, 'raster_1.tif')) args['lulc_snapshot_list'][0] = raster_zeros preprocessor.execute(args) trans_csv = os.path.join( self.workspace_dir, 'workspace', 'outputs_preprocessor', 'transitions_test.csv') with open(trans_csv, 'r') as f: lines = f.readlines() self.assertTrue(lines[1][:].startswith('n,NCC,accum'))
def test_mismatched_bands(self): """When band sizes are mismatched, TypeError should be raised.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_WILLAMETTE pixels = [ numpy.ones((5, 5)), numpy.ones((4, 4)), numpy.ones((7, 7)) ] nodata = 0 reference = SRS_WILLAMETTE filename = pygeoprocessing.temporary_filename() with self.assertRaises(TypeError): create_raster_on_disk( pixels, reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename)
def test_invalid_raster_bands(self): """Verify an error when raster matrices not in a list.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_WILLAMETTE pixels = numpy.ones((4, 4), numpy.uint16) nodata = 0 reference = SRS_WILLAMETTE filename = pygeoprocessing.temporary_filename() # Error raised when `pixels` is not a list. List of 2D matrices # expected. with self.assertRaises(TypeError): create_raster_on_disk( pixels, reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename)
def test_raster_nodata_notset(self): """When nodata=None, a nodata value should not be set.""" import pygeoprocessing from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_WILLAMETTE pixels = [numpy.array([[0]])] nodata = None reference = SRS_WILLAMETTE file_handle, filename = tempfile.mkstemp() os.close(file_handle) create_raster_on_disk( pixels, reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename) set_nodata_value = ( pygeoprocessing.get_raster_info(filename)['nodata'][0]) self.assertEqual(set_nodata_value, None)
def test_raster_autodtype(self): """Verify automatic detection of a matrix's dtype.""" from pygeoprocessing.testing import create_raster_on_disk from pygeoprocessing.testing.sampledata import SRS_COLOMBIA pixels = numpy.ones((4, 4), numpy.uint16) nodata = 0 reference = SRS_COLOMBIA filename = pygeoprocessing.temporary_filename() create_raster_on_disk([pixels], reference.origin, reference.projection, nodata, reference.pixel_size(30), datatype='auto', filename=filename) dataset = gdal.Open(filename) band = dataset.GetRasterBand(1) band_dtype = band.DataType # numpy.uint16 should translate to gdal.GDT_UInt16 self.assertEqual(band_dtype, gdal.GDT_UInt16)
def test_mark_transition_type_nodata_check(self): """Coastal Blue Carbon: Test mark_transition_type with nodata check.""" from natcap.invest.coastal_blue_carbon import preprocessor args = _get_preprocessor_args(1, self.workspace_dir) band_matrices_zero = [numpy.zeros((2, 2))] srs = pygeotest.sampledata.SRS_WILLAMETTE raster_zeros = pygeotest.create_raster_on_disk( band_matrices_zero, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join( self.workspace_dir, 'raster_1.tif')) args['lulc_snapshot_list'][0] = raster_zeros preprocessor.execute(args)
def test_raster_validation(self): """Coastal Blue Carbon: Test raster validation.""" from natcap.invest.coastal_blue_carbon import preprocessor args = _get_preprocessor_args(1, self.workspace_dir) OTHER_NODATA = -1 srs = pygeotest.sampledata.SRS_WILLAMETTE band_matrices_with_nodata = [numpy.ones((2, 2)) * OTHER_NODATA] raster_wrong_nodata = pygeotest.create_raster_on_disk( band_matrices_with_nodata, srs.origin, srs.projection, OTHER_NODATA, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join( self.workspace_dir, 'raster_wrong_nodata.tif')) args['lulc_snapshot_list'][0] = raster_wrong_nodata with self.assertRaises(ValueError): preprocessor.execute(args)
def _get_args(workspace, num_transitions=2, valuation=True): """Create and return arguments for CBC main model. Parameters: workspace(string): A path to a folder on disk. Generated inputs will be saved to this directory. num_transitions=2 (int): The number of transitions to synthesize. valuation=True (bool): Whether to include parameters related to valuation in the args dict. Returns: args (dict): main model arguments. """ band_matrices = [numpy.ones((2, 2))] band_matrices_two = [numpy.ones((2, 2)) * 2] band_matrices_with_nodata = [numpy.ones((2, 2))] band_matrices_with_nodata[0][0][0] = NODATA_INT srs = pygeotest.sampledata.SRS_WILLAMETTE lulc_lookup_uri = _create_table( os.path.join(workspace, 'lulc_lookup.csv'), lulc_lookup_list) lulc_transition_matrix_uri = _create_table( os.path.join(workspace, 'lulc_transition_matrix.csv'), lulc_transition_matrix_list) carbon_pool_initial_uri = _create_table( os.path.join(workspace, 'carbon_pool_initial.csv'), carbon_pool_initial_list) carbon_pool_transient_uri = _create_table( os.path.join(workspace, 'carbon_pool_transient.csv'), carbon_pool_transient_list) raster_0_uri = pygeotest.create_raster_on_disk( band_matrices, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_0.tif')) raster_1_uri = pygeotest.create_raster_on_disk( band_matrices_with_nodata, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_1.tif')) raster_2_uri = pygeotest.create_raster_on_disk( band_matrices_two, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_2.tif')) possible_transitions = [raster_1_uri, raster_2_uri] possible_transition_years = [2000, 2005] args = { 'workspace_dir': os.path.join(workspace, 'workspace'), 'results_suffix': 'test', 'lulc_lookup_uri': lulc_lookup_uri, 'lulc_transition_matrix_uri': lulc_transition_matrix_uri, 'lulc_baseline_map_uri': raster_0_uri, 'lulc_baseline_year': 1995, 'lulc_transition_maps_list': possible_transitions[:num_transitions+1], 'lulc_transition_years_list': possible_transition_years[:num_transitions+1], 'analysis_year': 2010, 'carbon_pool_initial_uri': carbon_pool_initial_uri, 'carbon_pool_transient_uri': carbon_pool_transient_uri, 'do_economic_analysis': False, } utils.make_directories([args['workspace_dir']]) if valuation: args.update({ 'do_economic_analysis': True, 'do_price_table': False, 'price': 2., 'inflation_rate': 5., 'price_table_uri': None, 'discount_rate': 2. }) return args
def _get_preprocessor_args(args_choice, workspace): """Create and return arguments for preprocessor model. Args: args_choice (int): which arguments to return workspace (string): The path to a workspace directory. Returns: args (dict): preprocessor arguments """ band_matrices_zeros = [numpy.zeros((2, 2))] band_matrices_ones = [numpy.ones((2, 3))] # tests alignment band_matrices_nodata = [numpy.ones((2, 2)) * NODATA_INT] srs = pygeotest.sampledata.SRS_WILLAMETTE lulc_lookup_uri = _create_table( os.path.join(workspace, 'lulc_lookup.csv'), lulc_lookup_list) raster_0_uri = pygeotest.create_raster_on_disk( band_matrices_ones, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_0.tif')) raster_1_uri = pygeotest.create_raster_on_disk( band_matrices_ones, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_1.tif')) raster_2_uri = pygeotest.create_raster_on_disk( band_matrices_ones, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_2.tif')) raster_3_uri = pygeotest.create_raster_on_disk( band_matrices_zeros, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_3.tif')) raster_4_uri = pygeotest.create_raster_on_disk( band_matrices_zeros, srs.origin, srs.projection, -1, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_4.tif')) raster_nodata_uri = pygeotest.create_raster_on_disk( band_matrices_nodata, srs.origin, srs.projection, NODATA_INT, srs.pixel_size(100), datatype=gdal.GDT_Int32, filename=os.path.join(workspace, 'raster_4.tif')) args = { 'workspace_dir': os.path.join(workspace, 'workspace'), 'results_suffix': 'test', 'lulc_lookup_uri': lulc_lookup_uri, 'lulc_snapshot_list': [raster_0_uri, raster_1_uri, raster_2_uri] } args2 = { 'workspace_dir': os.path.join(workspace, 'workspace'), 'results_suffix': 'test', 'lulc_lookup_uri': lulc_lookup_uri, 'lulc_snapshot_list': [raster_0_uri, raster_1_uri, raster_3_uri] } args3 = { 'workspace_dir': os.path.join(workspace, 'workspace'), 'results_suffix': 'test', 'lulc_lookup_uri': lulc_lookup_uri, 'lulc_snapshot_list': [raster_0_uri, raster_nodata_uri, raster_3_uri] } args4 = { 'workspace_dir': os.path.join(workspace, 'workspace'), 'results_suffix': 'test', 'lulc_lookup_uri': lulc_lookup_uri, 'lulc_snapshot_list': [raster_0_uri, raster_nodata_uri, raster_4_uri] } if args_choice == 1: return args elif args_choice == 2: return args2 elif args_choice == 3: return args3 else: return args4