|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from PyQt4.QtCore import * |
| 3 | +from PyQt4.QtGui import * |
| 4 | +from qgis.core import * |
| 5 | +from qgis.gui import * |
| 6 | + |
| 7 | +from ui_widgetDEM import Ui_GdalToolsWidget as Ui_Widget |
| 8 | +from widgetPluginBase import GdalToolsBasePluginWidget as BasePluginWidget |
| 9 | +import GdalTools_utils as Utils |
| 10 | + |
| 11 | +class GdalToolsDialog(QWidget, Ui_Widget, BasePluginWidget): |
| 12 | + |
| 13 | + def __init__(self, iface): |
| 14 | + QWidget.__init__(self) |
| 15 | + self.iface = iface |
| 16 | + self.modes = ("hillshade", "slope", "aspect", "color-relief", "TRI", "TPI", "roughness") |
| 17 | + |
| 18 | + self.setupUi(self) |
| 19 | + BasePluginWidget.__init__(self, self.iface, "gdaldem") |
| 20 | + |
| 21 | + # set the default QSpinBoxes and QProgressBar value |
| 22 | + self.bandSpin.setValue(1) |
| 23 | + |
| 24 | + self.hillshadeZFactorSpin.setValue(1) |
| 25 | + self.hillshadeScaleSpin.setValue(1) |
| 26 | + self.hillshadeAltitudeSpin.setValue(45.0) |
| 27 | + self.hillshadeAzimuthSpin.setValue(315.0) |
| 28 | + self.slopeScaleSpin.setValue(1) |
| 29 | + |
| 30 | + self.outputFormat = Utils.fillRasterOutputFormat() |
| 31 | + |
| 32 | + self.setParamsStatus( |
| 33 | + [ |
| 34 | + (self.inputLayerCombo, [SIGNAL("currentIndexChanged(int)"), SIGNAL("editTextChanged(const QString &)")] ), |
| 35 | + (self.outputFileEdit, SIGNAL("textChanged(const QString &)")), |
| 36 | + (self.computeEdgesCheck, SIGNAL("stateChanged(int)"), None, "1.8.0"), |
| 37 | + (self.bandSpin, SIGNAL("valueChanged(int)"), self.bandCheck), |
| 38 | + (self.creationOptionsTable, [SIGNAL("cellValueChanged(int, int)"), SIGNAL("rowRemoved()")], self.creationGroupBox), |
| 39 | + (self.modeCombo, SIGNAL("currentIndexChanged(int)")), |
| 40 | + ([self.hillshadeZFactorSpin, self.hillshadeScaleSpin, self.hillshadeAltitudeSpin, self.hillshadeAzimuthSpin], SIGNAL("valueChanged(double)")), |
| 41 | + (self.slopeScaleSpin, SIGNAL("valueChanged(double)")), |
| 42 | + (self.slopePercentCheck, SIGNAL("stateChanged(int)")), |
| 43 | + ([self.aspectTrigonometricCheck, self.aspectZeroForFlatCheck], SIGNAL("stateChanged(int)")), |
| 44 | + (self.colorConfigFileEdit, SIGNAL("textChanged(const QString &)")), |
| 45 | + ([self.colorExactRadio, self.colorNearestRadio], SIGNAL("toggled(bool)"), self.colorMatchGroupBox), |
| 46 | + (self.colorAlphaCheck, SIGNAL("stateChanged(int)")) |
| 47 | + ] |
| 48 | + ) |
| 49 | + |
| 50 | + self.connect(self.selectInputFileButton, SIGNAL("clicked()"), self.fillInputFileEdit) |
| 51 | + self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit) |
| 52 | + self.connect(self.colorSelectConfigFileButton, SIGNAL("clicked()"), self.fillColorConfigFileEdit) |
| 53 | + self.connect(self.modeCombo, SIGNAL("currentIndexChanged(int)"), self.showModeParams) |
| 54 | + |
| 55 | + def showModeParams(self, index): |
| 56 | + self.stackedWidget.setVisible( index < 4 ) |
| 57 | + |
| 58 | + def onLayersChanged(self): |
| 59 | + self.fillInputLayerCombo() |
| 60 | + |
| 61 | + def fillInputLayerCombo(self): |
| 62 | + self.inputLayerCombo.clear() |
| 63 | + ( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers() |
| 64 | + self.inputLayerCombo.addItems( names ) |
| 65 | + |
| 66 | + def fillInputFileEdit(self): |
| 67 | + lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter() |
| 68 | + inputFile = Utils.FileDialog.getOpenFileName(self, self.tr( "Select the file for DEM" ), Utils.FileFilter.allRastersFilter(), lastUsedFilter) |
| 69 | + if inputFile.isEmpty(): |
| 70 | + return |
| 71 | + Utils.FileFilter.setLastUsedRasterFilter(lastUsedFilter) |
| 72 | + |
| 73 | + self.inputLayerCombo.setCurrentIndex(-1) |
| 74 | + self.inputLayerCombo.setEditText(inputFile) |
| 75 | + |
| 76 | + def fillOutputFileEdit(self): |
| 77 | + lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter() |
| 78 | + outputFile = Utils.FileDialog.getSaveFileName(self, self.tr( "Select the raster file to save the results to" ), Utils.FileFilter.allRastersFilter(), lastUsedFilter ) |
| 79 | + if outputFile.isEmpty(): |
| 80 | + return |
| 81 | + Utils.FileFilter.setLastUsedRasterFilter(lastUsedFilter) |
| 82 | + |
| 83 | + self.outputFormat = Utils.fillRasterOutputFormat( lastUsedFilter, outputFile ) |
| 84 | + self.outputFileEdit.setText(outputFile) |
| 85 | + |
| 86 | + def fillColorConfigFileEdit(self): |
| 87 | + configFile = Utils.FileDialog.getOpenFileName(self, self.tr( "Select the color configuration file" ), "*") |
| 88 | + if configFile.isEmpty(): |
| 89 | + return |
| 90 | + |
| 91 | + self.colorConfigFileEdit.setText(configFile) |
| 92 | + |
| 93 | + def getArguments(self): |
| 94 | + mode = self.modes[ self.modeCombo.currentIndex() ] |
| 95 | + arguments = QStringList() |
| 96 | + arguments << mode |
| 97 | + arguments << self.getInputFileName() |
| 98 | + if mode == "color-relief": |
| 99 | + arguments << self.colorConfigFileEdit.text() |
| 100 | + arguments << self.outputFileEdit.text() |
| 101 | + if mode == "hillshade": |
| 102 | + arguments << "-z" << str(self.hillshadeZFactorSpin.value()) |
| 103 | + arguments << "-s" << str(self.hillshadeScaleSpin.value()) |
| 104 | + arguments << "-az" << str(self.hillshadeAzimuthSpin.value()) |
| 105 | + arguments << "-alt" << str(self.hillshadeAltitudeSpin.value()) |
| 106 | + elif mode == "slope": |
| 107 | + if self.slopePercentCheck.isChecked(): |
| 108 | + arguments << "-p" |
| 109 | + arguments << "-s" << str(self.slopeScaleSpin.value()) |
| 110 | + elif mode == "aspect": |
| 111 | + if self.aspectTrigonometricCheck.isChecked(): |
| 112 | + arguments << "-trigonometric" |
| 113 | + if self.aspectZeroForFlatCheck.isChecked(): |
| 114 | + arguments << "-zero_for_flat" |
| 115 | + elif mode == "color-relief": |
| 116 | + if self.colorAlphaCheck.isChecked(): |
| 117 | + arguments << "-alpha" |
| 118 | + if self.colorMatchGroupBox.isChecked(): |
| 119 | + if self.colorExactRadio.isChecked(): |
| 120 | + arguments << "-exact_color_entry" |
| 121 | + elif self.colorNearestRadio.isChecked(): |
| 122 | + arguments << "-nearest_color_entry" |
| 123 | + if self.computeEdgesCheck.isChecked(): |
| 124 | + arguments << "-compute_edges" |
| 125 | + if self.bandCheck.isChecked(): |
| 126 | + arguments << "-b" << str(self.bandSpin.value()) |
| 127 | + if not self.outputFileEdit.text().isEmpty(): |
| 128 | + arguments << "-of" << self.outputFormat |
| 129 | + if self.creationGroupBox.isChecked(): |
| 130 | + for opt in self.creationOptionsTable.options(): |
| 131 | + arguments << "-co" << opt |
| 132 | + return arguments |
| 133 | + |
| 134 | + def getInputFileName(self): |
| 135 | + if self.inputLayerCombo.currentIndex() >= 0: |
| 136 | + return self.layers[self.inputLayerCombo.currentIndex()].source() |
| 137 | + return self.inputLayerCombo.currentText() |
| 138 | + |
| 139 | + def getOutputFileName(self): |
| 140 | + return self.outputFileEdit.text() |
| 141 | + |
| 142 | + def addLayerIntoCanvas(self, fileInfo): |
| 143 | + self.iface.addRasterLayer(fileInfo.filePath()) |
| 144 | + |
0 commit comments