Commit 8a3b342b authored by Daniel Scheffler's avatar Daniel Scheffler

Fixed issue that bandnames are not written to ENVI header by GeoArray.save().

parent 297fa33f
Pipeline #3070 passed with stages
in 1 minute and 25 seconds
...@@ -41,6 +41,7 @@ test_styles: ...@@ -41,6 +41,7 @@ test_styles:
- source /root/miniconda3/bin/activate - source /root/miniconda3/bin/activate
- export GDAL_DATA=/root/miniconda3/share/gdal - export GDAL_DATA=/root/miniconda3/share/gdal
- export PYTHONPATH=$PYTHONPATH:/root # /root <- directory needed later - export PYTHONPATH=$PYTHONPATH:/root # /root <- directory needed later
- pip install "pycodestyle>=2.0.0,!=2.4.0" # TODO remove as soon as docker runner is recreated
- make lint - make lint
artifacts: artifacts:
paths: paths:
......
...@@ -159,21 +159,26 @@ class GeoArray(object): ...@@ -159,21 +159,26 @@ class GeoArray(object):
# type: (list) -> None # type: (list) -> None
if list_bandnames: if list_bandnames:
assert isinstance(list_bandnames, list), "A list must be given when setting the 'bandnames' attribute. " \ if not isinstance(list_bandnames, list):
"Received %s." % type(list_bandnames) raise TypeError("A list must be given when setting the 'bandnames' attribute. "
assert len(list_bandnames) == self.bands, \ "Received %s." % type(list_bandnames))
'Number of given bandnames does not match number of bands in array.' if len(list_bandnames) != self.bands:
assert len(list(set([type(b) for b in list_bandnames]))) == 1 and type(list_bandnames[0] == 'str'), \ raise ValueError('Number of given bandnames does not match number of bands in array.')
"'bandnames must be a set of strings. Got other datetypes in there.'" if len(list(set([type(b) for b in list_bandnames]))) != 1 or not isinstance(list_bandnames[0], str):
raise ValueError("'bandnames must be a set of strings. Got other datatypes in there.'")
bN_dict = OrderedDict((band, i) for i, band in enumerate(list_bandnames)) bN_dict = OrderedDict((band, i) for i, band in enumerate(list_bandnames))
assert len(bN_dict) == self.bands, \
'Bands must not have the same name. Received band list: %s' % list_bandnames if len(bN_dict) != self.bands:
raise ValueError('Bands must different names. Received band list: %s' % list_bandnames)
self._bandnames = bN_dict self._bandnames = bN_dict
# update bandnames in metadata try:
if self._metadata is not None:
self.metadata.band_meta['band_names'] = list_bandnames self.metadata.band_meta['band_names'] = list_bandnames
except AttributeError:
# in case self._metadata is None
pass
else: else:
del self.bandnames del self.bandnames
...@@ -893,6 +898,12 @@ class GeoArray(object): ...@@ -893,6 +898,12 @@ class GeoArray(object):
# write dataset # 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 if creationOptions else [])
# # 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)
# gdalnumeric.SaveArray(out_arr, out_path, format=fmt, prototype=ds_inmem) # expects bands,rows,columns
# ds_out = gdal.Open(out_path)
del ds_inmem del ds_inmem
################ ################
...@@ -909,7 +920,10 @@ class GeoArray(object): ...@@ -909,7 +920,10 @@ class GeoArray(object):
if 'band_names' in envi_metadict: if 'band_names' in envi_metadict:
for bidx in range(self.bands): for bidx in range(self.bands):
band = ds_out.GetRasterBand(bidx + 1) band = ds_out.GetRasterBand(bidx + 1)
band.SetDescription(self.metadata.band_meta['band_names'][bidx].strip()) bandname = self.metadata.band_meta['band_names'][bidx].strip()
band.SetDescription(bandname)
assert band.GetDescription() == bandname
del band del band
if 'description' in envi_metadict: if 'description' in envi_metadict:
...@@ -939,10 +953,6 @@ class GeoArray(object): ...@@ -939,10 +953,6 @@ class GeoArray(object):
del band del band
ds_out.FlushCache() ds_out.FlushCache()
# 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)
# gdalnumeric.SaveArray(out_arr, out_path, format=fmt, prototype=ds) # expects bands,rows,columns
del ds_out del ds_out
else: else:
......
...@@ -33,7 +33,7 @@ dependencies: ...@@ -33,7 +33,7 @@ dependencies:
- six - six
- spectral - spectral
- flake8 - flake8
- pycodestyle - pycodestyle<2.4.0 # fixes ImportError: module 'pycodestyle' has no attribute 'break_around_binary_operator'
- pylint - pylint
- pydocstyle - pydocstyle
- nose - nose
......
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