Skip to content

Commit

Permalink
Merge pull request #412 from Samweli/api_update
Browse files Browse the repository at this point in the history
API documentation update
  • Loading branch information
Samweli authored May 14, 2024
2 parents 7ba8922 + 0a8fd7a commit a0a626d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 69 deletions.
119 changes: 56 additions & 63 deletions src/cplus_plugin/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@

from .models.helpers import clone_activity

from .models.base import ScenarioResult, SpatialExtent
from .models.base import ScenarioResult

from .utils import (
align_rasters,
clean_filename,
open_documentation,
tr,
log,
FileUtils,
Expand Down Expand Up @@ -172,7 +171,6 @@ def run(self):
):
self.snap_analysis_data(
self.analysis_activities,
self.analysis_priority_layers_groups,
extent_string,
)

Expand All @@ -186,7 +184,6 @@ def run(self):

self.run_pathways_analysis(
self.analysis_activities,
self.analysis_priority_layers_groups,
extent_string,
temporary_output=not save_output,
)
Expand All @@ -196,7 +193,6 @@ def run(self):

self.run_pathways_normalization(
self.analysis_activities,
self.analysis_priority_layers_groups,
extent_string,
)

Expand All @@ -208,7 +204,6 @@ def run(self):

self.run_activities_analysis(
self.analysis_activities,
self.analysis_priority_layers_groups,
extent_string,
temporary_output=not save_output,
)
Expand Down Expand Up @@ -238,8 +233,6 @@ def run(self):
if sieve_enabled:
self.run_activities_sieve(
self.analysis_activities,
self.analysis_priority_layers_groups,
extent_string,
)

# After creating activities, we normalize them using the same coefficients
Expand All @@ -251,7 +244,6 @@ def run(self):

