Skip to content

Commit

Permalink
Backport changes to remove irmt:joinbylocationsummarystyle and apply …
Browse files Browse the repository at this point in the history
…styling after running qgis:joinbylocationsummary
  • Loading branch information
ptormene committed Oct 11, 2023
1 parent 1087b5e commit 845d1fc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 142 deletions.
20 changes: 12 additions & 8 deletions svir/calculations/aggregate_loss_by_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ def calculate_zonal_stats(callback, zonal_layer, points_layer, join_fields,
The full description of the algorithm can be obtained as follows:
processing.algorithmHelp('qgis:joinbylocationsummary') and it includes
the lists of predicates and summaries.
The code of the algorithm is here:
https://github.com/qgis/QGIS/blob
/483b4ff977e3d36b166fac792254c31e89e3aeae/python/plugins/processing/algs
/qgis/SpatialJoinSummary.py # NOQA
:param callback: function to be called once the aggregation is complete,
passing the output zonal layer as a parameter
Expand All @@ -92,11 +88,19 @@ def calculate_zonal_stats(callback, zonal_layer, points_layer, join_fields,
processing.Processing.initialize()
alg = QgsApplication.processingRegistry().algorithmById(
'qgis:joinbylocationsummary')
# make sure to use the actual lists of predicates and summaries as defined
# in the algorithm when it is instantiated
predicate_keys = [predicate[0] for predicate in alg.predicates]
if alg is None:
raise ImportError('Unable to retrieve processing algorithm'
' qgis:joinbylocationsummary')
# NOTE: predicates are no more retrieavable in the c++ version of the
# algorithm, so we can't make sure to use the actual lists of predicates
# and summaries as defined in the algorithm when it is instantiated
predicate_keys = ['intersects', 'contains', 'isEqual', 'touches',
'overlaps', 'within', 'crosses']
PREDICATES = dict(zip(predicate_keys, range(len(predicate_keys))))
summary_keys = [statistic[0] for statistic in alg.statistics]
summary_keys = [
'count', 'unique', 'min', 'max', 'range', 'sum', 'mean', 'median',
'stddev', 'minority', 'majority', 'q1', 'q3', 'iqr', 'empty', 'filled',
'min_length', 'max_length', 'mean_length']
SUMMARIES = dict(zip(summary_keys, range(len(summary_keys))))

context = QgsProcessingContext()
Expand Down
32 changes: 27 additions & 5 deletions svir/irmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
QgsApplication,
QgsWkbTypes,
)
from qgis.utils import iface

from qgis.PyQt.QtCore import (
QSettings,
Expand All @@ -55,7 +56,8 @@
QUrl,
Qt,
)
from qgis.PyQt.QtWidgets import QAction, QFileDialog, QApplication, QMenu
from qgis.PyQt.QtWidgets import (
QAction, QFileDialog, QApplication, QMenu, QInputDialog)
from qgis.PyQt.QtGui import QIcon, QDesktopServices, QColor

from svir.dialogs.viewer_dock import ViewerDock
Expand All @@ -74,6 +76,7 @@
from svir.dialogs.taxonomy_dialog import TaxonomyDialog
from svir.dialogs.drive_oq_engine_server_dialog import (
DriveOqEngineServerDialog)
from svir.dialogs.load_output_as_layer_dialog import LoadOutputAsLayerDialog

from svir.thread_worker.abstract_worker import start_worker
from svir.thread_worker.download_platform_data_worker import (
Expand Down Expand Up @@ -368,12 +371,20 @@ def recovery_settings(self):

def aggregate(self):
processing.Processing.initialize()
alg_id = 'irmt:joinbylocationsummarystyle'
alg_id = 'qgis:joinbylocationsummary'
alg = QgsApplication.processingRegistry().algorithmById(alg_id)
# make sure to use the actual lists of predicates and summaries as
# defined in the algorithm when it is instantiated
predicate_keys = [predicate[0] for predicate in alg.predicates]
# NOTE: predicates are no more retrieavable in the c++ version of the
# algorithm, so we can't make sure to use the actual lists of
# predicates and summaries as defined in the algorithm when it is
# instantiated
predicate_keys = ['intersects', 'contains', 'isEqual', 'touches',
'overlaps', 'within', 'crosses']
PREDICATES = dict(zip(predicate_keys, range(len(predicate_keys))))
summary_keys = [
'count', 'unique', 'min', 'max', 'range', 'sum', 'mean', 'median',
'stddev', 'minority', 'majority', 'q1', 'q3', 'iqr', 'empty',
'filled', 'min_length', 'max_length', 'mean_length']
SUMMARIES = dict(zip(summary_keys, range(len(summary_keys))))
default_predicates = ['intersects']
summary_keys = [statistic[0] for statistic in alg.statistics]
SUMMARIES = dict(zip(summary_keys, range(len(summary_keys))))
Expand All @@ -400,6 +411,17 @@ def aggregate(self):
res = processing.execAlgorithmDialog(alg_id, initial_params)
if 'OUTPUT' in res:
processed_layer = res['OUTPUT']
added_fieldnames = [
fieldname for fieldname in processed_layer.fields().names()
if fieldname not in zonal_layer.fields().names()]
if len(added_fieldnames) > 1:
style_by = QInputDialog.getItem(
iface.mainWindow(), "Style output by", "Field",
added_fieldnames, editable=False)[0]
else:
style_by = added_fieldnames[0]
LoadOutputAsLayerDialog.style_maps(
processed_layer, style_by, iface)
QgsProject.instance().addMapLayer(processed_layer)
self.iface.setActiveLayer(processed_layer)
self.iface.zoomToActiveLayer()
Expand Down
4 changes: 0 additions & 4 deletions svir/processing_provider/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
from svir.processing_provider.log10_alg import Log10Algorithm
from svir.processing_provider.simple_quadratic_alg import (
SimpleQuadraticAlgorithm)
from svir.processing_provider.spatial_join_summary_style import (
SpatialJoinSummaryStyle)


class Provider(QgsProcessingProvider):
Expand All @@ -46,8 +44,6 @@ def loadAlgorithms(self, *args, **kwargs):
self.addAlgorithm(SimpleQuadraticAlgorithm())
self.addAlgorithm(ZScoreAlgorithm())
self.addAlgorithm(Log10Algorithm())
# Zonal aggregation
self.addAlgorithm(SpatialJoinSummaryStyle())

def id(self, *args, **kwargs):
"""The ID of your plugin, used for identifying the provider.
Expand Down
125 changes: 0 additions & 125 deletions svir/processing_provider/spatial_join_summary_style.py

This file was deleted.

0 comments on commit 845d1fc

Please sign in to comment.