Skip to content

Commit 1c2e9da

Browse files
committed
feat(com6): add scarp algorithm
feat(com6scarp): Modifies input parameter to accept a DEM layer, coordinate layer, and perimeter layer feat(com6scarp): enhance scarp integration with automatic output handling - Updated to load scarp results (`asc`/`tif`) automatically into the context with styles applied.
1 parent 0ae301f commit 1c2e9da

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed

avaframeConnector_commonFunc.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,40 @@ def getDFAPathResults(targetDir):
173173

174174
return allDFAPathLayers
175175

176+
def getCom6ScarpResults(targetDir):
177+
"""Get results of com6 scarp analysis
178+
179+
Parameters
180+
-----------
181+
targetDir: pathlib path
182+
to avalanche directory
183+
Returns
184+
-------
185+
186+
"""
187+
from qgis.core import QgsRasterLayer
188+
189+
avaDir = pathlib.Path(str(targetDir))
190+
scarpResultsDir = avaDir / "Outputs" / "com6RockAvalanche" / "scarp"
191+
print("--------------")
192+
print(scarpResultsDir)
193+
194+
globbed = list(scarpResultsDir.glob("*.asc")) + list(scarpResultsDir.glob("*.tif"))
195+
scriptDir = pathlib.Path(__file__).parent
196+
qml = str(scriptDir / "QGisStyles" / "probMap.qml")
197+
198+
allRasterLayers = list()
199+
for item in globbed:
200+
rstLayer = QgsRasterLayer(str(item), item.stem)
201+
# try:
202+
# rstLayer.loadNamedStyle(qml)
203+
# except:
204+
# pass
205+
206+
allRasterLayers.append(rstLayer)
207+
208+
return allRasterLayers
209+
176210
def getAna4ProbAnaResults(targetDir):
177211
"""Get results of ana4PropAna
178212

avaframeConnector_provider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def find_python():
9393
from .runCom2AB_algorithm import runCom2ABAlgorithm
9494
from .runCom5SnowSlide_algorithm import runCom5SnowSlideAlgorithm
9595
from .runCom6RockAvalanche_algorithm import runCom6RockAvalancheAlgorithm
96+
from .runCom6Scarp_algorithm import runCom6ScarpAlgorithm
9697
from .runAna4ProbAna_algorithm import runAna4ProbAnaAlgorithm
9798
from .runAna4ProbDirOnly_algorithm import runAna4ProbDirOnlyAlgorithm
9899
from .runAna5DFAPathGeneration_algorithm import runAna5DFAPathGenerationAlgorithm
@@ -128,6 +129,7 @@ def loadAlgorithms(self):
128129
self.addAlgorithm(runCom2ABAlgorithm())
129130
self.addAlgorithm(runCom5SnowSlideAlgorithm())
130131
self.addAlgorithm(runCom6RockAvalancheAlgorithm())
132+
self.addAlgorithm(runCom6ScarpAlgorithm())
131133
self.addAlgorithm(runAna4ProbAnaAlgorithm())
132134
self.addAlgorithm(runAna4ProbDirOnlyAlgorithm())
133135
self.addAlgorithm(runAna5DFAPathGenerationAlgorithm())

pb_tool.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ python_files: __init__.py avaframeConnector.py avaframeConnector_provider.py
5757
runCom2AB_algorithm.py
5858
runCom5SnowSlide_algorithm.py
5959
runCom6RockAvalanche_algorithm.py
60+
runCom6Scarp_algorithm.py
6061
runAna4ProbAna_algorithm.py
6162
runAna4ProbDirOnly_algorithm.py
6263
runAna5DFAPathGeneration_algorithm.py

runCom6Scarp_algorithm.py

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
/***************************************************************************
5+
AvaFrameRunCom1DFA
6+
A QGIS plugin
7+
Connects to AvaFrame
8+
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
9+
-------------------
10+
begin : 2021-08-26
11+
copyright : (C) 2021 by AvaFrame Team
12+
email : felix@avaframe.org
13+
***************************************************************************/
14+
15+
/***************************************************************************
16+
* *
17+
* This program is free software; you can redistribute it and/or modify *
18+
* it under the terms of the GNU General Public License as published by *
19+
* the Free Software Foundation; either version 2 of the License, or *
20+
* (at your option) any later version. *
21+
* *
22+
***************************************************************************/
23+
"""
24+
25+
__author__ = "AvaFrame Team"
26+
__date__ = "2025"
27+
__copyright__ = "(C) 2025 by AvaFrame Team"
28+
29+
# This will get replaced with a git SHA1 when you do a git archive
30+
31+
__revision__ = "$Format:%H$"
32+
33+
from pathlib import Path
34+
35+
from qgis.PyQt.QtCore import QCoreApplication
36+
from qgis.core import (
37+
QgsProcessing,
38+
QgsRasterLayer,
39+
QgsProcessingException,
40+
QgsProcessingAlgorithm,
41+
QgsProcessingContext,
42+
QgsProcessingParameterFeatureSource,
43+
QgsProcessingParameterRasterLayer,
44+
QgsProcessingParameterEnum,
45+
QgsProcessingParameterMultipleLayers,
46+
QgsProcessingParameterFolderDestination,
47+
QgsProcessingOutputVectorLayer,
48+
QgsProcessingParameterDefinition,
49+
QgsProcessingOutputMultipleLayers,
50+
)
51+
52+
53+
class runCom6ScarpAlgorithm(QgsProcessingAlgorithm):
54+
"""
55+
This is the AvaFrame Connection, i.e. the part running with QGis. For this
56+
connector to work, more installation is needed. See instructions at docs.avaframe.org
57+
"""
58+
59+
DEM = "DEM"
60+
COORDINATES = "COORDINATES"
61+
PERIMETER = "PERIMETER"
62+
OUTPUT = "OUTPUT"
63+
FOLDEST = "FOLDEST"
64+
65+
def initAlgorithm(self, config):
66+
"""
67+
Here we define the inputs and output of the algorithm, along
68+
with some other properties.
69+
"""
70+
71+
self.addParameter(
72+
QgsProcessingParameterRasterLayer(self.DEM, self.tr("DEM layer"))
73+
)
74+
75+
self.addParameter(
76+
QgsProcessingParameterFeatureSource(
77+
self.COORDINATES,
78+
self.tr("Coordinate layer"),
79+
defaultValue="",
80+
types=[QgsProcessing.TypeVectorPoint],
81+
)
82+
)
83+
84+
self.addParameter(
85+
QgsProcessingParameterFeatureSource(
86+
self.PERIMETER,
87+
self.tr("Perimeter Layer"),
88+
defaultValue="",
89+
types=[QgsProcessing.TypeVectorAnyGeometry],
90+
)
91+
)
92+
93+
self.addParameter(
94+
QgsProcessingParameterFolderDestination(
95+
self.FOLDEST, self.tr("Destination folder")
96+
)
97+
)
98+
99+
self.addOutput(
100+
QgsProcessingOutputVectorLayer(
101+
self.OUTPUT,
102+
self.tr("Output layer"),
103+
QgsProcessing.TypeVectorAnyGeometry,
104+
)
105+
)
106+
107+
def flags(self):
108+
return super().flags()
109+
110+
def processAlgorithm(self, parameters, context, feedback):
111+
"""
112+
Here is where the processing itself takes place.
113+
"""
114+
115+
import avaframe.version as gv
116+
from . import avaframeConnector_commonFunc as cF
117+
118+
feedback.pushInfo("AvaFrame Version: " + gv.getVersion())
119+
120+
sourceDEM = self.parameterAsRasterLayer(parameters, self.DEM, context)
121+
if sourceDEM is None:
122+
raise QgsProcessingException(self.invalidSourceError(parameters, self.DEM))
123+
124+
sourcePerimeter = self.parameterAsVectorLayer(
125+
parameters, self.PERIMETER, context
126+
)
127+
128+
sourceCoordinates = self.parameterAsVectorLayer(
129+
parameters, self.COORDINATES, context
130+
)
131+
132+
sourceFOLDEST = self.parameterAsFile(parameters, self.FOLDEST, context)
133+
134+
finalTargetDir, targetDir = cF.createFolderStructure(sourceFOLDEST)
135+
136+
feedback.pushInfo(sourceDEM.source())
137+
138+
cF.copyDEM(sourceDEM, targetDir)
139+
140+
cF.copyShp(sourcePerimeter.source(), targetDir / "Inputs" / "POLYGONS", addToName="_perimeter")
141+
142+
cF.copyShp(sourceCoordinates.source(), targetDir / "Inputs" / "POINTS",addToName="_coordinates")
143+
144+
feedback.pushInfo('Starting the tool')
145+
feedback.pushInfo('This might take a while')
146+
feedback.pushInfo('See console for progress')
147+
#
148+
command = ['python', '-m', 'avaframe.runCom6Scarp', str(targetDir)]
149+
cF.runAndCheck(command, self, feedback)
150+
151+
feedback.pushInfo('Done, start loading the results')
152+
153+
cF.moveInputAndOutputFoldersToFinal(targetDir, finalTargetDir)
154+
155+
try:
156+
allRasterResults = cF.getCom6ScarpResults(finalTargetDir)
157+
except:
158+
raise QgsProcessingException(self.tr('Something went wrong with com6Scarp, please check log files'))
159+
160+
context = cF.addLayersToContext(context, allRasterResults, self.OUTPUT)
161+
162+
feedback.pushInfo("\n---------------------------------")
163+
feedback.pushInfo("Done, find results and logs here:")
164+
feedback.pushInfo(str(finalTargetDir.resolve()))
165+
feedback.pushInfo("---------------------------------\n")
166+
167+
return {self.OUTPUT: allRasterResults}
168+
# return
169+
170+
def name(self):
171+
return "com6scarp"
172+
173+
def displayName(self):
174+
return self.tr("Scarp (com6)")
175+
176+
def group(self):
177+
return self.tr(self.groupId())
178+
179+
def groupId(self):
180+
return "Experimental"
181+
182+
def tr(self, string):
183+
return QCoreApplication.translate("Processing", string)
184+
185+
def shortHelpString(self) -> str:
186+
hstring = "Runs scarp via module com6RockAvalanche. \n\
187+
For more information go to (or use the help button below): \n\
188+
AvaFrame Documentation: https://docs.avaframe.org\n\
189+
Homepage: https://avaframe.org\n\
190+
Praxisleitfaden: https://avaframe.org/reports\n"
191+
192+
return self.tr(hstring)
193+
194+
def helpUrl(self):
195+
return "https://docs.avaframe.org/en/latest/connector.html"
196+
197+
def createInstance(self):
198+
return runCom6ScarpAlgorithm()

0 commit comments

Comments
 (0)