self.run_activities_normalization(
self.analysis_activities,
self.analysis_priority_layers_groups,
extent_string,
temporary_output=not save_output,
)
Expand Down Expand Up @@ -379,7 +371,7 @@ def replace_nodata(self, layer_path, output_path, nodata_value):
:param nodata_value: Nodata value to be used
:type output_path: int
:returns: If the process was successful
:returns: Whether the task operations was successful
:rtype: bool
"""
Expand Down Expand Up @@ -429,29 +421,27 @@ def replace_nodata(self, layer_path, output_path, nodata_value):

return outputs is not None

def run_pathways_analysis(
self, activities, priority_layers_groups, extent, temporary_output=False
):
def run_pathways_analysis(self, activities, extent, temporary_output=False):
"""Runs the required activity pathways analysis on the passed
activities. The analysis involves adding the pathways
carbon layers into the pathway layer.
carbon layers into their respective pathway layers.
If the pathway layer has more than one carbon layer, the resulting
If a pathway layer has more than one carbon layer, the resulting
weighted pathway will contain the sum of the pathway layer values
with the average of the pathway carbon layers values.
:param activities: List of the selected activities
:type activities: typing.List[Activity]
:param priority_layers_groups: Used priority layers groups and their values
:type priority_layers_groups: dict
:param extent: The selected extent from user
:type extent: str
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""
if self.processing_cancelled:
return False
Expand Down Expand Up @@ -487,9 +477,7 @@ def run_pathways_analysis(
activities_paths.append(activity.path)

if not pathways and len(activities_paths) > 0:
self.run_pathways_normalization(
activities, priority_layers_groups, extent
)
self.run_pathways_normalization(activities, extent)
return

suitability_index = float(
Expand Down Expand Up @@ -551,9 +539,7 @@ def run_pathways_analysis(
expression = " + ".join(basenames)

if carbon_coefficient <= 0 and suitability_index <= 0:
self.run_pathways_normalization(
activities, priority_layers_groups, extent
)
self.run_pathways_normalization(activities, extent)
return

output = (
Expand Down Expand Up @@ -597,17 +583,14 @@ def run_pathways_analysis(

return True

def snap_analysis_data(self, activities, priority_layers_groups, extent):
def snap_analysis_data(self, activities, extent):
"""Snaps the passed activities pathways, carbon layers and priority layers
to align with the reference layer set on the settings
manager.
:param activities: List of the selected activities
:type activities: typing.List[Activity]
:param priority_layers_groups: Used priority layers groups and their values
:type priority_layers_groups: dict
:param extent: The selected extent from user
:type extent: list
"""
Expand Down Expand Up @@ -850,9 +833,7 @@ def snap_layer(

return output_path

def run_pathways_normalization(
self, activities, priority_layers_groups, extent, temporary_output=False
):
def run_pathways_normalization(self, activities, extent, temporary_output=False):
"""Runs the normalization on the activities pathways layers,
adjusting band values measured on different scale, the resulting scale
is computed using the below formula
Expand All @@ -867,15 +848,15 @@ def run_pathways_normalization(
:param activities: List of the analyzed activities
:type activities: typing.List[Activity]
:param priority_layers_groups: Used priority layers groups and their values
:type priority_layers_groups: dict
:param extent: selected extent from user
:type extent: str
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""
if self.processing_cancelled:
# Will not proceed if processing has been cancelled by the user
Expand Down Expand Up @@ -913,7 +894,7 @@ def run_pathways_normalization(
activities_paths.append(activity.path)

if not pathways and len(activities_paths) > 0:
self.run_activities_analysis(activities, priority_layers_groups, extent)
self.run_activities_analysis(activities, extent)

return

Expand Down Expand Up @@ -1023,25 +1004,23 @@ def run_pathways_normalization(

return True

def run_activities_analysis(
self, activities, priority_layers_groups, extent, temporary_output=False
):
def run_activities_analysis(self, activities, extent, temporary_output=False):
"""Runs the required activity analysis on the passed
activities.
activities pathways. The analysis is responsible for creating activities
layers from their respective pathways layers.
:param activities: List of the selected activities
:type activities: typing.List[Activity]
:param priority_layers_groups: Used priority layers
groups and their values
:type priority_layers_groups: dict
:param extent: selected extent from user
:type extent: str
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""
if self.processing_cancelled:
# Will not proceed if processing has been cancelled by the user
Expand Down Expand Up @@ -1148,6 +1127,9 @@ def run_activities_masking(
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""
if self.processing_cancelled:
# Will not proceed if processing has been cancelled by the user
Expand All @@ -1157,7 +1139,7 @@ def run_activities_masking(

try:
if len(masking_layers) < 1:
return None
return False

if len(masking_layers) > 1:
mask_layer = self.merge_vector_layers(masking_layers)
Expand Down Expand Up @@ -1267,8 +1249,8 @@ def merge_vector_layers(self, layers):
:param layers: List of the vector layers paths
:type layers: typing.List[str]
:return merged_layer: Merged vector layer
:rtype merged_layer: QgsMapLayer
:return: Merged vector layer
:rtype: QgsMapLayer
"""

input_map_layers = []
Expand Down Expand Up @@ -1304,9 +1286,7 @@ def merge_vector_layers(self, layers):

return results["OUTPUT"]

def run_activities_sieve(
self, models, priority_layers_groups, extent, temporary_output=False
):
def run_activities_sieve(self, models, temporary_output=False):
"""Runs the sieve functionality analysis on the passed models layers,
removing the models layer polygons that are smaller than the provided
threshold size (in pixels) and replaces them with the pixel value of
Expand All @@ -1315,15 +1295,12 @@ def run_activities_sieve(
:param models: List of the analyzed implementation models
:type models: typing.List[ImplementationModel]
:param priority_layers_groups: Used priority layers groups and their values
:type priority_layers_groups: dict
:param extent: Selected area of interest extent
:type extent: str
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""
if self.processing_cancelled:
# Will not proceed if processing has been cancelled by the user
Expand Down Expand Up @@ -1415,9 +1392,7 @@ def run_activities_sieve(

return True

def run_activities_normalization(
self, activities, priority_layers_groups, extent, temporary_output=False
):
def run_activities_normalization(self, activities, extent, temporary_output=False):
"""Runs the normalization analysis on the activities' layers,
adjusting band values measured on different scale, the resulting scale
is computed using the below formula
Expand All @@ -1432,15 +1407,15 @@ def run_activities_normalization(
:param activities: List of the analyzed activities
:type activities: typing.List[Activity]
:param priority_layers_groups: Used priority layers groups and their values
:type priority_layers_groups: dict
:param extent: Selected area of interest extent
:type extent: str
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""
if self.processing_cancelled:
# Will not proceed if processing has been cancelled by the user
Expand Down Expand Up @@ -1571,7 +1546,7 @@ def run_activities_weighting(
self, activities, priority_layers_groups, extent, temporary_output=False
):
"""Runs weighting analysis on the passed activities using
the corresponding activities weighting analysis.
the corresponding activities weight layers.
:param activities: List of the selected activities
:type activities: typing.List[Activity]
Expand All @@ -1585,6 +1560,10 @@ def run_activities_weighting(
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: A tuple with the weighted activities outputs and
a value of whether the task operations was successful
:rtype: typing.Tuple[typing.List, bool]
"""

if self.processing_cancelled:
Expand Down Expand Up @@ -1746,6 +1725,13 @@ def run_activities_cleaning(self, activities, extent=None, temporary_output=Fals
:param extent: Selected extent from user
:type extent: str
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""

if self.processing_cancelled:
Expand Down Expand Up @@ -1832,10 +1818,17 @@ def run_highest_position_analysis(self, temporary_output=False):
in scenario analysis. Uses the activities set by the current ongoing
analysis.
:param temporary_output: Whether to save the processing outputs as temporary
files
:type temporary_output: bool
:returns: Whether the task operations was successful
:rtype: bool
"""
if self.processing_cancelled:
# Will not proceed if processing has been cancelled by the user
return
return False

passed_extent_box = self.analysis_extent.bbox
passed_extent = QgsRectangle(
Expand Down
Loading

0 comments on commit a0a626d

Please sign in to comment.