2010-06-04 00:31:48 +00:00
# -*- coding: utf-8 -*-
2012-10-06 13:10:25 +02:00
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
doTranslate . py
- - - - - - - - - - - - - - - - - - - - -
Date : June 2010
Copyright : ( C ) 2010 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__ = ' June 2010 '
__copyright__ = ' (C) 2010, Giuseppe Sucameli '
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = ' $Format: % H$ '
2010-06-04 00:31:48 +00:00
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
from qgis . core import *
from qgis . gui import *
from ui_widgetTranslate import Ui_GdalToolsWidget as Ui_Widget
from widgetBatchBase import GdalToolsBaseBatchWidget as BaseBatchWidget
from dialogSRS import GdalToolsSRSDialog as SRSDialog
import GdalTools_utils as Utils
2013-06-15 22:44:11 +02:00
import re
2010-06-04 00:31:48 +00:00
class GdalToolsDialog ( QWidget , Ui_Widget , BaseBatchWidget ) :
def __init__ ( self , iface ) :
QWidget . __init__ ( self )
self . iface = iface
self . canvas = self . iface . mapCanvas ( )
2011-01-25 18:55:59 +00:00
self . expand_method = ( ' gray ' , ' rgb ' , ' rgba ' )
2010-06-04 00:31:48 +00:00
self . setupUi ( self )
BaseBatchWidget . __init__ ( self , self . iface , " gdal_translate " )
2011-04-15 18:30:10 +00:00
self . outSelector . setType ( self . outSelector . FILE )
2010-06-04 00:31:48 +00:00
# set the default QSpinBoxes and QProgressBar value
2010-08-06 16:43:14 +00:00
self . outsizeSpin . setValue ( 25 )
2010-06-04 00:31:48 +00:00
self . progressBar . setValue ( 0 )
self . progressBar . hide ( )
self . formatLabel . hide ( )
self . formatCombo . hide ( )
2010-08-06 16:43:14 +00:00
if Utils . Version ( Utils . GdalConfig . version ( ) ) < " 1.7 " :
index = self . expandCombo . findText ( ' gray ' , Qt . MatchFixedString )
if index > = 0 :
self . expandCombo . removeItem ( index )
2010-06-04 00:31:48 +00:00
self . outputFormat = Utils . fillRasterOutputFormat ( )
self . setParamsStatus (
[
2011-04-15 18:30:10 +00:00
( self . inSelector , SIGNAL ( " filenameChanged() " ) ) ,
( self . outSelector , SIGNAL ( " filenameChanged() " ) ) ,
2010-06-04 00:31:48 +00:00
( self . targetSRSEdit , SIGNAL ( " textChanged(const QString &) " ) , self . targetSRSCheck ) ,
( self . selectTargetSRSButton , None , self . targetSRSCheck ) ,
2013-01-18 18:20:51 -02:00
( self . creationOptionsWidget , SIGNAL ( " optionsChanged() " ) ) ,
2010-08-06 16:43:14 +00:00
( self . outsizeSpin , SIGNAL ( " valueChanged(const QString &) " ) , self . outsizeCheck ) ,
( self . nodataSpin , SIGNAL ( " valueChanged(int) " ) , self . nodataCheck ) ,
2013-01-18 18:20:51 -02:00
( self . expandCombo , SIGNAL ( " currentIndexChanged(int) " ) , self . expandCheck , 1600 ) ,
2010-08-06 16:43:14 +00:00
( self . sdsCheck , SIGNAL ( " stateChanged(int) " ) ) ,
( self . srcwinEdit , SIGNAL ( " textChanged(const QString &) " ) , self . srcwinCheck ) ,
( self . prjwinEdit , SIGNAL ( " textChanged(const QString &) " ) , self . prjwinCheck )
2010-06-04 00:31:48 +00:00
]
)
#self.connect(self.canvas, SIGNAL("layersChanged()"), self.fillInputLayerCombo)
2011-04-15 18:30:10 +00:00
self . connect ( self . inSelector , SIGNAL ( " layerChanged() " ) , self . fillTargetSRSEditDefault )
self . connect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputFile )
self . connect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputFileEdit )
2010-06-04 00:31:48 +00:00
self . connect ( self . selectTargetSRSButton , SIGNAL ( " clicked() " ) , self . fillTargetSRSEdit )
self . connect ( self . batchCheck , SIGNAL ( " stateChanged( int ) " ) , self . switchToolMode )
# add raster filters to combo
self . formatCombo . addItems ( Utils . FileFilter . allRastersFilter ( ) . split ( " ;; " ) )
def switchToolMode ( self ) :
self . setCommandViewerEnabled ( not self . batchCheck . isChecked ( ) )
2011-04-15 18:30:10 +00:00
self . progressBar . setVisible ( self . batchCheck . isChecked ( ) )
self . formatLabel . setVisible ( self . batchCheck . isChecked ( ) )
self . formatCombo . setVisible ( self . batchCheck . isChecked ( ) )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
self . inSelector . setType ( self . inSelector . FILE if self . batchCheck . isChecked ( ) else self . inSelector . FILE_LAYER )
self . outSelector . clear ( )
2010-06-04 00:31:48 +00:00
if self . batchCheck . isChecked ( ) :
self . inFileLabel = self . label_3 . text ( )
self . outFileLabel = self . label_2 . text ( )
2010-12-22 03:25:14 +00:00
self . label_3 . setText ( QCoreApplication . translate ( " GdalTools " , " &Input directory " ) )
self . label_2 . setText ( QCoreApplication . translate ( " GdalTools " , " &Output directory " ) )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
QObject . disconnect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputFile )
QObject . disconnect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputFileEdit )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
QObject . connect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputDir )
QObject . connect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputDir )
2010-06-04 00:31:48 +00:00
else :
self . label_3 . setText ( self . inFileLabel )
self . label_2 . setText ( self . outFileLabel )
2011-04-15 18:30:10 +00:00
QObject . disconnect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputDir )
QObject . disconnect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputDir )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
QObject . connect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputFile )
QObject . connect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputFileEdit )
2010-06-04 00:31:48 +00:00
2011-03-14 16:01:10 +00:00
def onLayersChanged ( self ) :
2011-04-15 18:30:10 +00:00
self . inSelector . setLayers ( Utils . LayerRegistry . instance ( ) . getRasterLayers ( ) )
2010-06-04 00:31:48 +00:00
def fillInputFile ( self ) :
lastUsedFilter = Utils . FileFilter . lastUsedRasterFilter ( )
inputFile = Utils . FileDialog . getOpenFileName ( self , self . tr ( " Select the input file for Translate " ) , Utils . FileFilter . allRastersFilter ( ) , lastUsedFilter )
if inputFile . isEmpty ( ) :
return
Utils . FileFilter . setLastUsedRasterFilter ( lastUsedFilter )
2011-04-15 18:30:10 +00:00
self . inSelector . setFilename ( inputFile )
2010-06-04 00:31:48 +00:00
2010-10-29 22:07:56 +00:00
# get SRS for target file if necessary and possible
self . refreshTargetSRS ( )
2010-06-04 00:31:48 +00:00
def fillInputDir ( self ) :
2010-06-05 01:56:17 +00:00
inputDir = Utils . FileDialog . getExistingDirectory ( self , self . tr ( " Select the input directory with files to Translate " ) )
2010-06-04 00:31:48 +00:00
if inputDir . isEmpty ( ) :
return
2011-04-15 18:30:10 +00:00
self . inSelector . setFilename ( inputDir )
2010-06-04 00:31:48 +00:00
filter = Utils . getRasterExtensions ( )
workDir = QDir ( inputDir )
workDir . setFilter ( QDir . Files | QDir . NoSymLinks | QDir . NoDotAndDotDot )
workDir . setNameFilters ( filter )
2010-12-20 03:34:51 +00:00
# search for a valid SRS, then use it as default target SRS
2013-06-04 09:28:43 +10:00
srs = ' '
2010-12-20 03:34:51 +00:00
for fname in workDir . entryList ( ) :
fl = inputDir + " / " + fname
srs = Utils . getRasterSRS ( self , fl )
if not srs . isEmpty ( ) :
break
self . targetSRSEdit . setText ( srs )
2010-06-04 00:31:48 +00:00
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 . allRastersFilter ( ) , lastUsedFilter )
if outputFile . isEmpty ( ) :
return
Utils . FileFilter . setLastUsedRasterFilter ( lastUsedFilter )
self . outputFormat = Utils . fillRasterOutputFormat ( lastUsedFilter , outputFile )
2011-04-15 18:30:10 +00:00
self . outSelector . setFilename ( outputFile )
2010-06-04 00:31:48 +00:00
def fillOutputDir ( self ) :
outputDir = Utils . FileDialog . getExistingDirectory ( self , self . tr ( " Select the output directory to save the results to " ) )
if outputDir . isEmpty ( ) :
return
2011-04-15 18:30:10 +00:00
self . outSelector . setFilename ( outputDir )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
def fillTargetSRSEditDefault ( self ) :
if self . inSelector . layer ( ) == None :
2010-10-29 22:07:56 +00:00
return
self . refreshTargetSRS ( )
def refreshTargetSRS ( self ) :
self . targetSRSEdit . setText ( Utils . getRasterSRS ( self , self . getInputFileName ( ) ) )
2010-06-04 00:31:48 +00:00
def fillTargetSRSEdit ( self ) :
2012-02-01 13:01:22 +01:00
dialog = SRSDialog ( " Select the target SRS " , self )
2010-06-04 00:31:48 +00:00
if dialog . exec_ ( ) :
self . targetSRSEdit . setText ( dialog . getProjection ( ) )
def getArguments ( self ) :
arguments = QStringList ( )
if self . targetSRSCheck . isChecked ( ) and not self . targetSRSEdit . text ( ) . isEmpty ( ) :
arguments << " -a_srs "
arguments << self . targetSRSEdit . text ( )
2013-01-18 18:20:51 -02:00
if self . creationOptionsGroupBox . isChecked ( ) :
for opt in self . creationOptionsWidget . options ( ) :
arguments << " -co " << opt
2010-08-06 16:43:14 +00:00
if self . outsizeCheck . isChecked ( ) and self . outsizeSpin . value ( ) != 100 :
2011-03-13 23:21:00 +00:00
arguments << " -outsize "
arguments << self . outsizeSpin . text ( )
arguments << self . outsizeSpin . text ( )
2010-08-06 16:43:14 +00:00
if self . expandCheck . isChecked ( ) :
arguments << " -expand "
2011-01-25 18:55:59 +00:00
arguments << self . expand_method [ self . expandCombo . currentIndex ( ) ]
2010-08-06 16:43:14 +00:00
if self . nodataCheck . isChecked ( ) :
arguments << " -a_nodata "
arguments << str ( self . nodataSpin . value ( ) )
if self . sdsCheck . isChecked ( ) :
arguments << " -sds "
if self . srcwinCheck . isChecked ( ) and not self . srcwinEdit . text ( ) . isEmpty ( ) :
coordList = self . srcwinEdit . text ( ) . split ( ' ' , QString . SkipEmptyParts )
if len ( coordList ) == 4 and not coordList [ 3 ] . isEmpty ( ) :
try :
for x in coordList :
test = int ( x )
except ValueError :
#print "Coordinates must be integer numbers."
2010-08-13 01:00:58 +00:00
QMessageBox . critical ( self , self . tr ( " Translate - srcwin " ) , self . tr ( " Image coordinates (pixels) must be integer numbers. " ) )
2010-08-06 16:43:14 +00:00
else :
arguments << " -srcwin "
for x in coordList :
arguments << x
if self . prjwinCheck . isChecked ( ) and not self . prjwinEdit . text ( ) . isEmpty ( ) :
coordList = self . prjwinEdit . text ( ) . split ( ' ' , QString . SkipEmptyParts )
if len ( coordList ) == 4 and not coordList [ 3 ] . isEmpty ( ) :
try :
for x in coordList :
test = float ( x )
except ValueError :
#print "Coordinates must be integer numbers."
2010-08-13 01:00:58 +00:00
QMessageBox . critical ( self , self . tr ( " Translate - prjwin " ) , self . tr ( " Image coordinates (geographic) must be numbers. " ) )
2010-08-06 16:43:14 +00:00
else :
arguments << " -projwin "
for x in coordList :
arguments << x
2010-06-04 00:31:48 +00:00
if self . isBatchEnabled ( ) :
if self . formatCombo . currentIndex ( ) != 0 :
arguments << " -of "
arguments << Utils . fillRasterOutputFormat ( self . formatCombo . currentText ( ) )
return arguments
else :
return arguments
2011-04-15 18:30:10 +00:00
outputFn = self . getOutputFileName ( )
if not outputFn . isEmpty ( ) :
2010-06-04 00:31:48 +00:00
arguments << " -of "
arguments << self . outputFormat
arguments << self . getInputFileName ( )
2011-04-15 18:30:10 +00:00
arguments << outputFn
2013-01-18 18:20:51 -02:00
# set creation options filename/layer for validation
if self . inSelector . layer ( ) :
self . creationOptionsWidget . setRasterLayer ( self . inSelector . layer ( ) )
else :
self . creationOptionsWidget . setRasterFileName ( self . getInputFileName ( ) )
2010-06-04 00:31:48 +00:00
return arguments
def getInputFileName ( self ) :
2011-04-15 18:30:10 +00:00
return self . inSelector . filename ( )
2010-06-04 00:31:48 +00:00
def getOutputFileName ( self ) :
2011-04-15 18:30:10 +00:00
return self . outSelector . filename ( )
2010-06-04 00:31:48 +00:00
def addLayerIntoCanvas ( self , fileInfo ) :
self . iface . addRasterLayer ( fileInfo . filePath ( ) )
def isBatchEnabled ( self ) :
return self . batchCheck . isChecked ( )
def setProgressRange ( self , maximum ) :
self . progressBar . setRange ( 0 , maximum )
def updateProgress ( self , index , total ) :
if index < total :
self . progressBar . setValue ( index + 1 )
else :
self . progressBar . setValue ( 0 )
def batchRun ( self ) :
exts = self . formatCombo . currentText ( ) . remove ( QRegExp ( ' ^.* \ ( ' ) ) . remove ( QRegExp ( ' \ ).*$ ' ) ) . split ( " " )
if not exts . isEmpty ( ) and exts != " * " and exts != " *.* " :
outExt = exts [ 0 ] . remove ( " * " )
else :
outExt = " .tif "
self . base . enableRun ( False )
self . base . setCursor ( Qt . WaitCursor )
inDir = self . getInputFileName ( )
outDir = self . getOutputFileName ( )
filter = Utils . getRasterExtensions ( )
workDir = QDir ( inDir )
workDir . setFilter ( QDir . Files | QDir . NoSymLinks | QDir . NoDotAndDotDot )
workDir . setNameFilters ( filter )
files = workDir . entryList ( )
self . inFiles = [ ]
self . outFiles = [ ]
for f in files :
self . inFiles . append ( inDir + " / " + f )
if outDir != None :
2013-06-15 22:44:11 +02:00
outFile = re . sub ( " \ .[a-zA-Z0-9] { 2,4} " , outExt , f )
2010-06-04 00:31:48 +00:00
self . outFiles . append ( outDir + " / " + outFile )
self . errors = QStringList ( )
self . batchIndex = 0
self . batchTotal = len ( self . inFiles )
self . setProgressRange ( self . batchTotal )
self . runItem ( self . batchIndex , self . batchTotal )