Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wee x pop layer to map from right click #730

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion geest/core/algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .area_iterator import AreaIterator
from .population_processor import PopulationRasterProcessingTask
from .wee_score_processor import WEEScoreProcessingTask
from .wee_score_processor import WEEByPopulationScoreProcessingTask
28 changes: 15 additions & 13 deletions geest/core/algorithms/wee_score_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
from geest.core.algorithms import AreaIterator


class WEEScoreProcessingTask(QgsTask):
class WEEByPopulationScoreProcessingTask(QgsTask):
"""
A QgsTask subclass for calculating WEE SCORE using raster algebra.
A QgsTask subclass for calculating WEE x Population SCORE using raster algebra.

It iterates over study areas, calculates the WEE SCORE using aligned input rasters
(GEEST and POP), combines the resulting rasters into a VRT, and applies a QML style.
(WEE and POP), combines the resulting rasters into a VRT, and applies a QML style.

Args:
study_area_gpkg_path (str): Path to the GeoPackage containing study area masks.
Expand Down Expand Up @@ -70,7 +70,7 @@ def run(self) -> bool:
Executes the WEE SCORE calculation task.
"""
try:
self.calculate_wee_score()
self.calculate_score()
self.generate_vrt()
return True
except Exception as e:
Expand Down Expand Up @@ -121,9 +121,9 @@ def validate_rasters(

log_message("Validation successful: rasters are aligned.")

def calculate_wee_score(self) -> None:
def calculate_score(self) -> None:
"""
Calculates WEE SCORE using raster algebra and saves the result for each area.
Calculates WEE by POP SCORE using raster algebra and saves the result for each area.
"""
area_iterator = AreaIterator(self.study_area_gpkg_path)
for index, (_, _, _, _) in enumerate(area_iterator):
Expand All @@ -136,15 +136,17 @@ def calculate_wee_score(self) -> None:
)
wee_layer = QgsRasterLayer(wee_path, "WEE")
pop_layer = QgsRasterLayer(population_path, "POP")
self.validate_rasters(wee_layer, pop_layer)
self.validate_rasters(wee_layer, pop_layer, dimension_check=False)

output_path = os.path.join(self.output_dir, f"wee_score_{index}.tif")
output_path = os.path.join(
self.output_dir, f"wee_by_population_score_{index}.tif"
)
if not self.force_clear and os.path.exists(output_path):
log_message(f"Reusing existing raster: {output_path}")
self.output_rasters.append(output_path)
continue

log_message(f"Calculating WEE SCORE for area {index}")
log_message(f"Calculating WEE by POP SCORE for area {index}")

