diff --git a/geoarray/baseclasses.py b/geoarray/baseclasses.py index 01030fd4faca32ba69b14ce95eb9f29f4b0112e0..b212f16ce1225fb3c8ee4ee9220ce39d41f80c54 100644 --- a/geoarray/baseclasses.py +++ b/geoarray/baseclasses.py @@ -1487,11 +1487,7 @@ class GeoArray(object): # apply zslice to bandnames and metadata if zslicing: bNs_out = list(np.array(list(self.bandnames))[zslice]) - - _meta_out = deepcopy(self.meta) - if self._metadata: - for k, v in _meta_out.band_meta.items(): - _meta_out.band_meta[k] = list(np.array(v)[zslice]) + _meta_out = self.metadata.get_band_subset(bandslice=zslice) else: bNs_out = list(self.bandnames) _meta_out = self.meta diff --git a/geoarray/metadata.py b/geoarray/metadata.py index 9a44e0341f25ea0251c81b415d03d76ce64efe0d..a79a71f422c2d88b546ec4d6b8d72496f644a5c7 100644 --- a/geoarray/metadata.py +++ b/geoarray/metadata.py @@ -2,8 +2,10 @@ import os from pprint import pformat +from copy import deepcopy from geopandas import GeoDataFrame, GeoSeries +import numpy as np try: from osgeo import gdal except ImportError: @@ -31,7 +33,7 @@ class GDAL_Metadata(object): self.bands = nbands self.filePath = filePath - self.format = '' + self.fileFormat = '' if filePath: self.read_from_file(filePath) @@ -140,13 +142,13 @@ class GDAL_Metadata(object): raise Exception('Error reading file: ' + gdal.GetLastErrorMsg()) self.bands = ds.RasterCount - self.format = ds.GetDriver().GetDescription() + self.fileFormat = ds.GetDriver().GetDescription() ############### # ENVI format # ############### - if self.format == 'ENVI': + if self.fileFormat == 'ENVI': metadict = ds.GetMetadata('ENVI') for k, v in metadict.items(): @@ -201,3 +203,14 @@ class GDAL_Metadata(object): def to_ENVI_metadict(self): return dict(zip(self.all_meta.keys(), [self._convert_param_to_ENVI_str(i) for i in self.all_meta.values()])) + + def get_band_subset(self, bandslice=None): + bandslice = bandslice or slice(None) + meta_sub = deepcopy(self) + + for k, v in meta_sub.band_meta.items(): + meta_sub.band_meta[k] = list(np.array(v)[bandslice]) + + meta_sub.bands = len(list(range(*bandslice.indices(bandslice.stop)))) + + return meta_sub