Commit 0fb58ef8 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Nodata values are now properly written to ENVI header files.

parent 8d362c57
......@@ -354,7 +354,7 @@ class GeoArray(object):
if not self.is_inmem:
if self._nodata is None:
self._nodata = self.find_noDataVal()
if self._nodata == 'ambiguous':
warnings.warn('Nodata value could not be clearly identified. It has been set to None.')
self._nodata = None
......@@ -369,6 +369,9 @@ class GeoArray(object):
# type: (Union[int, None]) -> None
self._nodata = value
if self._metadata and value is not None:
self.metadata.global_meta.update({'data ignore value': str(value)})
def mask_nodata(self):
......@@ -515,7 +518,7 @@ class GeoArray(object):
if self._metadata is not None:
return self._metadata
default = GDAL_Metadata(nbands=self.bands)
default = GDAL_Metadata(nbands=self.bands, nodata_allbands=self._nodata)
self._metadata = default
if not self.is_inmem:
......@@ -705,6 +708,7 @@ class GeoArray(object):
nodata = None
self.nodata = nodata
return nodata
def set_gdalDataset_meta(self):
......@@ -736,12 +740,12 @@ class GeoArray(object):
if 'nodata' not in self._initParams or self._initParams['nodata'] is None:
band = ds.GetRasterBand(1)
# FIXME this does not support different nodata values within the same file
self._nodata = band.GetNoDataValue()
self.nodata = band.GetNoDataValue()
# set metadata attribute
if self.is_inmem or not self.filePath:
# metadata cannot be read from disk -> set it to the default
self._metadata = GDAL_Metadata(nbands=self.bands)
self._metadata = GDAL_Metadata(nbands=self.bands, nodata_allbands=self._nodata)
self._metadata = GDAL_Metadata(filePath=self.filePath)
......@@ -4,6 +4,7 @@ import os
from pprint import pformat
from copy import deepcopy
from typing import Union # noqa F401 # flake8 issue
from collections import OrderedDict
from geopandas import GeoDataFrame, GeoSeries
import numpy as np
......@@ -27,18 +28,22 @@ autohandled_meta = [
class GDAL_Metadata(object):
def __init__(self, filePath='', nbands=1):
def __init__(self, filePath='', nbands=1, nodata_allbands=None):
# privates
self._global_meta = dict()
self._band_meta = dict()
self._global_meta = OrderedDict()
self._band_meta = OrderedDict()
self.bands = nbands
self.filePath = filePath
self.fileFormat = ''
self.nodata_allbands = nodata_allbands
if filePath:
if nodata_allbands is not None:
self.global_meta.update({'data ignore value': str(nodata_allbands)})
def from_file(cls, filePath):
return GDAL_Metadata(filePath=filePath)
......@@ -73,8 +78,8 @@ class GDAL_Metadata(object):
def band_meta(self, meta_dict):
if not isinstance(meta_dict, dict):
raise TypeError("Expected type 'dict', received '%s'." % type(meta_dict))
if not isinstance(meta_dict, (dict, OrderedDict)):
raise TypeError("Expected type 'dict'/'OrderedDict', received '%s'." % type(meta_dict))
for k, v in meta_dict.items():
if not isinstance(v, list):
......@@ -83,11 +88,11 @@ class GDAL_Metadata(object):
raise ValueError("The length of the given lists must be equal to the number of bands. "
"Received a list with %d items for '%s'." % (len(v), k))
self._band_meta = meta_dict # TODO convert strings to useful types
self._band_meta = OrderedDict(meta_dict) # TODO convert strings to useful types
def all_meta(self):
all_meta = self.global_meta.copy()
all_meta = OrderedDict(self.global_meta.copy())
return all_meta
......@@ -190,7 +195,7 @@ class GDAL_Metadata(object):
return 'Metadata: \n\n' + pformat(self.all_meta)
def to_ENVI_metadict(self):
return dict(zip(self.all_meta.keys(),
return OrderedDict(zip(self.all_meta.keys(),
[self._convert_param_to_ENVI_str(i) for i in self.all_meta.values()]))
def get_subset(self, bands2extract=None, keys2extract=None):
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