Skip to content

Commit

Permalink
Merge pull request #492 from Samweli/sieve_update
Browse files Browse the repository at this point in the history
Sieve update
  • Loading branch information
Samweli authored Jul 5, 2024
2 parents ebd626a + 5fd506d commit 26327ec
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 117 deletions.
170 changes: 148 additions & 22 deletions src/cplus_plugin/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ def run(self):
)

# TODO enable the sieve functionality
# sieve_enabled = self.get_settings_value(
# Settings.SIEVE_ENABLED, default=False, setting_type=bool
# )
#
# if sieve_enabled:
# self.run_activities_sieve(
# self.analysis_activities,
# )
sieve_enabled = self.get_settings_value(
Settings.SIEVE_ENABLED, default=False, setting_type=bool
)

if sieve_enabled:
self.run_activities_sieve(
self.analysis_activities,
)

# After creating activities, we normalize them using the same coefficients
# used in normalizing their respective pathways.
Expand Down Expand Up @@ -1447,30 +1447,156 @@ def run_activities_sieve(self, models, temporary_output=False):
)

# Actual processing calculation
alg_params = {
"INPUT": model.path,
"THRESHOLD": threshold_value,
"MASK_LAYER": mask_layer,
"OUTPUT": output,
}
# alg_params = {
# "INPUT": model.path,
# "THRESHOLD": threshold_value,
# "MASK_LAYER": mask_layer,
# "OUTPUT": output,
# }

