File: doDEM.py

package info (click to toggle)
qgis 2.18.28%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,007,948 kB
  • sloc: cpp: 671,774; python: 158,539; xml: 35,690; ansic: 8,346; sh: 1,766; perl: 1,669; sql: 999; yacc: 836; lex: 461; makefile: 292
file content (179 lines) | stat: -rw-r--r-- 8,000 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-

"""
***************************************************************************
    doDEM.py
    ---------------------
    Date                 : March 2011
    Copyright            : (C) 2011 by Giuseppe Sucameli
    Email                : brush dot tyler 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__ = 'Giuseppe Sucameli'
__date__ = 'March 2011'
__copyright__ = '(C) 2011, Giuseppe Sucameli'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.PyQt.QtWidgets import QWidget

from .ui_widgetDEM import Ui_GdalToolsWidget as Ui_Widget
from .widgetPluginBase import GdalToolsBasePluginWidget as BasePluginWidget
from . import GdalTools_utils as Utils


class GdalToolsDialog(QWidget, Ui_Widget, BasePluginWidget):

    def __init__(self, iface):
        QWidget.__init__(self)
        self.iface = iface
        self.modes = ("hillshade", "slope", "aspect", "color-relief", "TRI", "TPI", "roughness")

        self.setupUi(self)
        BasePluginWidget.__init__(self, self.iface, "gdaldem")

        self.outSelector.setType(self.outSelector.FILE)
        self.configSelector.setType(self.configSelector.FILE)

        # set the default QSpinBoxes and QProgressBar value
        self.bandSpin.setValue(1)

        self.hillshadeZFactorSpin.setValue(1)
        self.hillshadeScaleSpin.setValue(1)
        self.hillshadeAltitudeSpin.setValue(45.0)
        self.hillshadeAzimuthSpin.setValue(315.0)
        self.slopeScaleSpin.setValue(1)

        # set the default color configuration file to terrain
        import os.path
        colorConfigFile = os.path.join(os.path.dirname(__file__), "terrain.txt")
        self.configSelector.setFilename(colorConfigFile)

        self.outputFormat = Utils.fillRasterOutputFormat()
        self.creationOptionsWidget.setFormat(self.outputFormat)

        self.setParamsStatus([
            (self.inSelector, "filenameChanged"),
            (self.outSelector, "filenameChanged"),
            (self.computeEdgesCheck, "stateChanged", None, 1800),
            (self.bandSpin, "valueChanged", self.bandCheck),
            (self.algorithmCheck, "stateChanged", None, 1800),
            (self.creationOptionsWidget, "optionsChanged"),
            (self.creationOptionsGroupBox, "toggled"),
            (self.modeCombo, "currentIndexChanged"),
            ([self.hillshadeZFactorSpin, self.hillshadeScaleSpin, self.hillshadeAltitudeSpin, self.hillshadeAzimuthSpin], "valueChanged"),
            (self.slopeScaleSpin, "valueChanged"),
            (self.slopePercentCheck, "stateChanged"),
            ([self.aspectTrigonometricCheck, self.aspectZeroForFlatCheck], "stateChanged"),
            (self.configSelector, "filenameChanged"),
            ([self.colorExactRadio, self.colorNearestRadio], "toggled", self.colorMatchGroupBox),
            (self.colorAlphaCheck, "stateChanged")
        ])

        self.inSelector.selectClicked.connect(self.fillInputFileEdit)
        self.outSelector.selectClicked.connect(self.fillOutputFileEdit)
        self.configSelector.selectClicked.connect(self.fillColorConfigFileEdit)
        self.modeCombo.currentIndexChanged.connect(self.showModeParams)

    def showModeParams(self, index):
        self.stackedWidget.setVisible(index < 4)
        self.algorithmCheck.setVisible(index < 3)
        if index >= 3:
            self.algorithmCheck.setChecked(False)

    def onLayersChanged(self):
        self.inSelector.setLayers(Utils.LayerRegistry.instance().getRasterLayers())

    def fillInputFileEdit(self):
        lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter()
        inputFile = Utils.FileDialog.getOpenFileName(self, self.tr("Select the file for DEM"), Utils.FileFilter.allRastersFilter(), lastUsedFilter)
        if not inputFile:
            return
        Utils.FileFilter.setLastUsedRasterFilter(lastUsedFilter)

        self.inSelector.setFilename(inputFile)
        self.getArguments()

    def fillOutputFileEdit(self):
        lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter()
        outputFile = Utils.FileDialog.getSaveFileName(self, self.tr("Select the raster file to save the results to"), Utils.FileFilter.saveRastersFilter(), lastUsedFilter)
        if not outputFile:
            return
        Utils.FileFilter.setLastUsedRasterFilter(lastUsedFilter)

        self.outputFormat = Utils.fillRasterOutputFormat(lastUsedFilter, outputFile)
        self.outSelector.setFilename(outputFile)
        self.creationOptionsWidget.setFormat(self.outputFormat)

    def fillColorConfigFileEdit(self):
        configFile = Utils.FileDialog.getOpenFileName(self, self.tr("Select the color configuration file"))
        if not configFile:
            return
        self.configSelector.setFilename(configFile)

    def getArguments(self):
        mode = self.modes[self.modeCombo.currentIndex()]
        arguments = []
        arguments.append(mode)
        arguments.append(self.getInputFileName())
        if mode == "color-relief":
            arguments.append(self.configSelector.filename())
        outputFn = self.getOutputFileName()
        arguments.append(outputFn)
        if mode == "hillshade":
            arguments.extend(["-z", self.hillshadeZFactorSpin.value()])
            arguments.extend(["-s", self.hillshadeScaleSpin.value()])
            arguments.extend(["-az", self.hillshadeAzimuthSpin.value()])
            arguments.extend(["-alt", self.hillshadeAltitudeSpin.value()])
        elif mode == "slope":
            if self.slopePercentCheck.isChecked():
                arguments.append("-p")
            arguments.extend(["-s", self.slopeScaleSpin.value()])
        elif mode == "aspect":
            if self.aspectTrigonometricCheck.isChecked():
                arguments.append("-trigonometric")
            if self.aspectZeroForFlatCheck.isChecked():
                arguments.append("-zero_for_flat")
        elif mode == "color-relief":
            if self.colorAlphaCheck.isChecked():
                arguments.append("-alpha")
            if self.colorMatchGroupBox.isChecked():
                if self.colorExactRadio.isChecked():
                    arguments.append("-exact_color_entry")
                elif self.colorNearestRadio.isChecked():
                    arguments.append("-nearest_color_entry")
        if self.algorithmCheck.isChecked():
            arguments.extend(["-alg", "ZevenbergenThorne"])
        if self.computeEdgesCheck.isChecked():
            arguments.append("-compute_edges")
        if self.bandCheck.isChecked():
            arguments.extend(["-b", self.bandSpin.value()])
        if outputFn:
            arguments.extend(["-of", self.outputFormat])
        if self.creationOptionsGroupBox.isChecked():
            for opt in self.creationOptionsWidget.options():
                arguments.extend(["-co", opt])
        # set creation options filename/layer for validation
        if self.inSelector.layer():
            self.creationOptionsWidget.setRasterLayer(self.inSelector.layer())
        else:
            self.creationOptionsWidget.setRasterFileName(self.getInputFileName())

        return arguments

    def getInputFileName(self):
        return self.inSelector.filename()

    def getOutputFileName(self):
        return self.outSelector.filename()

    def addLayerIntoCanvas(self, fileInfo):
        self.iface.addRasterLayer(fileInfo.filePath())