|
| 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