3232
3333import traceback
3434from pathlib import Path
35+ from datetime import datetime
3536
3637from qgis .core import (
3738 Qgis ,
4950 QgsProcessingParameterRasterLayer ,
5051 QgsProcessingParameterString ,
5152 QgsProject ,
53+ QgsVectorLayer ,
54+ QgsProcessingOutputFile ,
5255)
5356from qgis .PyQt .QtCore import QCoreApplication
5457
5558from ..REMEDY_GIS_RiskTool .BegrensSkade import mainBegrensSkade_Excavation
56- from ..utilities .AddLayersTask import AddLayersTask
5759from ..utilities .gui import GuiUtils
5860from ..utilities .logger import CustomLogger
5961from ..utilities .methodslib import (
@@ -117,7 +119,6 @@ def __init__(self):
117119 self .feature_name = None # Default value
118120 self .layers_info = {}
119121 self .styles_dir_path = Path ()
120- self .add_layers_task = AddLayersTask ()
121122 self .logger .info ("__INIT__ - Finished initialize BegrensSkadeExcavation " )
122123
123124 def tr (self , string ):
@@ -449,8 +450,9 @@ def initAlgorithm(self, config):
449450 self .OUTPUT_FEATURE_NAME ,
450451 self .tr (
451452 "Naming Conventions for Analysis and Features (Output feature name appended to file-names)"
452- ),
453- )
453+ )
454+ ),
455+ createOutput = True
454456 )
455457 self .addParameter (
456458 QgsProcessingParameterCrs (
@@ -465,6 +467,24 @@ def initAlgorithm(self, config):
465467 self .tr ("Output Folder" ),
466468 )
467469 )
470+ self .addOutput (
471+ QgsProcessingOutputFile (
472+ self .OUTPUT_BUILDING ,
473+ self .tr ("Output Buildings Shapefile" ),
474+ )
475+ )
476+ self .addOutput (
477+ QgsProcessingOutputFile (
478+ self .OUTPUT_WALL ,
479+ self .tr ("Output Walls Shapefile" ),
480+ )
481+ )
482+ self .addOutput (
483+ QgsProcessingOutputFile (
484+ self .OUTPUT_CORNER ,
485+ self .tr ("Output Corners Shapefile" ),
486+ )
487+ )
468488
469489 self .logger .info ("initAlgorithm - Done setting up the inputs." )
470490
@@ -801,7 +821,7 @@ def processAlgorithm(self, parameters, context, feedback):
801821 return {}
802822
803823 #################### HANDLE THE RESULT ###############################
804- feedback .setProgress (80 )
824+ feedback .setProgress (90 )
805825 self .logger .info (f"PROCESS - OUTPUT BUILDINGS: { output_shapefiles [0 ]} " )
806826 self .logger .info (f"PROCESS - OUTPUT WALL: { output_shapefiles [1 ]} " )
807827 self .logger .info (f"PROCESS - OUTPUT CORNER: { output_shapefiles [2 ]} " )
@@ -820,80 +840,89 @@ def processAlgorithm(self, parameters, context, feedback):
820840 "shape_path" : output_shapefiles [1 ],
821841 "style_name" : "WALL-ANGLE.qml" ,
822842 },
823- "BUILDING-TOTAL-SETTLMENT" : {
824- "shape_path" : output_shapefiles [0 ],
825- "style_name" : "BUILDING-TOTAL-SETTLMENT_sv_tot.qml" ,
826- },
827843 "BUILDING-TOTAL-ANGLE" : {
828844 "shape_path" : output_shapefiles [0 ],
829845 "style_name" : "BUILDING-TOTAL-ANGLE_max_angle.qml" ,
830846 },
847+ "BUILDING-TOTAL-SETTLMENT" : {
848+ "shape_path" : output_shapefiles [0 ],
849+ "style_name" : "BUILDING-TOTAL-SETTLMENT_sv_tot.qml" ,
850+ }
831851 }
832852 if bVulnerability :
833853 self .layers_info .update (
834854 {
835- "BUILDING-RISK-SETTLMENT" : {
836- "shape_path" : output_shapefiles [0 ],
837- "style_name" : "BUILDING-TOTAL-RISK-SELLMENT_risk_tots.qml" ,
838- },
839855 "BUILDING-RISK-ANGLE" : {
840856 "shape_path" : output_shapefiles [0 ],
841857 "style_name" : "BUILDING-TOTAL-RISK-ANGLE_risk_angle.qml" ,
842858 },
859+ "BUILDING-RISK-SETTLMENT" : {
860+ "shape_path" : output_shapefiles [0 ],
861+ "style_name" : "BUILDING-TOTAL-RISK-SELLMENT_risk_tots.qml" ,
862+ }
843863 }
844864 )
845865
846- feedback .setProgress (90 )
866+ feedback .setProgress (100 )
847867 feedback .pushInfo ("PROCESS - Finished processing!" )
868+
848869 # Return the results of the algorithm.
849- return {
850- self .OUTPUT_BUILDING : output_shapefiles [0 ],
851- self .OUTPUT_WALL : output_shapefiles [1 ],
852- self . OUTPUT_CORNER : output_shapefiles [ 2 ],
853- }
854-
870+ return {self . OUTPUT_BUILDING : output_shapefiles [ 0 ],
871+ self .OUTPUT_WALL : output_shapefiles [1 ],
872+ self .OUTPUT_CORNER : output_shapefiles [2 ],
873+
874+ }
875+
855876 def postProcessAlgorithm (self , context , feedback ):
856877 """
857- Handles the post-processing steps of the algorithm, specifically adding output layers to the QGIS project.
858-
859- This method creates and executes a process to add layers to the QGIS interface, applying predefined styles
860- and organizing them within a specified group. It leverages the `AddLayersTask` class to manage layer
861- addition in a way that ensures thread safety and proper GUI updates.
862-
863- Parameters:
864- - context (QgsProcessingContext): The context of the processing, providing access to the QGIS project and other relevant settings.
865- - feedback (QgsProcessingFeedback): The object used to report progress and log messages back to the user.
866-
867- Returns:
868- - dict: An empty dictionary. This method does not produce output parameters but instead focuses on the side effect of adding layers to the project.
869-
870- Note:
871- This method sets up a task for layer addition, defining success and failure callbacks to provide user feedback.
872- It manually starts the process and handles its completion.
878+ This method is called after processAlgorithm finishes.
879+ Here, we manually load the output shapefiles, apply QML styles,
880+ and place them under a custom group in the layer tree.
873881 """
874- ######### EXPERIMENTAL ADD LAYERS TO GUI #########
875- # Create the task
876- self .add_layers_task .setParameters (
877- self .layers_info ,
878- self .feature_name ,
879- self .styles_dir_path ,
880- self .logger ,
881- )
882-
883- # Define a slot to handle the task completion
884- def onTaskCompleted (success ):
885- if success :
886- feedback .pushInfo ("POSTPROCESS - Layers added successfully." )
887- feedback .setProgress (100 )
882+ project = context .project ()
883+ root = project .layerTreeRoot ()
884+
885+ # Create (or find) a group at the top level named by 'self.feature_name'.
886+ group_name = self .feature_name
887+ group = root .findGroup (group_name )
888+ if not group :
889+ group = root .insertGroup (0 , group_name )
890+
891+ # 2) Loop through the entries stored in 'self.layers_info' defined during processAlgorithm
892+ for layer_label , layer_info in self .layers_info .items ():
893+ shape_path = layer_info ["shape_path" ] # e.g. "C:/somefolder/buildings.shp"
894+ style_name = layer_info ["style_name" ] # e.g. "BUILDING-TOTAL-SETTLMENT_sv_tot.qml"
895+
896+ # Generate a unique layer name with timestamp
897+ timestamp = datetime .now ().strftime ("%Y%m%d_%H%M" )
898+ final_layer_name = f"{ layer_label } _{ timestamp } "
899+
900+ # Create the QgsVectorLayer from the file path
901+ layer = QgsVectorLayer (shape_path , final_layer_name , "ogr" )
902+ if not layer .isValid ():
903+ feedback .reportError (f"Could not load layer from file: { shape_path } " )
904+ continue
905+
906+ # Load the QML style if it exists
907+ style_path = self .styles_dir_path / style_name # e.g. /path/to/styles/BUILDING-TOTAL-SETTLMENT_sv_tot.qml
908+ if style_path .is_file ():
909+ layer .loadNamedStyle (str (style_path ))
910+ layer .triggerRepaint ()
888911 else :
889- feedback .reportError ("POSTPROCESS - Failed to add layers." )
890- feedback .setProgress (100 )
912+ feedback .reportError (f"Style file not found: { style_path } " )
913+
914+ # Add the layer to the project (layer registry) *without* adding to the root TOC
915+ QgsProject .instance ().addMapLayer (layer , False )
916+
917+ # Place the layer under the group at the bottom
918+ group .insertLayer (- 1 , layer )
891919
892- # Connect the task's completed signal to the slot
893- self .add_layers_task .taskCompleted .connect (onTaskCompleted )
920+ # Ensure the new layer node is visible
921+ node = group .findLayer (layer .id ())
922+ if node :
923+ node .setItemVisibilityChecked (True )
894924
895- # Start the task
896- success = self .add_layers_task .run ()
897- self .add_layers_task .finished (success )
925+ feedback .pushInfo (f"Loaded and styled layer '{ final_layer_name } ' in group '{ group_name } '." )
898926
899- return {}
927+ feedback .pushInfo ("postProcessAlgorithm complete." )
928+ return {}
0 commit comments