2010-06-04 00:31:48 +00:00
# -*- coding: utf-8 -*-
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
from qgis . core import *
from qgis . gui import *
from ui_widgetWarp import Ui_GdalToolsWidget as Ui_Widget
from widgetBatchBase import GdalToolsBaseBatchWidget as BaseBatchWidget
from dialogSRS import GdalToolsSRSDialog as SRSDialog
import GdalTools_utils as Utils
class GdalToolsDialog ( QWidget , Ui_Widget , BaseBatchWidget ) :
def __init__ ( self , iface ) :
QWidget . __init__ ( self )
self . iface = iface
self . resampling_method = ( ' near ' , ' bilinear ' , ' cubic ' , ' cubicspline ' , ' lanczos ' )
self . setupUi ( self )
BaseBatchWidget . __init__ ( self , self . iface , " gdalwarp " )
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
self . widthSpin . setValue ( 3000 )
self . heightSpin . setValue ( 3000 )
self . progressBar . setValue ( 0 )
self . progressBar . hide ( )
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 . sourceSRSEdit , SIGNAL ( " textChanged(const QString &) " ) , self . sourceSRSCheck ) ,
( self . selectSourceSRSButton , None , self . sourceSRSCheck ) ,
( self . targetSRSEdit , SIGNAL ( " textChanged(const QString &) " ) , self . targetSRSCheck ) ,
( self . selectTargetSRSButton , None , self . targetSRSCheck ) ,
( self . resamplingCombo , SIGNAL ( " currentIndexChanged(int) " ) , self . resamplingCheck ) ,
( self . cacheSpin , SIGNAL ( " valueChanged(int) " ) , self . cacheCheck ) ,
( [ self . widthSpin , self . heightSpin ] , SIGNAL ( " valueChanged(int) " ) , self . resizeGroupBox ) ,
( self . multithreadCheck , SIGNAL ( " stateChanged(int) " ) ) ,
2011-03-14 17:17:37 +00:00
( self . noDataEdit , SIGNAL ( " textChanged( const QString & ) " ) , self . noDataCheck ) ,
2011-04-15 18:30:10 +00:00
( self . maskSelector , SIGNAL ( " filenameChanged() " ) , self . maskCheck , " 1.6.0 " ) ,
2010-06-04 00:31:48 +00:00
]
)
2011-04-15 18:30:10 +00:00
self . connect ( self . inSelector , SIGNAL ( " layerChanged() " ) , self . fillSourceSRSEditDefault )
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 . selectSourceSRSButton , SIGNAL ( " clicked() " ) , self . fillSourceSRSEdit )
self . connect ( self . selectTargetSRSButton , SIGNAL ( " clicked() " ) , self . fillTargetSRSEdit )
2011-04-15 18:30:10 +00:00
self . connect ( self . maskSelector , SIGNAL ( " selectClicked() " ) , self . fillMaskFile )
2010-06-04 00:31:48 +00:00
self . connect ( self . batchCheck , SIGNAL ( " stateChanged( int ) " ) , self . switchToolMode )
# switch to batch or normal mode
def switchToolMode ( self ) :
self . setCommandViewerEnabled ( not self . batchCheck . isChecked ( ) )
2011-04-15 18:30:10 +00:00
self . progressBar . 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 . text ( )
self . outFileLabel = self . label_2 . text ( )
2010-12-22 03:25:14 +00:00
self . label . 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 . 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 ( ) )
self . maskSelector . setLayers ( filter ( lambda x : x . geometryType ( ) == QGis . Polygon , Utils . LayerRegistry . instance ( ) . getVectorLayers ( ) ) )
2011-03-14 17:17:37 +00:00
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 Warp " ) , 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 source file if necessary and possible
self . refreshSourceSRS ( )
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
2011-04-15 18:30:10 +00:00
def fillMaskFile ( self ) :
2011-03-14 17:17:37 +00:00
lastUsedFilter = Utils . FileFilter . lastUsedVectorFilter ( )
2011-04-15 18:30:10 +00:00
maskFile = Utils . FileDialog . getOpenFileName ( self , self . tr ( " Select the mask file " ) , Utils . FileFilter . allVectorsFilter ( ) , lastUsedFilter )
if maskFile . isEmpty ( ) :
2011-03-14 17:17:37 +00:00
return
Utils . FileFilter . setLastUsedVectorFilter ( lastUsedFilter )
2011-04-15 18:30:10 +00:00
self . maskSelector . setFilename ( maskFile )
2011-03-14 17:17:37 +00:00
2010-06-04 00:31:48 +00:00
def fillInputDir ( self ) :
inputDir = Utils . FileDialog . getExistingDirectory ( self , self . tr ( " Select the input directory with files to Warp " ) )
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 )
if workDir . entryList ( ) . count ( ) > 0 :
fl = inputDir + " / " + workDir . entryList ( ) [ 0 ]
self . sourceSRSEdit . setText ( Utils . getRasterSRS ( self , fl ) )
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
def fillSourceSRSEdit ( self ) :
2012-02-01 13:01:22 +01:00
dialog = SRSDialog ( " Select the source SRS " , self )
2010-06-04 00:31:48 +00:00
if dialog . exec_ ( ) :
self . sourceSRSEdit . setText ( dialog . getProjection ( ) )
2011-04-15 18:30:10 +00:00
def fillSourceSRSEditDefault ( self ) :
if self . inSelector . layer ( ) == None :
2010-10-29 22:07:56 +00:00
return
self . refreshSourceSRS ( )
def refreshSourceSRS ( self ) :
crs = Utils . getRasterSRS ( self , self . getInputFileName ( ) )
self . sourceSRSEdit . setText ( crs )
self . sourceSRSCheck . setChecked ( not crs . isEmpty ( ) )
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 . sourceSRSCheck . isChecked ( ) and not self . sourceSRSEdit . text ( ) . isEmpty ( ) :
arguments << " -s_srs "
arguments << self . sourceSRSEdit . text ( )
if self . targetSRSCheck . isChecked ( ) and not self . targetSRSEdit . text ( ) . isEmpty ( ) :
arguments << " -t_srs "
arguments << self . targetSRSEdit . text ( )
if self . resamplingCheck . isChecked ( ) and self . resamplingCombo . currentIndex ( ) > = 0 :
arguments << " -r "
arguments << self . resampling_method [ self . resamplingCombo . currentIndex ( ) ]
if self . cacheCheck . isChecked ( ) :
arguments << " -wm "
arguments << str ( self . cacheSpin . value ( ) )
if self . resizeGroupBox . isChecked ( ) :
2010-10-29 22:07:56 +00:00
arguments << " -ts "
2010-06-04 00:31:48 +00:00
arguments << str ( self . widthSpin . value ( ) )
arguments << str ( self . heightSpin . value ( ) )
if self . multithreadCheck . isChecked ( ) :
arguments << " -multi "
2010-12-20 03:34:51 +00:00
if self . noDataCheck . isChecked ( ) :
nodata = self . noDataEdit . text ( ) . trimmed ( )
if not nodata . isEmpty ( ) :
arguments << " -dstnodata "
arguments << nodata
2011-04-15 18:30:10 +00:00
if self . maskCheck . isChecked ( ) :
mask = self . getMaskFileName ( )
if not mask . isEmpty ( ) :
2011-03-14 17:17:37 +00:00
arguments << " -q "
2011-05-07 00:43:29 +02:00
arguments << " -cutline "
2011-04-15 18:30:10 +00:00
arguments << mask
2011-03-14 17:17:37 +00:00
arguments << " -dstalpha "
2010-06-04 00:31:48 +00:00
if self . isBatchEnabled ( ) :
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
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
2011-04-15 18:30:10 +00:00
def getMaskFileName ( self ) :
return self . maskSelector . filename ( )
2011-03-14 17:17:37 +00:00
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 )
2011-04-15 18:30:10 +00:00