Skip to content

Commit

Permalink
Fixes for aggregation workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Oct 5, 2024
1 parent 450cacf commit 08a4596
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
11 changes: 8 additions & 3 deletions geest/core/workflows/aggregation_workflow_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@
from .workflow_base import WorkflowBase
from geest.core.convert_to_8bit import RasterConverter
from geest.utilities import resources_path
from geest.gui.treeview import JsonTreeItem


class AggregationWorkflowBase(WorkflowBase):
"""
Base class for all aggregation workflows (factor, dimension, analysis)
"""

def __init__(self, attributes: dict, feedback: QgsFeedback):
def __init__(self, item: JsonTreeItem, feedback: QgsFeedback):
"""
Initialize the Factor Aggregation with attributes and feedback.
:param attributes: Dictionary containing workflow parameters.
:param item: A reference to a JsonTreeItem object.
:param feedback: QgsFeedback object for progress reporting and cancellation.
"""
super().__init__(attributes, feedback)
# ⭐️ Note that the item is a reference to the JsonTreeItem object so any changes
# made to the item will be reflected directly in the tree view.
super().__init__(item, feedback)
self.attributes = item.data(3)
self.aggregation_attributes = None # This should be set by the child class e.g. item.getIndicatorAttributes()
self.analysis_mode = self.attributes.get("Analysis Mode", "")
self.id = None # This should be set by the child class
self.layers = None # This should be set by the child class
Expand Down
13 changes: 9 additions & 4 deletions geest/core/workflows/analysis_aggregation_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from .aggregation_workflow_base import AggregationWorkflowBase
from geest.utilities import resources_path
from geest.gui.treeview import JsonTreeItem


class AnalysisAggregationWorkflow(AggregationWorkflowBase):
Expand All @@ -14,15 +15,19 @@ class AnalysisAggregationWorkflow(AggregationWorkflowBase):
It will aggregate the dimensions within an analysis to create a single raster output.
"""

def __init__(self, attributes: dict, feedback: QgsFeedback):
def __init__(self, item: JsonTreeItem, feedback: QgsFeedback):
"""
Initialize the Analysis Aggregation with attributes and feedback.
:param attributes: Dictionary containing workflow parameters.
⭐️ Item is a reference - whatever you change in this item will directly update the tree
:param JsonTreeItem: Treeview item containing workflow parameters.
:param feedback: QgsFeedback object for progress reporting and cancellation.
"""
super().__init__(attributes, feedback)
super().__init__(item, feedback)
self.id = "geest_analysis"
self.layers = self.attributes.get(f"Dimensions", [])
self.aggregation_attributes = self.item.getAnalysisAttributes()
self.layers = self.aggregation_attributes.get(f"Dimensions", [])
self.weight_key = "Dimension Weighting"
self.result_file_tag = "Analysis Result File"
self.vrt_path_key = "Dimension Result File"
Expand Down
15 changes: 10 additions & 5 deletions geest/core/workflows/dimension_aggregation_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from .aggregation_workflow_base import AggregationWorkflowBase
from geest.utilities import resources_path
from geest.gui.treeview import JsonTreeItem


class DimensionAggregationWorkflow(AggregationWorkflowBase):
Expand All @@ -15,15 +16,19 @@ class DimensionAggregationWorkflow(AggregationWorkflowBase):
"""

