forked from gioman/gioman_processing_circuitscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircuitscapeAlgorithm.py
89 lines (68 loc) · 3.16 KB
/
circuitscapeAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
***************************************************************************
CircuitscapeAlgorithm.py
---------------------
Date : June 2014
Copyright : (C) 2014-2018 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Alexander Bruy'
__date__ = 'June 2014'
__copyright__ = '(C) 2014-2018, Alexander Bruy'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.core import (QgsProcessingAlgorithm,
QgsProcessingUtils,
QgsProcessingParameterRasterLayer
)
pluginPath = os.path.dirname(__file__)
sessionExportedLayers = {}
class CircuitscapeAlgorithm(QgsProcessingAlgorithm):
def __init__(self):
super().__init__()
def createInstance(self):
return type(self)()
def icon(self):
return QIcon(os.path.join(pluginPath, "icons", "circuitscape.png"))
def tr(self, text):
return QCoreApplication.translate(self.__class__.__name__, text)
def exportRasterLayer(self, source):
global sessionExportedLayers
if source in sessionExportedLayers:
self.exportedLayers[source] = sessionExportedLayers[source]
return None
fileName = os.path.basename(source)
validChars = \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
fileName = ''.join(c for c in fileName if c in validChars)
if len(fileName) == 0:
fileName = 'layer'
destFilename = QgsProcessingUtils.generateTempFilename("{}.asc".format(fileName))
self.exportedLayers[source] = destFilename
sessionExportedLayers[source] = destFilename
return "gdal_translate -of AAIGrid {} {}".format(source, destFilename)
def prepareInputs(self, parameters, context):
commands = []
self.exportedLayers = {}
for param in self.parameterDefinitions():
if isinstance(param, QgsProcessingParameterRasterLayer):
layer = self.parameterAsRasterLayer(parameters, param.name(), context)
if layer is None:
continue
if not layer.source().lower().endswith("asc"):
exportCommand = self.exportRasterLayer(layer.source())
if exportCommand is not None:
commands.append(exportCommand)
return commands