params = {
"INPUT_A": wee_layer,
Expand All @@ -168,11 +170,11 @@ def calculate_wee_score(self) -> None:

def generate_vrt(self) -> None:
"""
Combines all WEE SCORE rasters into a single VRT and applies a QML style.
Combines all WEE SCORE rasters into a single VRT and ap plies a QML style.
"""
vrt_path = os.path.join(self.output_dir, "wee_score.vrt")
qml_path = os.path.join(self.output_dir, "wee_score.qml")
source_qml = resources_path("resources", "qml", "wee_score_style.qml")
vrt_path = os.path.join(self.output_dir, "wee_by_population_score.vrt")
qml_path = os.path.join(self.output_dir, "wee_by_population_score.qml")
source_qml = resources_path("resources", "qml", "wee_by_population_score.qml")

params = {
"INPUT": self.output_rasters,
Expand Down
12 changes: 10 additions & 2 deletions geest/core/workflows/analysis_aggregation_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from qgis.core import QgsFeedback, QgsProcessingContext
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from .aggregation_workflow_base import AggregationWorkflowBase
from geest.core.algorithms import PopulationRasterProcessingTask, WEEScoreProcessingTask
from geest.core.algorithms import (
PopulationRasterProcessingTask,
WEEByPopulationScoreProcessingTask,
)
from geest.utilities import resources_path
from geest.core import JsonTreeItem

Expand Down Expand Up @@ -55,9 +58,14 @@ def __init__(
feedback=self.feedback,
)
population_processor.run()
wee_processor = WEEScoreProcessingTask(
wee_processor = WEEByPopulationScoreProcessingTask(
study_area_gpkg_path=self.gpkg_path,
working_directory=self.working_directory,
force_clear=False,
)
wee_processor.run()
# Shamelessly hard coded for now, needs to move to the wee processor class
output = os.path.join(
self.working_directory, "wee_score", "wee_by_population_score.vrt"
)
item.setAttribute("wee_by_population", output)
16 changes: 12 additions & 4 deletions geest/gui/panels/tree_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ def update_action_text():
menu.addAction(clear_results_action)
menu.addAction(run_item_action)
menu.addAction(add_to_map_action)
add_wee_by_population = QAction("Add WEE by Pop to Map")
add_wee_by_population.triggered.connect(
lambda: self.add_to_map(
item, key="wee_by_population", layer_name="WEE by Population"
)
)
menu.addAction(add_wee_by_population)
add_study_area_layers_action = QAction("Add Study Area to Map", self)
add_study_area_layers_action.triggered.connect(self.add_study_area_to_map)
menu.addAction(add_study_area_layers_action)
Expand Down Expand Up @@ -870,13 +877,14 @@ def add_study_area_to_map(self):
f"Added layer: {layer.name()} to group: {geest_group.name()}"
)

def add_to_map(self, item):
def add_to_map(self, item, key="result_file", layer_name=None):
"""Add the item to the map."""

layer_uri = item.attribute(f"result_file")

layer_uri = item.attribute(f"{key}")
log_message(f"Adding {layer_uri} for key {key} to map")
if layer_uri:
layer_name = item.data(0)
if not layer_name:
layer_name = item.data(0)
layer = QgsRasterLayer(layer_uri, layer_name)

if not layer.isValid():
Expand Down
Binary file added test/test_data/points/fake_childcare.gpkg
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis styleCategories="AllStyleCategories" hasScaleBasedVisibilityFlag="0" minScale="1e+08" maxScale="0" version="3.38.3-Grenoble">
<flags>
<Identifiable>1</Identifiable>
<Removable>1</Removable>
<Searchable>1</Searchable>
<Private>0</Private>
</flags>
<temporal mode="0" fetchMode="0" enabled="0" bandNumber="1">
<fixedRange>
<start></start>
<end></end>
</fixedRange>
</temporal>
<elevation zscale="1" mode="RepresentsElevationSurface" symbology="Line" band="1" enabled="0" zoffset="0">
<data-defined-properties>
<Option type="Map">
<Option value="" type="QString" name="name"/>
<Option name="properties"/>
<Option value="collection" type="QString" name="type"/>
</Option>
</data-defined-properties>
<profileLineSymbol>
<symbol is_animated="0" alpha="1" frame_rate="10" force_rhr="0" type="line" name="" clip_to_extent="1">
<data_defined_properties>
<Option type="Map">
<Option value="" type="QString" name="name"/>
<Option name="properties"/>
<Option value="collection" type="QString" name="type"/>
</Option>
</data_defined_properties>
<layer pass="0" enabled="1" id="{bc455cb7-d46f-40c3-96bf-33d947aff219}" class="SimpleLine" locked="0">
<Option type="Map">
<Option value="0" type="QString" name="align_dash_pattern"/>
<Option value="square" type="QString" name="capstyle"/>
<Option value="5;2" type="QString" name="customdash"/>
<Option value="3x:0,0,0,0,0,0" type="QString" name="customdash_map_unit_scale"/>
<Option value="MM" type="QString" name="customdash_unit"/>
<Option value="0" type="QString" name="dash_pattern_offset"/>
<Option value="3x:0,0,0,0,0,0" type="QString" name="dash_pattern_offset_map_unit_scale"/>
<Option value="MM" type="QString" name="dash_pattern_offset_unit"/>
<Option value="0" type="QString" name="draw_inside_polygon"/>
<Option value="bevel" type="QString" name="joinstyle"/>
<Option value="196,60,57,255,rgb:0.7686274509803922,0.23529411764705882,0.22352941176470589,1" type="QString" name="line_color"/>
<Option value="solid" type="QString" name="line_style"/>
<Option value="0.6" type="QString" name="line_width"/>
<Option value="MM" type="QString" name="line_width_unit"/>
<Option value="0" type="QString" name="offset"/>
<Option value="3x:0,0,0,0,0,0" type="QString" name="offset_map_unit_scale"/>
<Option value="MM" type="QString" name="offset_unit"/>
<Option value="0" type="QString" name="ring_filter"/>
<Option value="0" type="QString" name="trim_distance_end"/>
<Option value="3x:0,0,0,0,0,0" type="QString" name="trim_distance_end_map_unit_scale"/>
<Option value="MM" type="QString" name="trim_distance_end_unit"/>
<Option value="0" type="QString" name="trim_distance_start"/>
<Option value="3x:0,0,0,0,0,0" type="QString" name="trim_distance_start_map_unit_scale"/>
<Option value="MM" type="QString" name="trim_distance_start_unit"/>
<Option value="0" type="QString" name="tweak_dash_pattern_on_corners"/>
<Option value="0" type="QString" name="use_custom_dash"/>
<Option value="3x:0,0,0,0,0,0" type="QString" name="width_map_unit_scale"/>
</Option>
<data_defined_properties>
<Option type="Map">
<Option value="" type="QString" name="name"/>
<Option name="properties"/>
<Option value="collection" type="QString" name="type"/>
</Option>
</data_defined_properties>
</layer>
</symbol>
</profileLineSymbol>
<profileFillSymbol>
<symbol is_animated="0" alpha="1" frame_rate="10" force_rhr="0" type="fill" name="" clip_to_extent="1">
<data_defined_properties>
<Option type="Map">
<Option value="" type="QString" name="name"/>
<Option name="properties"/>
<Option value="collection" type="QString" name="type"/>
</Option>
</data_defined_properties>
<layer pass="0" enabled="1" id="{eb329025-1bcb-405a-8116-6e8a42d0574c}" class="SimpleFill" locked="0">
<Option type="Map">
<Option value="3x:0,0,0,0,0,0" type="QString" name="border_width_map_unit_scale"/>
<Option value="196,60,57,255,rgb:0.7686274509803922,0.23529411764705882,0.22352941176470589,1" type="QString" name="color"/>
<Option value="bevel" type="QString" name="joinstyle"/>
<Option value="0,0" type="QString" name="offset"/>
<Option value="3x:0,0,0,0,0,0" type="QString" name="offset_map_unit_scale"/>
<Option value="MM" type="QString" name="offset_unit"/>
<Option value="35,35,35,255,rgb:0.13725490196078433,0.13725490196078433,0.13725490196078433,1" type="QString" name="outline_color"/>
<Option value="no" type="QString" name="outline_style"/>
<Option value="0.26" type="QString" name="outline_width"/>
<Option value="MM" type="QString" name="outline_width_unit"/>
<Option value="solid" type="QString" name="style"/>
</Option>
<data_defined_properties>
<Option type="Map">
<Option value="" type="QString" name="name"/>
<Option name="properties"/>
<Option value="collection" type="QString" name="type"/>
</Option>
</data_defined_properties>
</layer>
</symbol>
</profileFillSymbol>
</elevation>
<customproperties>
<Option type="Map">
<Option value="false" type="bool" name="WMSBackgroundLayer"/>
<Option value="false" type="bool" name="WMSPublishDataSourceUrl"/>
<Option value="0" type="int" name="embeddedWidgets/count"/>
<Option value="Value" type="QString" name="identify/format"/>
</Option>
</customproperties>
<mapTip enabled="1"></mapTip>
<pipe-data-defined-properties>
<Option type="Map">
<Option value="" type="QString" name="name"/>
<Option name="properties"/>
<Option value="collection" type="QString" name="type"/>
</Option>
</pipe-data-defined-properties>
<pipe>
<provider>
<resampling zoomedInResamplingMethod="nearestNeighbour" zoomedOutResamplingMethod="nearestNeighbour" enabled="false" maxOversampling="2"/>
</provider>
<rasterrenderer classificationMin="0" classificationMax="5" alphaBand="-1" opacity="1" type="singlebandpseudocolor" nodataColor="" band="1">
<rasterTransparency/>
<minMaxOrigin>
<limits>MinMax</limits>
<extent>WholeRaster</extent>
<statAccuracy>Estimated</statAccuracy>
<cumulativeCutLower>0.02</cumulativeCutLower>
<cumulativeCutUpper>0.98</cumulativeCutUpper>
<stdDevFactor>2</stdDevFactor>
</minMaxOrigin>
<rastershader>
<colorrampshader colorRampType="DISCRETE" classificationMode="2" clip="0" minimumValue="0" labelPrecision="4" maximumValue="5">
<colorramp type="gradient" name="[source]">
<Option type="Map">
<Option value="215,48,39,255,rgb:0.84313725490196079,0.18823529411764706,0.15294117647058825,1" type="QString" name="color1"/>
<Option value="69,117,180,255,rgb:0.27058823529411763,0.45882352941176469,0.70588235294117652,1" type="QString" name="color2"/>
<Option value="ccw" type="QString" name="direction"/>
<Option value="0" type="QString" name="discrete"/>
<Option value="gradient" type="QString" name="rampType"/>
<Option value="rgb" type="QString" name="spec"/>
<Option value="0.1;215,48,39,255,rgb:0.84313725490196079,0.18823529411764706,0.15294117647058825,1;rgb;ccw:0.3;252,141,89,255,rgb:0.9882352941176471,0.55294117647058827,0.34901960784313724,1;rgb;ccw:0.5;254,224,144,255,rgb:0.99607843137254903,0.8784313725490196,0.56470588235294117,1;rgb;ccw:0.7;224,243,248,255,rgb:0.8784313725490196,0.95294117647058818,0.97254901960784312,1;rgb;ccw:0.9;145,191,219,255,rgb:0.56862745098039214,0.74901960784313726,0.85882352941176465,1;rgb;ccw" type="QString" name="stops"/>
</Option>
</colorramp>
<item alpha="255" value="0.5" color="#d73027" label="&lt;= 0,5 Not Enabling"/>
<item alpha="255" value="1.5" color="#fc8d59" label="0,5 - 1,5 Very Low Enablement"/>
<item alpha="255" value="2.5" color="#fee090" label="1,5 - 2,5 Low Enablement"/>
<item alpha="255" value="3.5" color="#e0f3f8" label="2,5 - 3,5 Moderately Enabling"/>
<item alpha="255" value="4.5" color="#91bfdb" label="3,5 - 4,5 Enabling"/>
<item alpha="255" value="5" color="#4575b4" label="4,5 - 5,0 Highly Enabling"/>
<rampLegendSettings useContinuousLegend="1" maximumLabel="" direction="0" orientation="2" suffix="" prefix="" minimumLabel="">
<numericFormat id="basic">
<Option type="Map">
<Option type="invalid" name="decimal_separator"/>
<Option value="6" type="int" name="decimals"/>
<Option value="0" type="int" name="rounding_type"/>
<Option value="false" type="bool" name="show_plus"/>
<Option value="true" type="bool" name="show_thousand_separator"/>
<Option value="false" type="bool" name="show_trailing_zeros"/>
<Option type="invalid" name="thousand_separator"/>
</Option>
</numericFormat>
</rampLegendSettings>
</colorrampshader>
</rastershader>
</rasterrenderer>
<brightnesscontrast brightness="0" contrast="0" gamma="1"/>
<huesaturation colorizeOn="0" invertColors="0" saturation="0" grayscaleMode="0" colorizeRed="255" colorizeGreen="128" colorizeBlue="128" colorizeStrength="100"/>
<rasterresampler maxOversampling="2"/>
<resamplingStage>resamplingFilter</resamplingStage>
</pipe>
<blendMode>0</blendMode>
</qgis>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<VRTDataset rasterXSize="6" rasterYSize="6">
<SRS dataAxisToSRSAxisMapping="1,2">PROJCS["WGS 84 / UTM zone 20N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-63],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32620"]]</SRS>
<GeoTransform> 7.1400000000000000e+05, 1.0000000000000000e+03, 0.0000000000000000e+00, 1.5530000000000000e+06, 0.0000000000000000e+00, -1.0000000000000000e+03</GeoTransform>
<VRTRasterBand dataType="Float32" band="1">
<Metadata>
<MDI key="STATISTICS_MINIMUM">0</MDI>
<MDI key="STATISTICS_MAXIMUM">4.5</MDI>
<MDI key="STATISTICS_MEAN">2.0833333333333</MDI>
<MDI key="STATISTICS_STDDEV">1.497683396301</MDI>
<MDI key="STATISTICS_VALID_PERCENT">100</MDI>
</Metadata>
<NoDataValue>255</NoDataValue>
<ColorInterp>Gray</ColorInterp>
<ComplexSource resampling="nearest">
<SourceFilename relativeToVRT="1">accessibility_masked_0.tif</SourceFilename>
<SourceBand>1</SourceBand>
<SourceProperties RasterXSize="6" RasterYSize="6" DataType="Float32" BlockXSize="6" BlockYSize="6" />
<SrcRect xOff="0" yOff="0" xSize="6" ySize="6" />
<DstRect xOff="0" yOff="0" xSize="6" ySize="6" />
<NODATA>255</NODATA>
</ComplexSource>
</VRTRasterBand>
</VRTDataset>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<PAMDataset>
<PAMRasterBand band="1">
<Metadata>
<MDI key="STATISTICS_MAXIMUM">4.5</MDI>
<MDI key="STATISTICS_MEAN">2.0833333333333</MDI>
<MDI key="STATISTICS_MINIMUM">0</MDI>
<MDI key="STATISTICS_STDDEV">1.497683396301</MDI>
<MDI key="STATISTICS_VALID_PERCENT">100</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>
Loading
Loading