Commit a0b2b7bc authored by Daniel Scheffler's avatar Daniel Scheffler

Band names and description are now correctly saved in ENVI format.

parent e04db20b
Pipeline #3067 failed with stages
in 1 minute and 12 seconds
......@@ -884,6 +884,8 @@ class GeoArray(object):
if not os.path.isdir(os.path.dirname(out_path)):
os.makedirs(os.path.dirname(out_path))
envi_metadict = self.metadata.to_ENVI_metadict()
if self.is_inmem:
ds_inmem = get_GDAL_ds_inmem(self.arr, self.geotransform, self.projection,
self.nodata) # expects rows,columns,bands
......@@ -895,12 +897,16 @@ class GeoArray(object):
# set metadata
# NOTE: The dataset has to be written BEFORE metadata are added. Otherwise, metadata are not written.
if fmt == 'ENVI':
envi_metadict = self.metadata.to_ENVI_metadict()
ds_out.SetMetadata(envi_metadict, 'ENVI')
if 'band_names' in envi_metadict:
ds_out.SetMetadata({'Band_%s' % str(bidx + 1): self.metadata.band_meta['band_names'][bidx]
for bidx in range(self.bands)})
for bidx in range(self.bands):
band = ds_out.GetRasterBand(bidx + 1)
band.SetDescription(self.metadata.band_meta['band_names'][bidx])
del band
if 'description' in envi_metadict:
ds_out.SetDescription(envi_metadict['description'])
ds_out.FlushCache()
gdal.Unlink(out_path + '.aux.xml')
......@@ -910,12 +916,18 @@ class GeoArray(object):
if self.metadata.global_meta:
ds_out.SetMetadata(self.metadata.global_meta)
if 'description' in envi_metadict:
ds_out.SetDescription(envi_metadict['description'])
# set band domain metadata
for bidx in range(self.bands):
band = ds_out.GetRasterBand(bidx + 1)
meta2write = dict((k, repr(v)) for k, v in self.metadata.band_meta.items() if v is not np.nan)
band.SetMetadata(meta2write)
if 'band_names' in envi_metadict:
band.SetDescription(self.metadata.band_meta['band_names'][bidx])
band.FlushCache()
del band
......@@ -935,6 +947,15 @@ class GeoArray(object):
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
if not os.path.exists(out_path):
raise Exception(gdal.GetLastErrorMsg())
......
Markdown is supported
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