|
| 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_widgetFillNodata import Ui_GdalToolsWidget as Ui_Widget |
| 8 | +from widgetBatchBase import GdalToolsBaseBatchWidget as BaseBatchWidget |
| 9 | +import GdalTools_utils as Utils |
| 10 | + |
| 11 | +import os.path |
| 12 | + |
| 13 | +class GdalToolsDialog( QWidget, Ui_Widget, BaseBatchWidget ): |
| 14 | + def __init__( self, iface ): |
| 15 | + QWidget.__init__( self ) |
| 16 | + self.iface = iface |
| 17 | + |
| 18 | + self.setupUi( self ) |
| 19 | + BaseBatchWidget.__init__( self, self.iface, "gdal_fillnodata.py" ) |
| 20 | + |
| 21 | + self.inSelector.setType( self.inSelector.FILE_LAYER ) |
| 22 | + self.outSelector.setType( self.outSelector.FILE ) |
| 23 | + self.maskSelector.setType( self.maskSelector.FILE ) |
| 24 | + |
| 25 | + self.progressBar.setValue(0) |
| 26 | + self.progressBar.hide() |
| 27 | + self.formatLabel.hide() |
| 28 | + self.formatCombo.hide() |
| 29 | + |
| 30 | + self.outputFormat = Utils.fillRasterOutputFormat() |
| 31 | + |
| 32 | + self.setParamsStatus( |
| 33 | + [ |
| 34 | + ( self.inSelector, SIGNAL( "filenameChanged()" ) ), |
| 35 | + ( self.outSelector, SIGNAL( "filenameChanged()" ) ), |
| 36 | + ( self.maskSelector, SIGNAL( "filenameChanged()" ), self.maskCheck ), |
| 37 | + ( self.distanceSpin, SIGNAL( "valueChanged( int )" ), self.distanceCheck ), |
| 38 | + ( self.smoothSpin, SIGNAL( "valueChanged( int )" ), self.smoothCheck ), |
| 39 | + ( self.bandSpin, SIGNAL( "valueChanged( int )" ), self.bandCheck ), |
| 40 | + ( self.nomaskCheck, SIGNAL( "stateChanged( int )" ) ) |
| 41 | + ] |
| 42 | + ) |
| 43 | + |
| 44 | + self.connect( self.inSelector, SIGNAL( "selectClicked()" ), self.fillInputFile ) |
| 45 | + self.connect( self.outSelector, SIGNAL( "selectClicked()" ), self.fillOutputFile) |
| 46 | + self.connect( self.maskSelector, SIGNAL( "selectClicked()" ), self.fillMaskFile) |
| 47 | + self.connect( self.batchCheck, SIGNAL( "stateChanged( int )" ), self.switchToolMode ) |
| 48 | + |
| 49 | + # add raster filters to combo |
| 50 | + self.formatCombo.addItems( Utils.FileFilter.allRastersFilter().split( ";;" ) ) |
| 51 | + |
| 52 | + |
| 53 | + def switchToolMode( self ): |
| 54 | + self.setCommandViewerEnabled( not self.batchCheck.isChecked() ) |
| 55 | + self.progressBar.setVisible( self.batchCheck.isChecked() ) |
| 56 | + self.formatLabel.setVisible( self.batchCheck.isChecked() ) |
| 57 | + self.formatCombo.setVisible( self.batchCheck.isChecked() ) |
| 58 | + |
| 59 | + self.inSelector.setType( self.inSelector.FILE if self.batchCheck.isChecked() else self.inSelector.FILE_LAYER ) |
| 60 | + self.outSelector.clear() |
| 61 | + |
| 62 | + if self.batchCheck.isChecked(): |
| 63 | + self.inFileLabel = self.label.text() |
| 64 | + self.outFileLabel = self.label_1.text() |
| 65 | + self.label.setText( QCoreApplication.translate( "GdalTools", "&Input directory" ) ) |
| 66 | + self.label_1.setText( QCoreApplication.translate( "GdalTools", "&Output directory" ) ) |
| 67 | + |
| 68 | + QObject.disconnect( self.inSelector, SIGNAL( "selectClicked()" ), self.fillInputFile ) |
| 69 | + QObject.disconnect( self.outSelector, SIGNAL( "selectClicked()" ), self.fillOutputFile ) |
| 70 | + |
| 71 | + QObject.connect( self.inSelector, SIGNAL( "selectClicked()" ), self. fillInputDir ) |
| 72 | + QObject.connect( self.outSelector, SIGNAL( "selectClicked()" ), self.fillOutputDir ) |
| 73 | + else: |
| 74 | + self.label.setText( self.inFileLabel ) |
| 75 | + self.label_1.setText( self.outFileLabel ) |
| 76 | + |
| 77 | + QObject.disconnect( self.inSelector, SIGNAL( "selectClicked()" ), self.fillInputDir ) |
| 78 | + QObject.disconnect( self.outSelector, SIGNAL( "selectClicked()" ), self.fillOutputDir ) |
| 79 | + |
| 80 | + QObject.connect( self.inSelector, SIGNAL( "selectClicked()" ), self.fillInputFile ) |
| 81 | + QObject.connect( self.outSelector, SIGNAL( "selectClicked()" ), self.fillOutputFile ) |
| 82 | + |
| 83 | + def fillInputFile( self ): |
| 84 | + lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter() |
| 85 | + inputFile = Utils.FileDialog.getOpenFileName( self, |
| 86 | + self.tr( "Select the files to analyse" ), |
| 87 | + Utils.FileFilter.allRastersFilter(), |
| 88 | + lastUsedFilter ) |
| 89 | + if inputFile.isEmpty(): |
| 90 | + return |
| 91 | + Utils.FileFilter.setLastUsedRasterFilter( lastUsedFilter ) |
| 92 | + self.inSelector.setFilename( inputFile ) |
| 93 | + |
| 94 | + def fillOutputFile( self ): |
| 95 | + lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter() |
| 96 | + outputFile = Utils.FileDialog.getSaveFileName( self, self.tr( "Select the raster file to save the results to" ), Utils.FileFilter.allRastersFilter(), lastUsedFilter ) |
| 97 | + if outputFile.isEmpty(): |
| 98 | + return |
| 99 | + Utils.FileFilter.setLastUsedRasterFilter( lastUsedFilter ) |
| 100 | + |
| 101 | + self.outputFormat = Utils.fillRasterOutputFormat( lastUsedFilter, outputFile ) |
| 102 | + self.outSelector.setFilename( outputFile ) |
| 103 | + |
| 104 | + def fillMaskFile( self ): |
| 105 | + lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter() |
| 106 | + inputFile = Utils.FileDialog.getOpenFileName( self, |
| 107 | + self.tr( "Select the files to analyse" ), |
| 108 | + Utils.FileFilter.allRastersFilter(), |
| 109 | + lastUsedFilter ) |
| 110 | + if inputFile.isEmpty(): |
| 111 | + return |
| 112 | + Utils.FileFilter.setLastUsedRasterFilter( lastUsedFilter ) |
| 113 | + self.maskSelector.setFilename( inputFile ) |
| 114 | + |
| 115 | + def fillInputDir( self ): |
| 116 | + inputDir = Utils.FileDialog.getExistingDirectory( self, self.tr( "Select the input directory with files" )) |
| 117 | + if inputDir.isEmpty(): |
| 118 | + return |
| 119 | + self.inSelector.setFilename( inputDir ) |
| 120 | + |
| 121 | + def fillOutputDir( self ): |
| 122 | + outputDir = Utils.FileDialog.getExistingDirectory( self, self.tr( "Select the output directory to save the results to" ) ) |
| 123 | + if outputDir.isEmpty(): |
| 124 | + return |
| 125 | + self.outSelector.setFilename( outputDir ) |
| 126 | + |
| 127 | + def getArguments(self): |
| 128 | + arguments = QStringList() |
| 129 | + maskFile = self.maskSelector.filename() |
| 130 | + if self.distanceCheck.isChecked() and self.distanceSpin.value() != 0: |
| 131 | + arguments << "-md" |
| 132 | + arguments << self.distanceSpin.text() |
| 133 | + if self.smoothCheck.isChecked() and self.smoothSpin.value() != 0: |
| 134 | + arguments << "-si" |
| 135 | + arguments << str( self.smoothSpin.value() ) |
| 136 | + if self.bandCheck.isChecked() and self.bandSpin.value() != 0: |
| 137 | + arguments << "-b" |
| 138 | + arguments << str( self.bandSpin.value() ) |
| 139 | + if self.maskCheck.isChecked() and not maskFile.isEmpty(): |
| 140 | + arguments << "-mask" |
| 141 | + arguments << maskFile |
| 142 | + if self.nomaskCheck.isChecked(): |
| 143 | + arguments << "-nomask" |
| 144 | + if self.isBatchEnabled(): |
| 145 | + if self.formatCombo.currentIndex() != 0: |
| 146 | + arguments << "-of" |
| 147 | + arguments << Utils.fillRasterOutputFormat( self.formatCombo.currentText() ) |
| 148 | + return arguments |
| 149 | + else: |
| 150 | + outputFn = self.getOutputFileName() |
| 151 | + if not outputFn.isEmpty(): |
| 152 | + arguments << "-of" |
| 153 | + arguments << self.outputFormat |
| 154 | + arguments << self.getInputFileName() |
| 155 | + arguments << outputFn |
| 156 | + return arguments |
| 157 | + |
| 158 | + def onLayersChanged( self ): |
| 159 | + self.inSelector.setLayers( Utils.LayerRegistry.instance().getRasterLayers() ) |
| 160 | + |
| 161 | + def getInputFileName(self): |
| 162 | + return self.inSelector.filename() |
| 163 | + |
| 164 | + def getOutputFileName(self): |
| 165 | + return self.outSelector.filename() |
| 166 | + |
| 167 | + def addLayerIntoCanvas(self, fileInfo): |
| 168 | + self.iface.addRasterLayer(fileInfo.filePath()) |
| 169 | + |
| 170 | + def isBatchEnabled(self): |
| 171 | + return self.batchCheck.isChecked() |
| 172 | + |
| 173 | + def setProgressRange(self, maximum): |
| 174 | + self.progressBar.setRange(0, maximum) |
| 175 | + |
| 176 | + def updateProgress(self, index, total): |
| 177 | + if index < total: |
| 178 | + self.progressBar.setValue( index + 1 ) |
| 179 | + else: |
| 180 | + self.progressBar.setValue( 0 ) |
| 181 | + |
| 182 | + def batchRun(self): |
| 183 | + exts = self.formatCombo.currentText().remove( QRegExp('^.*\(') ).remove( QRegExp('\).*$') ).split( " " ) |
| 184 | + if not exts.isEmpty() and exts != "*" and exts != "*.*": |
| 185 | + outExt = exts[ 0 ].remove( "*" ) |
| 186 | + else: |
| 187 | + outExt = ".tif" |
| 188 | + |
| 189 | + self.base.enableRun( False ) |
| 190 | + self.base.setCursor( Qt.WaitCursor ) |
| 191 | + |
| 192 | + inDir = self.getInputFileName() |
| 193 | + outDir = self.getOutputFileName() |
| 194 | + |
| 195 | + extensions = Utils.getRasterExtensions() |
| 196 | + workDir = QDir( inDir ) |
| 197 | + workDir.setFilter( QDir.Files | QDir.NoSymLinks | QDir.NoDotAndDotDot ) |
| 198 | + workDir.setNameFilters( extensions ) |
| 199 | + files = workDir.entryList() |
| 200 | + |
| 201 | + self.inFiles = [] |
| 202 | + self.outFiles = [] |
| 203 | + |
| 204 | + for f in files: |
| 205 | + self.inFiles.append( inDir + "/" + f ) |
| 206 | + if outDir != None: |
| 207 | + outFile = f.replace( QRegExp( "\.[a-zA-Z0-9]{2,4}" ), outExt ) |
| 208 | + self.outFiles.append( outDir + "/" + outFile ) |
| 209 | + |
| 210 | + self.errors = QStringList() |
| 211 | + self.batchIndex = 0 |
| 212 | + self.batchTotal = len( self.inFiles ) |
| 213 | + self.setProgressRange( self.batchTotal ) |
| 214 | + |
| 215 | + self.runItem( self.batchIndex, self.batchTotal ) |
0 commit comments