From 52b773fb140fbad7b2cf3f906a0ea2b513b3cb3b Mon Sep 17 00:00:00 2001 From: Nathan Woodrow Date: Tue, 4 Jun 2013 09:28:43 +1000 Subject: [PATCH] Patch from Matthias for gdal tools --- python/plugins/GdalTools/GdalTools.py | 4 +- .../GdalTools/tools/GdalTools_utils.py | 62 +++++++++---------- python/plugins/GdalTools/tools/dialogSRS.py | 2 +- python/plugins/GdalTools/tools/doContour.py | 2 +- python/plugins/GdalTools/tools/doGrid.py | 2 +- python/plugins/GdalTools/tools/doInfo.py | 6 +- .../plugins/GdalTools/tools/doProjection.py | 4 +- python/plugins/GdalTools/tools/doTranslate.py | 2 +- .../plugins/GdalTools/tools/inOutSelector.py | 14 ++--- .../GdalTools/tools/widgetBatchBase.py | 22 +++---- .../GdalTools/tools/widgetPluginBase.py | 3 +- 11 files changed, 61 insertions(+), 62 deletions(-) diff --git a/python/plugins/GdalTools/GdalTools.py b/python/plugins/GdalTools/GdalTools.py index 8bb70e99d549..61bb112d333a 100644 --- a/python/plugins/GdalTools/GdalTools.py +++ b/python/plugins/GdalTools/GdalTools.py @@ -71,11 +71,11 @@ def __init__( self, iface ): userPluginPath = QFileInfo( QgsApplication.qgisUserDbFilePath() ).path() + "/python/plugins/GdalTools" systemPluginPath = QgsApplication.prefixPath() + "/python/plugins/GdalTools" - overrideLocale = QSettings().value( "locale/overrideFlag", QVariant( False ) ).toBool() + overrideLocale = QSettings().value( "locale/overrideFlag", False, type=bool ) if not overrideLocale: localeFullName = QLocale.system().name() else: - localeFullName = QSettings().value( "locale/userLocale", QVariant( "" ) ).toString() + localeFullName = QSettings().value( "locale/userLocale", "" ) if QFileInfo( userPluginPath ).exists(): translationPath = userPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" diff --git a/python/plugins/GdalTools/tools/GdalTools_utils.py b/python/plugins/GdalTools/tools/GdalTools_utils.py index 4e9e8c9a32f3..3b863210c3e2 100644 --- a/python/plugins/GdalTools/tools/GdalTools_utils.py +++ b/python/plugins/GdalTools/tools/GdalTools_utils.py @@ -45,20 +45,20 @@ # Escapes arguments and return them joined in a string def escapeAndJoin(strList): - joined = QString() + joined = '' for s in strList: - if s.contains(" "): + if s.find(" ") is not -1: escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"' else: escaped = s joined += escaped + " " - return joined.trimmed() + return joined.strip() # Retrieves last used dir from persistent settings def getLastUsedDir(): settings = QSettings() - lastProjectDir = settings.value( "/UI/lastProjectDir", QVariant(".") ).toString() - return settings.value( "/GdalTools/lastUsedDir", QVariant(lastProjectDir) ).toString() + lastProjectDir = settings.value( "/UI/lastProjectDir", u"." ) + return settings.value( "/GdalTools/lastUsedDir", QVariant(lastProjectDir) ) # Stores last used dir in persistent settings def setLastUsedDir(filePath): @@ -73,7 +73,7 @@ def setLastUsedDir(filePath): # Retrieves GDAL binaries location def getGdalBinPath(): settings = QSettings() - return settings.value( "/GdalTools/gdalPath", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/gdalPath", u"" ) # Stores GDAL binaries location def setGdalBinPath( path ): @@ -83,7 +83,7 @@ def setGdalBinPath( path ): # Retrieves GDAL python modules location def getGdalPymodPath(): settings = QSettings() - return settings.value( "/GdalTools/gdalPymodPath", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/gdalPymodPath", u"" ) # Stores GDAL python modules location def setGdalPymodPath( path ): @@ -93,7 +93,7 @@ def setGdalPymodPath( path ): # Retrieves GDAL help files location def getHelpPath(): settings = QSettings() - return settings.value( "/GdalTools/helpPath", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/helpPath", u"" ) # Stores GDAL help files location def setHelpPath( path ): @@ -103,7 +103,7 @@ def setHelpPath( path ): # Retrieves last used encoding from persistent settings def getLastUsedEncoding(): settings = QSettings() - return settings.value( "/UI/encoding", QVariant("System") ).toString() + return settings.value( "/UI/encoding", u"System" ) # Stores last used encoding in persistent settings def setLastUsedEncoding(encoding): @@ -220,7 +220,7 @@ def getRasterFiles(path, recursive=False): return rasters def fillRasterOutputFormat(aFilter = None, filename = None): - shortName = QString() + shortName = '' if aFilter != None: supportedRasters = GdalConfig.getSupportedRasters() @@ -239,7 +239,7 @@ def fillRasterOutputFormat(aFilter = None, filename = None): return shortName def fillVectorOutputFormat(aFilter = None, filename = None): - shortName = QString() + shortName = '' if aFilter != None: supportedVectors = GdalConfig.getSupportedVectors() @@ -292,11 +292,11 @@ def getRasterSRS( parent, fileName ): processSRS.close() if arr.isEmpty(): - return QString() + return '' info = QString( arr ).split( "\n" ).filter( "AUTHORITY" ) if info.count() == 0: - return QString() + return '' srs = info[ info.count() - 1 ] srs = srs.simplified().remove( "AUTHORITY[" ) @@ -338,7 +338,7 @@ def getRasterExtent(parent, fileName): # so sometimes the dialog excedes the screen width class FileDialog: @classmethod - def getDialog(self, parent = None, caption = QString(), acceptMode = QFileDialog.AcceptOpen, fileMode = QFileDialog.ExistingFile, filter = QString(), selectedFilter = None, useEncoding = False): + def getDialog(self, parent = None, caption = '', acceptMode = QFileDialog.AcceptOpen, fileMode = QFileDialog.ExistingFile, filter = '', selectedFilter = None, useEncoding = False): if useEncoding: dialog = QgsEncodingFileDialog(parent, caption, getLastUsedDir(), filter, getLastUsedEncoding()) else: @@ -351,8 +351,8 @@ def getDialog(self, parent = None, caption = QString(), acceptMode = QFileDialog if not dialog.exec_(): if useEncoding: - return (QString(), None) - return QString() + return ('', None) + return '' # change the selected filter value if selectedFilter != None: @@ -390,26 +390,26 @@ def getDialog(self, parent = None, caption = QString(), acceptMode = QFileDialog return files @classmethod - def getOpenFileNames(self, parent = None, caption = QString(), filter = QString(), selectedFilter = None, useEncoding = False): + def getOpenFileNames(self, parent = None, caption = '', filter = '', selectedFilter = None, useEncoding = False): return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.ExistingFiles, filter, selectedFilter, useEncoding) @classmethod - def getOpenFileName(self, parent = None, caption = QString(), filter = QString(), selectedFilter = None, useEncoding = False): + def getOpenFileName(self, parent = None, caption = '', filter = '', selectedFilter = None, useEncoding = False): return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.ExistingFile, filter, selectedFilter, useEncoding) @classmethod - def getSaveFileName(self, parent = None, caption = QString(), filter = QString(), selectedFilter = None, useEncoding = False): + def getSaveFileName(self, parent = None, caption = '', filter = '', selectedFilter = None, useEncoding = False): return self.getDialog(parent, caption, QFileDialog.AcceptSave, QFileDialog.AnyFile, filter, selectedFilter, useEncoding) @classmethod - def getExistingDirectory(self, parent = None, caption = QString(), useEncoding = False): - return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.DirectoryOnly, QString(), None, useEncoding) + def getExistingDirectory(self, parent = None, caption = '', useEncoding = False): + return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.DirectoryOnly, '', None, useEncoding) class FileFilter: @classmethod def getFilter(self, typeName): settings = QSettings() - return settings.value( "/GdalTools/" + typeName + "FileFilter", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/" + typeName + "FileFilter", u"" ) @classmethod def setFilter(self, typeName, aFilter): @@ -417,7 +417,7 @@ def setFilter(self, typeName, aFilter): settings.setValue( "/GdalTools/" + typeName + "FileFilter", QVariant( aFilter ) ) # stores the supported raster file filter - rastersFilter = QString() + rastersFilter = '' # Retrieves the filter for supported raster files @classmethod @@ -429,7 +429,7 @@ def allRastersFilter(self): # separates multiple extensions that joined by a slash if QGis.QGIS_VERSION[0:3] < "1.5": formats = self.rastersFilter.split( ";;" ) - self.rastersFilter = QString() + self.rastersFilter = '' for f in formats: oldExts = QString(f).remove( QRegExp('^.*\(') ).remove( QRegExp('\).*$') ) newExts = QString(oldExts).replace( '/', ' *.' ) @@ -448,7 +448,7 @@ def setLastUsedRasterFilter(self, aFilter): self.setFilter("lastRaster", aFilter) # stores the supported vectors file filter - vectorsFilter = QString() + vectorsFilter = '' # Retrieves the filter for supported vector files @classmethod @@ -533,7 +533,7 @@ def getSupportedRasters(self): longName = QString(driver.LongName).remove( QRegExp( '\(.*$' ) ).trimmed() shortName = QString(driver.ShortName).remove( QRegExp( '\(.*$' ) ).trimmed() - extensions = QString() + extensions = '' description = QString(driver.GetDescription()) glob = QStringList() @@ -597,7 +597,7 @@ def getSupportedVectors(self): continue driverName = QString(driver.GetName()) - longName = QString() + longName = '' glob = QStringList() if driverName.startsWith( "AVCBin" ): @@ -711,7 +711,7 @@ class SupportedRasters: @classmethod def long2ShortName(self, longName): if longName.isEmpty(): - return QString() + return '' if self.dict_long2shortName.has_key(longName): return self.dict_long2shortName[longName] @@ -720,7 +720,7 @@ def long2ShortName(self, longName): if gdal.GetDriverCount() == 0: gdal.AllRegister() - shortName = QString() + shortName = '' # for each loaded GDAL driver for i in range(gdal.GetDriverCount()): @@ -740,9 +740,9 @@ def long2ShortName(self, longName): @classmethod def filename2ShortName(self, fileName): if fileName.isEmpty(): - return QString() + return '' - shortName = QString() + shortName = '' # for each raster format search for the file extension formats = FileFilter.allRastersFilter().split( ";;" ) diff --git a/python/plugins/GdalTools/tools/dialogSRS.py b/python/plugins/GdalTools/tools/dialogSRS.py index 7da043e5282a..330ec13fe44e 100644 --- a/python/plugins/GdalTools/tools/dialogSRS.py +++ b/python/plugins/GdalTools/tools/dialogSRS.py @@ -57,4 +57,4 @@ def getProjection(self): if not self.selector.selectedProj4String().isEmpty(): return self.proj4string() - return QString() + return '' diff --git a/python/plugins/GdalTools/tools/doContour.py b/python/plugins/GdalTools/tools/doContour.py index a0304957c83e..dca174bbae9c 100644 --- a/python/plugins/GdalTools/tools/doContour.py +++ b/python/plugins/GdalTools/tools/doContour.py @@ -98,7 +98,7 @@ def getArguments(self): arguments << self.attributeEdit.text() if True: # XXX in this moment the -i argument is not optional arguments << "-i" - arguments << QString(str(self.intervalDSpinBox.value())) + arguments << unicode(self.intervalDSpinBox.value()) arguments << self.getInputFileName() arguments << self.outSelector.filename() return arguments diff --git a/python/plugins/GdalTools/tools/doGrid.py b/python/plugins/GdalTools/tools/doGrid.py index 0e7712d823dc..60501c53a78d 100644 --- a/python/plugins/GdalTools/tools/doGrid.py +++ b/python/plugins/GdalTools/tools/doGrid.py @@ -198,7 +198,7 @@ def algorithmArguments(self, index): arguments.append("nodata=" + str(self.datametricsNoDataSpin.value())) return arguments.join(":") - def loadFields(self, vectorFile = QString()): + def loadFields(self, vectorFile = ''): self.zfieldCombo.clear() if vectorFile.isEmpty(): diff --git a/python/plugins/GdalTools/tools/doInfo.py b/python/plugins/GdalTools/tools/doInfo.py index 91cbb371fc91..48b9946f2cd3 100644 --- a/python/plugins/GdalTools/tools/doInfo.py +++ b/python/plugins/GdalTools/tools/doInfo.py @@ -66,7 +66,7 @@ def __init__( self, iface ): def doCopyLine( self ): - output = QString() + output = '' items = self.rasterInfoList.selectedItems() for r in items: output.append( r.text() + "\n" ) @@ -75,7 +75,7 @@ def doCopyLine( self ): clipboard.setText( output ) def doCopyAll( self ): - output = QString() + output = '' for r in range( self.rasterInfoList.count() ): output.append( self.rasterInfoList.item( r ).text() + "\n" ) if not output.isEmpty(): @@ -84,7 +84,7 @@ def doCopyAll( self ): def keyPressEvent( self, e ): if ( e.modifiers() == Qt.ControlModifier or e.modifiers() == Qt.MetaModifier ) and e.key() == Qt.Key_C: - items = QString() + items = '' for r in range( self.rasterInfoList.count() ): items.append( self.rasterInfoList.item( r ).text() + "\n" ) if not items.isEmpty(): diff --git a/python/plugins/GdalTools/tools/doProjection.py b/python/plugins/GdalTools/tools/doProjection.py index fbe1d84da86c..0604884a27eb 100644 --- a/python/plugins/GdalTools/tools/doProjection.py +++ b/python/plugins/GdalTools/tools/doProjection.py @@ -122,7 +122,7 @@ def getArguments( self ): inputFn = self.getInputFileName() arguments << inputFn - self.tempFile = QString( inputFn ) + self.tempFile = inputFn self.needOverwrite = False if not self.tempFile.isEmpty(): if self.tempFile.toLower().contains( QRegExp( "\.tif{1,2}" ) ): @@ -157,7 +157,7 @@ def getOutputFileName( self ): def getBatchOutputFileName(self, fn): # get GeoTiff - fn = QString( fn ).replace( QRegExp( "\.[a-zA-Z]{2,4}$" ), ".tif" ) + fn = re.sub( r'\.[a-zA-Z]{2,4}$', r'.tif', fn ) return BaseBatchWidget.getBatchOutputFileName( self, fn ) def addLayerIntoCanvas(self, fileInfo): diff --git a/python/plugins/GdalTools/tools/doTranslate.py b/python/plugins/GdalTools/tools/doTranslate.py index e871d918e04f..bec30e99a32a 100644 --- a/python/plugins/GdalTools/tools/doTranslate.py +++ b/python/plugins/GdalTools/tools/doTranslate.py @@ -145,7 +145,7 @@ def fillInputDir( self ): workDir.setNameFilters( filter ) # search for a valid SRS, then use it as default target SRS - srs = QString() + srs = '' for fname in workDir.entryList(): fl = inputDir + "/" + fname srs = Utils.getRasterSRS( self, fl ) diff --git a/python/plugins/GdalTools/tools/inOutSelector.py b/python/plugins/GdalTools/tools/inOutSelector.py index b094365fb6d4..fd4689c46c3c 100644 --- a/python/plugins/GdalTools/tools/inOutSelector.py +++ b/python/plugins/GdalTools/tools/inOutSelector.py @@ -132,8 +132,8 @@ def setFilename(self, fn=None): if isinstance(fn, QgsMapLayer): fn = fn.source() - elif isinstance(fn, str) or isinstance(fn, unicode) or isinstance(fn, QString): - fn = QString( fn ) + elif isinstance(fn, str) or isinstance(fn, unicode): + fn = unicode( fn ) elif hasattr(fn, '__iter__') or isinstance(fn, QStringList): if len( fn ) > 0: @@ -142,10 +142,10 @@ def setFilename(self, fn=None): self.filenames = fn fn = fn.join( "," ) else: - fn = QString() + fn = '' else: - fn = QString() + fn = '' if not (self.getType() & self.LAYER): self.fileEdit.setText( fn ) @@ -215,7 +215,7 @@ def clearComboState(self): def saveComboState(self): index = self.combo.currentIndex() text = self.combo.currentText() - layerID = self.combo.itemData(index).toString() if index >= 0 else "" + layerID = self.combo.itemData(index) if index >= 0 else "" self.prevState = ( index, text, layerID ) def restoreComboState(self): @@ -241,7 +241,7 @@ def restoreComboState(self): def layer(self): if self.getType() != self.FILE and self.combo.currentIndex() >= 0: - layerID = self.combo.itemData(self.combo.currentIndex()).toString() + layerID = self.combo.itemData(self.combo.currentIndex()) return QgsMapLayerRegistry.instance().mapLayer( layerID ) return None @@ -259,5 +259,5 @@ def filename(self): if layer != None: return layer.source() - return QString() + return '' diff --git a/python/plugins/GdalTools/tools/widgetBatchBase.py b/python/plugins/GdalTools/tools/widgetBatchBase.py index 0372f83cd47a..2b048e8fceee 100644 --- a/python/plugins/GdalTools/tools/widgetBatchBase.py +++ b/python/plugins/GdalTools/tools/widgetBatchBase.py @@ -37,9 +37,9 @@ def __init__(self, iface, commandName): BasePluginWidget.__init__(self, iface, commandName) def getBatchArguments(self, inFile, outFile = None): - arguments = QStringList() - arguments << self.getArguments() - arguments << inFile + arguments = [] + arguments.append( self.getArguments() ) + arguments.append( inFile ) if outFile != None: arguments << outFile return arguments @@ -62,9 +62,9 @@ def getBatchOutputFileName(self, fn): # if overwrites existent files if outDir == None or outDir == inDir: - return QString( fn ).append( ".tmp" ) + return fn + ".tmp" - return QString( fn ).mid( len(inDir) ).prepend( outDir ) + return outDir + fn[len(inDir):] def onRun( self ): if not self.isBatchEnabled(): @@ -85,7 +85,7 @@ def batchRun(self): for f in self.inFiles: self.outFiles.append( self.getBatchOutputFileName( f ) ) - self.errors = QStringList() + self.errors = [] self.batchIndex = 0 self.batchTotal = len( self.inFiles ) self.setProgressRange( self.batchTotal ) @@ -112,7 +112,7 @@ def onFinished(self, exitCode, status): BasePluginWidget.onFinished(self, exitCode, status) return - msg = QString.fromLocal8Bit( self.base.process.readAllStandardError() ) + msg = bytes.decode( bytes( self.base.process.readAllStandardError() ) ) if not msg.isEmpty(): self.errors.append( ">> " + self.inFiles[self.batchIndex] + "
" + msg.replace( "\n", "
" ) ) @@ -134,7 +134,7 @@ def batchFinished( self ): self.base.stop() if not self.errors.isEmpty(): - msg = QString( "Processing of the following files ended with error:

" ).append( self.errors.join( "

" ) ) + msg = u"Processing of the following files ended with error:

" + "

".join( self.errors ) QErrorMessage( self ).showMessage( msg ) inDir = self.getInputFileName() @@ -146,17 +146,17 @@ def batchFinished( self ): canvas = self.iface.mapCanvas() previousRenderFlag = canvas.renderFlag() canvas.setRenderFlag( False ) - notCreatedList = QStringList() + notCreatedList = [] for item in self.outFiles: fileInfo = QFileInfo( item ) if fileInfo.exists(): if self.base.loadCheckBox.isChecked(): self.addLayerIntoCanvas( fileInfo ) else: - notCreatedList << item + notCreatedList.append( item ) canvas.setRenderFlag( previousRenderFlag ) - if notCreatedList.isEmpty(): + if len( notCreatedList ) == 0: QMessageBox.information( self, self.tr( "Finished" ), self.tr( "Operation completed." ) ) else: QMessageBox.warning( self, self.tr( "Warning" ), self.tr( "The following files were not created: \n%1" ).arg( notCreatedList.join( ", " ) ) ) diff --git a/python/plugins/GdalTools/tools/widgetPluginBase.py b/python/plugins/GdalTools/tools/widgetPluginBase.py index f7dea74b60cf..d46aad3f61a8 100644 --- a/python/plugins/GdalTools/tools/widgetPluginBase.py +++ b/python/plugins/GdalTools/tools/widgetPluginBase.py @@ -106,8 +106,7 @@ def finished(self, load): if outFn == None: return - outFn = QString(outFn) - if outFn.isEmpty(): + if outFn == '': QMessageBox.warning(self, self.tr( "Warning" ), self.tr( "No output file created." ) ) return