From 8a3b342b24fef6ea0f12b27ca209bb35c7f8c930 Mon Sep 17 00:00:00 2001 From: Daniel Scheffler Date: Wed, 8 Aug 2018 18:31:04 +0200 Subject: [PATCH] Fixed issue that bandnames are not written to ENVI header by GeoArray.save(). --- .gitlab-ci.yml | 1 + geoarray/baseclasses.py | 40 ++++++++++++------- .../context/environment_geoarray.yml | 2 +- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b386188..2e7f186 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -41,6 +41,7 @@ test_styles: - source /root/miniconda3/bin/activate - export GDAL_DATA=/root/miniconda3/share/gdal - 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 artifacts: paths: diff --git a/geoarray/baseclasses.py b/geoarray/baseclasses.py index cde6143..01030fd 100644 --- a/geoarray/baseclasses.py +++ b/geoarray/baseclasses.py @@ -159,21 +159,26 @@ class GeoArray(object): # type: (list) -> None if list_bandnames: - assert isinstance(list_bandnames, list), "A list must be given when setting the 'bandnames' attribute. " \ - "Received %s." % type(list_bandnames) - assert len(list_bandnames) == self.bands, \ - 'Number of given bandnames does not match number of bands in array.' - assert len(list(set([type(b) for b in list_bandnames]))) == 1 and type(list_bandnames[0] == 'str'), \ - "'bandnames must be a set of strings. Got other datetypes in there.'" + if not isinstance(list_bandnames, list): + raise TypeError("A list must be given when setting the 'bandnames' attribute. " + "Received %s." % type(list_bandnames)) + if len(list_bandnames) != self.bands: + raise ValueError('Number of given bandnames does not match number of bands in array.') + 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)) - 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 - # update bandnames in metadata - if self._metadata is not None: + try: self.metadata.band_meta['band_names'] = list_bandnames + except AttributeError: + # in case self._metadata is None + pass else: del self.bandnames @@ -893,6 +898,12 @@ class GeoArray(object): # write dataset 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 ################ @@ -909,7 +920,10 @@ class GeoArray(object): if 'band_names' in envi_metadict: for bidx in range(self.bands): 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 if 'description' in envi_metadict: @@ -939,10 +953,6 @@ class GeoArray(object): del band 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 else: diff --git a/tests/CI_docker/context/environment_geoarray.yml b/tests/CI_docker/context/environment_geoarray.yml index f5c2e0e..2bb3fe1 100644 --- a/tests/CI_docker/context/environment_geoarray.yml +++ b/tests/CI_docker/context/environment_geoarray.yml @@ -33,7 +33,7 @@ dependencies: - six - spectral - flake8 - - pycodestyle + - pycodestyle<2.4.0 # fixes ImportError: module 'pycodestyle' has no attribute 'break_around_binary_operator' - pylint - pydocstyle - nose -- GitLab