From bc4719535c00a81939804a561483948e8175ec37 Mon Sep 17 00:00:00 2001
From: "Daniel.Frisinghelli" <daniel.frisinghelli@eurac.edu>
Date: Fri, 11 Dec 2020 16:34:25 +0100
Subject: [PATCH] Added a check for an empty dataset.

---
 pysegcnn/core/utils.py | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/pysegcnn/core/utils.py b/pysegcnn/core/utils.py
index 3a5ee0b..8da06b6 100644
--- a/pysegcnn/core/utils.py
+++ b/pysegcnn/core/utils.py
@@ -365,31 +365,35 @@ def hdf2tifs(path, outpath=None, overwrite=False, create_stack=True, **kwargs):
     # read the hdf dataset
     hdf = gdal.Open(str(path)).GetSubDatasets()
 
-    # iterate over the different subdatasets in the hdf
-    for ds in hdf:
+    # check if the dataset is not empty
+    if hdf:
 
-        # name of the current subdataset
-        name = ds[0].split(':')[-1]
+        # iterate over the different subdatasets in the hdf
+        for ds in hdf:
+
+            # name of the current subdataset
+            name = ds[0].split(':')[-1]
 
-        # filename of the GeoTIFF
-        tif_name = outpath.joinpath(path.name.replace(path.suffix,
-                                                      '_{}.tif'.format(name)))
+            # filename of the GeoTIFF
+            tif_name = outpath.joinpath(
+                path.name.replace(path.suffix, '_{}.tif'.format(name)))
 
-        # convert hdf subdataset to GeoTIFF
-        gdal.Translate(str(tif_name), gdal.Open(ds[0]), **kwargs)
+            # convert hdf subdataset to GeoTIFF
+            gdal.Translate(str(tif_name), gdal.Open(ds[0]), **kwargs)
 
-    # check whether to create a GeoTIFF stack
-    if create_stack:
-        # filename for the GeoTIFF stack
-        stk = tif_name.parent.joinpath(path.name.replace(path.suffix, '.tif'))
-        LOGGER.info('Creating GeoTIFF stack: {}'.format(stk))
+        # check whether to create a GeoTIFF stack
+        if create_stack:
+            # filename for the GeoTIFF stack
+            stk = tif_name.parent.joinpath(
+                path.name.replace(path.suffix, '.tif'))
+            LOGGER.info('Creating GeoTIFF stack: {}'.format(stk))
 
-        # generated GeoTIFF files
-        tifs = [str(f) for f in outpath.iterdir() if f.suffix in
-                ['.tif', '.TIF']]
+            # generated GeoTIFF files
+            tifs = [str(f) for f in outpath.iterdir() if f.suffix in
+                    ['.tif', '.TIF']]
 
-        # create stacked GeoTIFF
-        stack_tifs(str(stk), tifs)
+            # create stacked GeoTIFF
+            stack_tifs(str(stk), tifs)
 
     return
 
-- 
GitLab