self.log_message(
f"Used parameters for running sieve function to the models: {alg_params} \n"
input_name = os.path.splitext(os.path.basename(model.path))[0]

# Step 1: Create a binary mask from the original raster
binary_mask = processing.run(
"qgis:rastercalculator",
{
"CELLSIZE": 0,
"LAYERS": [model.path],
"CRS": None,
"EXPRESSION": f"{input_name}@1 > 0",
"OUTPUT": "TEMPORARY_OUTPUT",
},
)["OUTPUT"]

# feedback.pushInfo(f"binary mask {binary_mask}")

# binary_mask_layer = QgsRasterLayer(binary_mask, 'binary')

# QgsProject.instance().addMapLayer(binary_mask_layer)

# Step 2: Run sieve analysis from on the binary mask
sieved_mask = processing.run(
"gdal:sieve",
{
"INPUT": binary_mask,
"THRESHOLD": threshold_value,
"EIGHT_CONNECTEDNESS": True,
"NO_MASK": True,
"MASK_LAYER": None,
"OUTPUT": "TEMPORARY_OUTPUT",
},
context=self.processing_context,
feedback=self.feedback,
)["OUTPUT"]

# feedback.pushInfo(f"sieved mask {sieved_mask}")

# sieved_mask_layer = QgsRasterLayer(sieved_mask, 'sieved_mask')

# QgsProject.instance().addMapLayer(sieved_mask_layer)

expr = f"({os.path.splitext(os.path.basename(sieved_mask))[0]}@1 > 0) * {os.path.splitext(os.path.basename(sieved_mask))[0]}@1"
# feedback.pushInfo(f"used expression {expr}")

# Step 3: Remove and convert any no data value to 0
sieved_mask_clean = processing.run(
"qgis:rastercalculator",
{
"CELLSIZE": 0,
"LAYERS": [sieved_mask],
"CRS": None,
"EXPRESSION": expr,
"OUTPUT": "TEMPORARY_OUTPUT",
},
context=self.processing_context,
feedback=self.feedback,
)["OUTPUT"]

# feedback.pushInfo(f"sieved mask clean {sieved_mask_clean}")

# sieved_mask_clean_layer = QgsRasterLayer(sieved_mask_clean, 'sieved_mask_clean')

# QgsProject.instance().addMapLayer(sieved_mask_clean_layer)

expr_2 = f"{input_name}@1 * {os.path.splitext(os.path.basename(sieved_mask_clean))[0]}@1"

# feedback.pushInfo(f"Used expression 2 {expr_2}")

# Step 4: Join the sieved mask with the original input layer to filter out the small areas
sieve_output = processing.run(
"qgis:rastercalculator",
{
"CELLSIZE": 0,
"LAYERS": [model.path, sieved_mask_clean],
"CRS": None,
"EXPRESSION": expr_2,
"OUTPUT": "TEMPORARY_OUTPUT",
},
context=self.processing_context,
feedback=self.feedback,
)["OUTPUT"]

# feedback.pushInfo(f"sieved output joined {sieve_output}")

# sieve_output_layer = QgsRasterLayer(sieve_output, 'sieve_output')

# QgsProject.instance().addMapLayer(sieve_output_layer)

# expr_3 = f'if ( {os.path.splitext(os.path.basename(sieve_output))[0]}@1 <= 0, -9999, {os.path.splitext(os.path.basename(sieve_output))[0]}@1 )'

# feedback.pushInfo(f"used expression 3 {expr_3}")

# Step 5. Replace all 0 with -9999 using if ("combined@1" <= 0, -9999, "combined@1")
sieve_output_updated = processing.run(
"gdal:rastercalculator",
{
"INPUT_A": f"{sieve_output}",
"BAND_A": 1,
"FORMULA": "9999*(A<=0)*(-1)+A*(A>0)",
"NO_DATA": None,
"EXTENT_OPT": 0,
"PROJWIN": None,
"RTYPE": 5,
"OPTIONS": "",
"EXTRA": "",
"OUTPUT": "TEMPORARY_OUTPUT",
},
context=self.processing_context,
feedback=self.feedback,
)["OUTPUT"]

# feedback.pushInfo(f"sieved output updated {sieve_output_updated}")

# sieve_output_updated_layer = QgsRasterLayer(sieve_output_updated, 'sieve_output_updated')

# QgsProject.instance().addMapLayer(sieve_output_updated_layer)

# Step 6. Run sum statistics with ignore no data values set to false and no data value of -9999
results = processing.run(
"native:cellstatistics",
{
"INPUT": [sieve_output_updated],
"STATISTIC": 0,
"IGNORE_NODATA": False,
"REFERENCE_LAYER": sieve_output_updated,
"OUTPUT_NODATA_VALUE": -9999,
"OUTPUT": output,
},
context=self.processing_context,
feedback=self.feedback,
)

# self.log_message(
# f"Used parameters for running sieve function to the models: {alg_params} \n"
# )

feedback = QgsProcessingFeedback()

feedback.progressChanged.connect(self.update_progress)

if self.processing_cancelled:
return False

results = processing.run(
"gdal:sieve",
alg_params,
context=self.processing_context,
feedback=self.feedback,
)
model.path = results["OUTPUT"]

except Exception as e:
Expand Down
22 changes: 9 additions & 13 deletions src/cplus_plugin/ui/cplus_settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-172</y>
<width>557</width>
<height>875</height>
<y>0</y>
<width>558</width>
<height>732</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
Expand Down Expand Up @@ -157,13 +157,6 @@
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="reference_layer_label">
<property name="text">
<string>Reference layer</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
Expand Down Expand Up @@ -198,7 +191,7 @@
<item>
<widget class="QgsCollapsibleGroupBox" name="sieve_group_box">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="title">
<string>Sieve </string>
Expand All @@ -214,8 +207,11 @@
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="resample_method_2">
<property name="toolTip">
<string>Minimum number of connectect pixel that should be preserved when sieving.</string>
</property>
<property name="text">
<string>Pixel size</string>
<string>Threshold</string>
</property>
</widget>
</item>
Expand All @@ -236,7 +232,7 @@
<item>
<widget class="QgsFileWidget" name="mask_layer_widget">
<property name="toolTip">
<string>Select reference layer from the local filesystem.</string>
<string>Select reference layer to be used as mask layer for sieving.</string>
</property>
</widget>
</item>
Expand Down
Loading

0 comments on commit 26327ec

Please sign in to comment.