From 20846770bb64b48d5eee127d35d0deaa47e6b417 Mon Sep 17 00:00:00 2001
From: ptresson <paul.tresson@ird.fr>
Date: Fri, 6 Sep 2024 12:25:37 +0200
Subject: [PATCH] update layer names if a 'merged features' allready axists,
 fixes #12

---
 encoder.py | 7 +++++--
 iamap.py   | 3 ++-
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/encoder.py b/encoder.py
index b18c624..43f9a2e 100644
--- a/encoder.py
+++ b/encoder.py
@@ -558,7 +558,10 @@ class EncoderAlgorithm(QgsProcessingAlgorithm):
         if not all_encoding_done :
             dst_path = Path(os.path.join(self.output_subdir,'merged_tmp.tif'))
         else:
-            dst_path = Path(os.path.join(self.output_subdir,'merged.tif'))
+            # dst_path = Path(os.path.join(self.output_subdir,'merged.tif'))
+            ## update filename if a merged.tif file allready exists
+            dst_path, layer_name = get_unique_filename(self.output_subdir, 'merged.tif')
+            dst_path = Path(dst_path)
 
         merge_tiles(
                 tiles = all_tiles, 
@@ -593,7 +596,7 @@ class EncoderAlgorithm(QgsProcessingAlgorithm):
 
         parameters['OUTPUT_RASTER']=dst_path
 
-        return {"Output feature path": self.output_subdir, 'Patch samples saved': self.iPatch, 'OUTPUT_RASTER':dst_path}
+        return {"Output feature path": self.output_subdir, 'Patch samples saved': self.iPatch, 'OUTPUT_RASTER':dst_path, 'OUTPUT_LAYER_NAME':layer_name}
 
     def get_last_batch_done(self):
 
diff --git a/iamap.py b/iamap.py
index a0a36a0..f66f95b 100644
--- a/iamap.py
+++ b/iamap.py
@@ -111,9 +111,10 @@ class IAMap(QObject):
             # Retrieve output parameters from the result dictionary
             if 'OUTPUT_RASTER' in result:
                 output_raster_path = result['OUTPUT_RASTER']
+                output_layer_name = result['OUTPUT_LAYER_NAME']
 
                 # Add the output raster layer to the map canvas
-                self.iface.addRasterLayer(str(output_raster_path), 'merged features')
+                self.iface.addRasterLayer(str(output_raster_path),output_layer_name)
             else:
                 # Handle missing or unexpected output
                 print('Output raster not found in algorithm result.')
-- 
GitLab