diff --git a/plio/io/io_gdal.py b/plio/io/io_gdal.py index 848e28586db662cafcd58d72b8f258571742a160..555f73ebe17d61838b74e4a5286c4f038b9caeba 100644 --- a/plio/io/io_gdal.py +++ b/plio/io/io_gdal.py @@ -468,30 +468,39 @@ class GeoDataset(object): def array_to_raster(array, file_name, projection=None, geotransform=None, outformat='GTiff', - ndv=None): + ndv=None, bittype='GDT_Float64'): """ Converts the given NumPy array to a raster format using the GeoDataset class. Parameters ---------- array : ndarray - + The data to be written via GDAL - file_name : str + file_name : str + The output file PATH (relative or absolute) - projection : - Default projection=None. + projection : object + A GDAL readable projection object, WKT string, PROJ4 string, etc. + Default: None - geotransform : object - Default geotransform=None. + geotransform : object + A six parameter geotransformation + Default:None. outformat : str - Default outformat='GTiff'. + A GDAL supported output format + Default: 'GTiff'. ndv : float - The no data value for the given band. See no_data_value(). Default ndv=None. + The no data value for the given band. + Default: None. + bittype : str + A GDAL supported bittype, e.g. GDT_Int32 + Default: GDT_Float64 """ + driver = gdal.GetDriverByName(outformat) try: y, x, bands = array.shape @@ -501,8 +510,7 @@ def array_to_raster(array, file_name, projection=None, y, x = array.shape single = True - #This is a crappy hard code to 32bit. - dataset = driver.Create(file_name, x, y, bands, gdal.GDT_Float64) + dataset = driver.Create(file_name, x, y, bands, getattr(gdal, bittype)) if geotransform: dataset.SetGeoTransform(geotransform) diff --git a/plio/io/tests/test_io_gdal.py b/plio/io/tests/test_io_gdal.py index 8a29eabf08aeb4b50cf170e4e9137caf00b8495e..b7e79fdd0beae49ef0397b981094589418fe5875 100644 --- a/plio/io/tests/test_io_gdal.py +++ b/plio/io/tests/test_io_gdal.py @@ -179,14 +179,14 @@ class TestWriter(unittest.TestCase): dataset = io_gdal.GeoDataset('test.tif') self.assertEqual(gt, dataset.geotransform) - def test_with_no_data_value(self): + def test_with_no_data_value_nd(self): no_data_value = 0.0 - #nd array io_gdal.array_to_raster(self.ndarr, 'test.tif', ndv=no_data_value) dataset = io_gdal.GeoDataset('test.tif') self.assertEqual(dataset.no_data_value, no_data_value) - #array + def test_with_no_data_value(self): + no_data_value = 0.0 io_gdal.array_to_raster(self.arr, 'test.tif', ndv=no_data_value) dataset = io_gdal.GeoDataset('test.tif') self.assertEqual(dataset.no_data_value, no_data_value) diff --git a/plio/io/tests/test_io_hdf.py b/plio/io/tests/test_io_hdf.py index fa27dd692ef108a2e57572aa3bc94a811a89b202..cdfe33aac407a1d0cb8b506dc6bf09923a783b66 100644 --- a/plio/io/tests/test_io_hdf.py +++ b/plio/io/tests/test_io_hdf.py @@ -17,10 +17,6 @@ class TestHDF(unittest.TestCase): cls.df = pd.DataFrame(cls.x[['bar', 'baz']], index=cls.x['index'], columns=['bar', 'baz']) - @classmethod - def tearDownClass(cls): - os.remove('test_io_hdf.hdf') - def test_df_sarray(self): converted = self.hdf.df_to_sarray(self.df.reset_index()) np.testing.assert_array_equal(converted, self.x) @@ -29,4 +25,8 @@ class TestHDF(unittest.TestCase): converted = self.hdf.sarray_to_df(self.x) self.assertTrue((self.df == converted).all().all()) - + @classmethod + def tearDownClass(cls): + try: + os.remove('test_io_hdf.hdf') + except: pass