Commit e2d6e752 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

GeoArray.meta attributes are now correctly written in case of linked (not in-memory) datasets.



Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent a0eb45ae
......@@ -1011,12 +1011,18 @@ class GeoArray(object):
envi_metadict = self.metadata.to_ENVI_metadict()
#####################
# write raster data #
#####################
if self.is_inmem:
# write in-memory data
ds_inmem = get_GDAL_ds_inmem(self.arr, self.geotransform, self.projection,
self.nodata) # expects rows,columns,bands
# write dataset
ds_out = driver.CreateCopy(out_path, ds_inmem, options=creationOptions if creationOptions else [])
ds_out = driver.CreateCopy(out_path, ds_inmem,
options=creationOptions or []) # noqa
# # rows, columns, bands => bands, rows, columns
# out_arr = self.arr if self.ndim == 2 else np.swapaxes(np.swapaxes(self.arr, 0, 2), 1, 2)
......@@ -1024,12 +1030,33 @@ class GeoArray(object):
# ds_out = gdal.Open(out_path)
del ds_inmem
del ds_out
else:
# transform linked image dataset (not in-memory) to target format
src_ds = gdal.Open(self.filePath)
if not src_ds:
raise Exception('Error reading file: ' + gdal.GetLastErrorMsg())
# metadomains = {dom: src_ds.GetMetadata(dom) for dom in src_ds.GetMetadataDomainList()}
gdal.Translate(out_path, src_ds, format=fmt, creationOptions=creationOptions)
del src_ds
if not os.path.exists(out_path):
raise Exception(gdal.GetLastErrorMsg())
################
# set metadata #
################
################
# set metadata #
################
# NOTE: The dataset has to be written BEFORE metadata are added. Otherwise, metadata are not written.
# NOTE: The dataset has to be written BEFORE metadata are added. Otherwise, metadata are not written.
ds_out = gdal.Open(out_path, gdal.GA_Update)
try:
if not ds_out:
raise Exception('Error reading file: ' + gdal.GetLastErrorMsg())
# ENVI #
########
......@@ -1102,28 +1129,10 @@ class GeoArray(object):
band.FlushCache()
del band
ds_out.FlushCache()
del ds_out
else:
src_ds = gdal.Open(self.filePath)
if not src_ds:
raise Exception('Error reading file: ' + gdal.GetLastErrorMsg())
gdal.Translate(out_path, src_ds, format=fmt, creationOptions=creationOptions)
del src_ds
# add band names
if 'band_names' in envi_metadict:
ds_out = gdal.Open(out_path)
for bidx in range(self.bands):
band = ds_out.GetRasterBand(bidx + 1)
band.SetDescription(self.metadata.band_meta['band_names'][bidx])
del band
ds_out.FlushCache()
if not os.path.exists(out_path):
raise Exception(gdal.GetLastErrorMsg())
finally:
del ds_out
def dump(self, out_path):
# type: (str) -> None
......
......@@ -452,6 +452,8 @@ class Test_GeoArray(TestCase):
self.assertTrue(np.array_equal(gA[:], gA_copy[:]))
self.assertEqual(gA._nodata, gA_copy._nodata)
# TODO validate written metadata (inMem, notInMem, in case a notInMem dataset already has custom metadata)
def test_show(self):
# test 3D case
self.gA.show()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment