Skip to content

Commit

Permalink
Merge pull request #34 from kartoza/feat-output-selection-detail
Browse files Browse the repository at this point in the history
update scenario detail with selection outputs field and default values
  • Loading branch information
danangmassandy authored May 23, 2024
2 parents 890c96a + 3baf7ea commit cd154dc
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 35 deletions.
78 changes: 64 additions & 14 deletions django_project/cplus_api/serializers/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from core.models.task_log import TaskLog
from cplus_api.models.layer import BaseLayer, InputLayer
from cplus_api.models.scenario import ScenarioTask
from cplus_api.utils.default import DEFAULT_VALUES


def validate_layer_uuid(value):
Expand Down Expand Up @@ -220,17 +221,24 @@ class Meta:
class ScenarioInputSerializer(serializers.Serializer):
scenario_name = serializers.CharField(required=True)
scenario_desc = serializers.CharField(required=True)
snapping_enabled = serializers.BooleanField(required=False)
snapping_enabled = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.snapping_enabled)
snap_layer = serializers.CharField(required=False, allow_blank=True)
snap_layer_uuid = serializers.CharField(
required=False, validators=[validate_layer_uuid], allow_blank=True
)
pathway_suitability_index = serializers.IntegerField(required=False)
carbon_coefficient = serializers.FloatField(required=False)
snap_rescale = serializers.BooleanField(required=False)
snap_method = serializers.IntegerField(required=False)
sieve_enabled = serializers.BooleanField(required=False)
sieve_threshold = serializers.FloatField(required=False)
pathway_suitability_index = serializers.IntegerField(
required=False, default=DEFAULT_VALUES.pathway_suitability_index)
carbon_coefficient = serializers.FloatField(
required=False, default=DEFAULT_VALUES.carbon_coefficient)
snap_rescale = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.snap_rescale)
snap_method = serializers.IntegerField(
required=False, default=DEFAULT_VALUES.snap_method)
sieve_enabled = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.sieve_enabled)
sieve_threshold = serializers.FloatField(
required=False, default=DEFAULT_VALUES.sieve_threshold)
sieve_mask_path = serializers.CharField(required=False, allow_blank=True)
sieve_mask_uuid = serializers.CharField(
required=False, validators=[validate_layer_uuid], allow_blank=True
Expand All @@ -251,6 +259,16 @@ class ScenarioInputSerializer(serializers.Serializer):
priority_layers = PriorityLayerSerializer(many=True)
priority_layer_groups = PriorityGroupSerializer(many=True)
activities = ActivitySerializer(many=True)
ncs_with_carbon = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.ncs_with_carbon)
landuse_project = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.landuse_project)
landuse_normalized = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.landuse_normalized)
landuse_weighted = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.landuse_weighted)
highest_position = serializers.BooleanField(
required=False, default=DEFAULT_VALUES.highest_position)

class Meta:
swagger_schema_fields = {
Expand All @@ -267,7 +285,8 @@ class Meta:
),
'snapping_enabled': openapi.Schema(
title='Is snapping enabled',
type=openapi.TYPE_BOOLEAN
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.snapping_enabled
),
'snap_layer': openapi.Schema(
title='Snap layer Path',
Expand All @@ -279,27 +298,33 @@ class Meta:
),
'pathway_suitability_index': openapi.Schema(
title='Pathway suitability index',
type=openapi.TYPE_INTEGER
type=openapi.TYPE_INTEGER,
default=DEFAULT_VALUES.pathway_suitability_index
),
'carbon_coefficient': openapi.Schema(
title='Carbon coefficient',
type=openapi.TYPE_NUMBER
type=openapi.TYPE_NUMBER,
default=DEFAULT_VALUES.carbon_coefficient
),
'snap_rescale': openapi.Schema(
title='Is snap rescale',
type=openapi.TYPE_BOOLEAN
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.snap_rescale
),
'snap_method': openapi.Schema(
title='Snap method',
type=openapi.TYPE_INTEGER
type=openapi.TYPE_INTEGER,
default=DEFAULT_VALUES.snap_method
),
'sieve_enabled': openapi.Schema(
title='Is sieve function enabled',
type=openapi.TYPE_BOOLEAN
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.sieve_enabled
),
'sieve_threshold': openapi.Schema(
title='Sieve function threshold',
type=openapi.TYPE_NUMBER
type=openapi.TYPE_NUMBER,
default=DEFAULT_VALUES.sieve_threshold
),
'sieve_mask_path': openapi.Schema(
title='Sieve mask layer path',
Expand All @@ -309,6 +334,31 @@ class Meta:
title='Sieve mask layer UUID',
type=openapi.TYPE_STRING
),
'ncs_with_carbon': openapi.Schema(
title='Enable output NCS with carbon',
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.ncs_with_carbon
),
'landuse_project': openapi.Schema(
title='Enable output Landuse Activity',
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.landuse_project
),
'landuse_normalized': openapi.Schema(
title='Enable output Landuse Activity Normalized',
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.landuse_normalized
),
'landuse_weighted': openapi.Schema(
title='Enable output Landuse Weighted with PWL',
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.landuse_weighted
),
'highest_position': openapi.Schema(
title='Enable output Scenario Highest Position analysis',
type=openapi.TYPE_BOOLEAN,
default=DEFAULT_VALUES.highest_position
),
'mask_path': openapi.Schema(
title='Mask layer path',
type=openapi.TYPE_STRING
Expand Down
4 changes: 3 additions & 1 deletion django_project/cplus_api/tasks/remove_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def remove_layers():

# Remove private data that is more 2 weeks
output_group_to_keep = SitePreferences.preferences().output_group_to_keep
output_layers = OutputLayer.objects.exclude(
output_layers = OutputLayer.objects.filter(
created_on__lt=last_14_days_datetime
).exclude(
Q(is_final_output=True) | Q(group__in=output_group_to_keep)
)
results[OutputLayer] = output_layers.count()
Expand Down
14 changes: 14 additions & 0 deletions django_project/cplus_api/utils/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

class DEFAULT_VALUES(object):
snapping_enabled = False
pathway_suitability_index = 0
carbon_coefficient = 0.0
snap_rescale = False
snap_method = 0
sieve_enabled = False
sieve_threshold = 10.0
ncs_with_carbon = False
landuse_project = True
landuse_normalized = True
landuse_weighted = True
highest_position = True
84 changes: 64 additions & 20 deletions django_project/cplus_api/utils/worker_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from cplus_api.models.scenario import ScenarioTask
from cplus_api.models.layer import BaseLayer, OutputLayer, InputLayer
from cplus_api.utils.api_helper import convert_size, todict, CustomJsonEncoder
from cplus_api.utils.default import DEFAULT_VALUES

logger = logging.getLogger(__name__)

Expand All @@ -34,15 +35,15 @@ class TaskConfig(object):
priority_layers: typing.List = []
priority_layer_groups: typing.List = []
analysis_extent: SpatialExtent = None
snapping_enabled: bool = False
snapping_enabled: bool = DEFAULT_VALUES.snapping_enabled
snap_layer = ''
snap_layer_uuid = ''
pathway_suitability_index = 0
carbon_coefficient = 0.0
snap_rescale = False
snap_method = 0
sieve_enabled = False
sieve_threshold = 10.0
pathway_suitability_index = DEFAULT_VALUES.pathway_suitability_index
carbon_coefficient = DEFAULT_VALUES.carbon_coefficient
snap_rescale = DEFAULT_VALUES.snap_rescale
snap_method = DEFAULT_VALUES.snap_method
sieve_enabled = DEFAULT_VALUES.sieve_enabled
sieve_threshold = DEFAULT_VALUES.sieve_threshold
sieve_mask_uuid = ''
mask_path = ''
mask_layers_paths = ''
Expand All @@ -52,16 +53,31 @@ class TaskConfig(object):
carbon_uuid_layers = {}
priority_uuid_layers = {}
total_input_layers = 0
# output selections
ncs_with_carbon = DEFAULT_VALUES.ncs_with_carbon
landuse_project = DEFAULT_VALUES.landuse_project
landuse_normalized = DEFAULT_VALUES.landuse_normalized
landuse_weighted = DEFAULT_VALUES.landuse_weighted
highest_position = DEFAULT_VALUES.highest_position

def __init__(self, scenario_name, scenario_desc, extent,
analysis_activities, priority_layers,
priority_layer_groups,
snapping_enabled=False, snap_layer_uuid='',
pathway_suitability_index=0,
carbon_coefficient=0.0, snap_rescale=False,
snap_method=0, sieve_enabled=False,
sieve_threshold=10.0, sieve_mask_uuid='',
mask_layer_uuids='', scenario_uuid=None) -> None:
pathway_suitability_index=
DEFAULT_VALUES.pathway_suitability_index,
carbon_coefficient=DEFAULT_VALUES.carbon_coefficient,
snap_rescale=DEFAULT_VALUES.snap_rescale,
snap_method=DEFAULT_VALUES.snap_method,
sieve_enabled=DEFAULT_VALUES.sieve_enabled,
sieve_threshold=DEFAULT_VALUES.sieve_threshold,
sieve_mask_uuid='',
mask_layer_uuids='', scenario_uuid=None,
ncs_with_carbon=DEFAULT_VALUES.ncs_with_carbon,
landuse_project=DEFAULT_VALUES.landuse_project,
landuse_normalized=DEFAULT_VALUES.landuse_normalized,
landuse_weighted=DEFAULT_VALUES.landuse_weighted,
highest_position=DEFAULT_VALUES.highest_position) -> None:
self.scenario_name = scenario_name
self.scenario_desc = scenario_desc
if scenario_uuid:
Expand Down Expand Up @@ -89,6 +105,12 @@ def __init__(self, scenario_name, scenario_desc, extent,
weighted_activities=[],
priority_layer_groups=self.priority_layer_groups
)
# output selections
self.ncs_with_carbon = ncs_with_carbon
self.landuse_project = landuse_project
self.landuse_normalized = landuse_normalized
self.landuse_weighted = landuse_weighted
self.highest_position = highest_position

def get_activity(
self, activity_uuid: str
Expand Down Expand Up @@ -137,7 +159,12 @@ def to_dict(self):
'pathway_uuid_layers': self.pathway_uuid_layers,
'carbon_uuid_layers': self.carbon_uuid_layers,
'priority_uuid_layers': self.priority_uuid_layers,
'total_input_layers': self.total_input_layers
'total_input_layers': self.total_input_layers,
'ncs_with_carbon': self.ncs_with_carbon,
'landuse_project': self.landuse_project,
'landuse_normalized': self.landuse_normalized,
'landuse_weighted': self.landuse_weighted,
'highest_position': self.highest_position
}
for activity in self.analysis_activities:
activity_dict = {
Expand Down Expand Up @@ -171,17 +198,34 @@ def from_dict(cls, data: dict) -> typing.Self:
)
config.priority_layers = data.get('priority_layers', [])
config.priority_layer_groups = data.get('priority_layer_groups', [])
config.snapping_enabled = data.get('snapping_enabled', False)
config.snapping_enabled = data.get(
'snapping_enabled', DEFAULT_VALUES.snapping_enabled)
config.snap_layer_uuid = data.get('snap_layer_uuid', '')
config.pathway_suitability_index = data.get(
'pathway_suitability_index', 0)
config.carbon_coefficient = data.get('carbon_coefficient', 0.0)
config.snap_rescale = data.get('snap_rescale', False)
config.snap_method = data.get('snap_method', 0)
config.sieve_enabled = data.get('sieve_enabled', False)
config.sieve_threshold = data.get('sieve_threshold', 10.0)
'pathway_suitability_index',
DEFAULT_VALUES.pathway_suitability_index)
config.carbon_coefficient = data.get(
'carbon_coefficient', DEFAULT_VALUES.carbon_coefficient)
config.snap_rescale = data.get(
'snap_rescale', DEFAULT_VALUES.snap_rescale)
config.snap_method = data.get(
'snap_method', DEFAULT_VALUES.snap_method)
config.sieve_enabled = data.get(
'sieve_enabled', DEFAULT_VALUES.sieve_enabled)
config.sieve_threshold = data.get(
'sieve_threshold', DEFAULT_VALUES.sieve_threshold)
config.sieve_mask_uuid = data.get('sieve_mask_uuid', '')
config.mask_layer_uuids = data.get('mask_layer_uuids', '')
config.ncs_with_carbon = data.get(
'ncs_with_carbon', DEFAULT_VALUES.ncs_with_carbon)
config.landuse_project = data.get(
'landuse_project', DEFAULT_VALUES.landuse_project)
config.landuse_normalized = data.get(
'landuse_normalized', DEFAULT_VALUES.landuse_normalized)
config.landuse_weighted = data.get(
'landuse_weighted', DEFAULT_VALUES.landuse_weighted)
config.highest_position = data.get(
'highest_position', DEFAULT_VALUES.highest_position)
# store dict of <layer_uuid, list of obj identifier>
config.priority_uuid_layers = {}
config.pathway_uuid_layers = {}
Expand Down

0 comments on commit cd154dc

Please sign in to comment.