From 80abc989fba6b5fa5e9f18627e05c1436d00bc16 Mon Sep 17 00:00:00 2001
From: "Daniel.Frisinghelli" <daniel.frisinghelli@eurac.edu>
Date: Tue, 16 Feb 2021 11:25:58 +0100
Subject: [PATCH] Keep hdf metadata when converting to GeoTiff.

---
 pysegcnn/core/utils.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/pysegcnn/core/utils.py b/pysegcnn/core/utils.py
index 1e18dea..4be32f9 100644
--- a/pysegcnn/core/utils.py
+++ b/pysegcnn/core/utils.py
@@ -510,6 +510,7 @@ def hdf2tifs(path, outpath=None, overwrite=False, create_stack=True, **kwargs):
         for ds in hdf:
 
             # name of the current subdataset
+            hdf_ds = gdal.Open(ds[0])
             name = ds[0].split(':')[-1].lower()
 
             # filename of the GeoTIFF
@@ -518,9 +519,14 @@ def hdf2tifs(path, outpath=None, overwrite=False, create_stack=True, **kwargs):
 
             # convert hdf subdataset to GeoTIFF
             LOGGER.info('Converting: {}'.format(tif_name.name))
-            gdal.Translate(str(tif_name), gdal.Open(ds[0]), creationOptions=[
+            gdal.Translate(str(tif_name), hdf_ds, creationOptions=[
                 'COMPRESS=DEFLATE', 'PREDICTOR=1', 'TILED=YES'], **kwargs)
 
+            # set metadata field
+            tif_ds = gdal.Open(str(tif_name))
+            tif_ds.SetMetadata(hdf_ds.GetMetadata())
+            del tif_ds
+
         # check whether to create a GeoTIFF stack
         if create_stack:
             # filename for the GeoTIFF stack
-- 
GitLab