def __init__(self, attributes: dict, feedback: QgsFeedback):
def __init__(self, item: JsonTreeItem, feedback: QgsFeedback):
"""
Initialize the Dimension Aggregation with attributes and feedback.
:param attributes: Dictionary containing workflow parameters.
⭐️ Item is a reference - whatever you change in this item will directly update the tree
:param item: JsonTreeItem containing workflow parameters.
:param feedback: QgsFeedback object for progress reporting and cancellation.
"""
super().__init__(attributes, feedback)
self.id = self.attributes[f"Dimension ID"].lower().replace(" ", "_")
self.layers = self.attributes.get(f"Factors", [])
super().__init__(item, feedback)
self.aggregation_attributes = self.item.getDimensionAttributes()
self.id = self.aggregation_attributes[f"Dimension ID"].lower().replace(" ", "_")
self.layers = self.aggregation_attributes.get(f"Factors", [])
self.weight_key = "Factor Weighting"
self.result_file_tag = "Dimension Result File"
self.vrt_path_key = "Factor Result File"
Expand Down
21 changes: 13 additions & 8 deletions geest/core/workflows/factor_aggregation_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from qgis.core import (
QgsFeedback,
)
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from .aggregation_workflow_base import AggregationWorkflowBase
from geest.utilities import resources_path
from geest.gui.treeview import JsonTreeItem


class FactorAggregationWorkflow(AggregationWorkflowBase):
Expand All @@ -14,15 +14,20 @@ class FactorAggregationWorkflow(AggregationWorkflowBase):
It will aggregate the indicators within a factor to create a single raster output.
"""

def __init__(self, attributes: dict, feedback: QgsFeedback):
def __init__(self, item: dict, feedback: QgsFeedback):
"""
Initialize the Factor Aggregation with attributes and feedback.
:param attributes: Dictionary containing workflow parameters.
⭐️ Item is a reference - whatever you change in this item will directly update the tree
:param item: JsonTreeItem containing workflow parameters.
:param feedback: QgsFeedback object for progress reporting and cancellation.
"""
super().__init__(attributes, feedback)
self.id = self.attributes[f"Factor ID"].lower().replace(" ", "_")
self.layers = self.attributes.get(f"Indicators", [])
super().__init__(item, feedback)

self.aggregation_attributes = self.item.getFactorAttributes()
self.id = self.aggregation_attributes[f"Factor ID"].lower().replace(" ", "_")
self.layers = self.aggregation_attributes.get(f"Indicators", [])
self.weight_key = "Indicator Weighting"
self.result_file_tag = "Factor Result File"
self.vrt_path_key = "Indicator Result File"
Expand All @@ -40,8 +45,8 @@ def output_path(self, extension: str) -> str:
"""
directory = os.path.join(
self.workflow_directory,
self.attributes.get("Dimension ID").lower().replace(" ", "_"),
self.attributes.get("Factor ID").lower().replace(" ", "_"),
self.aggregation_attributes.get("Dimension ID").lower().replace(" ", "_"),
self.aggregation_attributes.get("Factor ID").lower().replace(" ", "_"),
)
# Create the directory if it doesn't exist
if not os.path.exists(directory):
Expand Down
13 changes: 11 additions & 2 deletions geest/gui/tree_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,14 @@ def show_attributes(self, item):

def add_to_map(self, item):
"""Add the item to the map."""
layer_uri = item.data(3).get(f"{item.topic.upper()} Result File")
# TODO refactor use of the term Layer everywhere to Indicator
# for now, some spaghetti code to get the layer_uri
if item.role == "layer":
layer_uri = item.data(3).get(f"Indicator Result File")
else:
layer_uri = item.data(3).get(
f"{item.role.title()} Result File"
) # title = title case string
layer_name = item.data(0)
QgsMessageLog.logMessage(
f"Adding {layer_uri} to the map.", tag="Geest", level=Qgis.Info
Expand Down Expand Up @@ -514,12 +521,14 @@ def queue_workflow_task(self, item, role):
if role == item.role and role == "layer":
task = self.queue_manager.add_task(item)
if role == item.role and role == "factor":
item.data(3)["Analysis Mode"] = "Factor Aggregation"
task = self.queue_manager.add_task(item)
if role == item.role and role == "dimension":
item.data(3)["Analysis Mode"] = "Dimension Aggregation"
task = self.queue_manager.add_task(item)
if role == item.role and role == "analysis":
item.data(3)["Analysis Mode"] = "Analysis Aggregation"
task = self.queue_manager.add_task(item)

if task is None:
return

Expand Down

0 comments on commit 08a4596

Please sign in to comment.