2011-03-15 11:40:45 +00:00
# -*- coding: utf-8 -*-
2012-10-06 13:10:25 +02:00
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
doDEM . py
- - - - - - - - - - - - - - - - - - - - -
Date : March 2011
Copyright : ( C ) 2011 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__ = ' March 2011 '
__copyright__ = ' (C) 2011, Giuseppe Sucameli '
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = ' $Format: % H$ '
2011-03-15 11:40:45 +00:00
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
from qgis . core import *
from qgis . gui import *
from ui_widgetDEM import Ui_GdalToolsWidget as Ui_Widget
from widgetPluginBase import GdalToolsBasePluginWidget as BasePluginWidget
import GdalTools_utils as Utils
class GdalToolsDialog ( QWidget , Ui_Widget , BasePluginWidget ) :
def __init__ ( self , iface ) :
QWidget . __init__ ( self )
self . iface = iface
self . modes = ( " hillshade " , " slope " , " aspect " , " color-relief " , " TRI " , " TPI " , " roughness " )
self . setupUi ( self )
BasePluginWidget . __init__ ( self , self . iface , " gdaldem " )
2011-04-15 18:30:10 +00:00
self . outSelector . setType ( self . outSelector . FILE )
self . configSelector . setType ( self . configSelector . FILE )
2011-03-15 11:40:45 +00:00
# set the default QSpinBoxes and QProgressBar value
self . bandSpin . setValue ( 1 )
self . hillshadeZFactorSpin . setValue ( 1 )
self . hillshadeScaleSpin . setValue ( 1 )
self . hillshadeAltitudeSpin . setValue ( 45.0 )
self . hillshadeAzimuthSpin . setValue ( 315.0 )
self . slopeScaleSpin . setValue ( 1 )
2011-03-23 16:57:22 +00:00
# set the default color configuration file to terrain
import os . path
colorConfigFile = os . path . join ( os . path . dirname ( __file__ ) , " terrain.txt " )
2011-04-15 18:30:10 +00:00
self . configSelector . setFilename ( colorConfigFile )
2011-03-23 16:57:22 +00:00
2011-03-15 11:40:45 +00:00
self . outputFormat = Utils . fillRasterOutputFormat ( )
2013-01-18 18:20:51 -02:00
self . creationOptionsWidget . setFormat ( self . outputFormat )
2011-03-15 11:40:45 +00:00
self . setParamsStatus (
[
2012-12-10 00:12:07 +01:00
( self . inSelector , SIGNAL ( " filenameChanged() " ) ) ,
( self . outSelector , SIGNAL ( " filenameChanged() " ) ) ,
2013-01-18 18:20:51 -02:00
( self . computeEdgesCheck , SIGNAL ( " stateChanged(int) " ) , None , 1800 ) ,
2012-12-10 00:12:07 +01:00
( self . bandSpin , SIGNAL ( " valueChanged(int) " ) , self . bandCheck ) ,
2013-01-18 18:20:51 -02:00
( self . algorithmCheck , SIGNAL ( " stateChanged(int) " ) , None , 1800 ) ,
( self . creationOptionsWidget , SIGNAL ( " optionsChanged() " ) ) ,
( self . creationOptionsGroupBox , SIGNAL ( " toggled(bool) " ) ) ,
2011-03-15 11:40:45 +00:00
( self . modeCombo , SIGNAL ( " currentIndexChanged(int) " ) ) ,
2012-12-10 00:12:07 +01:00
( [ self . hillshadeZFactorSpin , self . hillshadeScaleSpin , self . hillshadeAltitudeSpin , self . hillshadeAzimuthSpin ] , SIGNAL ( " valueChanged(double) " ) ) ,
( self . slopeScaleSpin , SIGNAL ( " valueChanged(double) " ) ) ,
( self . slopePercentCheck , SIGNAL ( " stateChanged(int) " ) ) ,
( [ self . aspectTrigonometricCheck , self . aspectZeroForFlatCheck ] , SIGNAL ( " stateChanged(int) " ) ) ,
( self . configSelector , SIGNAL ( " filenameChanged() " ) ) ,
( [ self . colorExactRadio , self . colorNearestRadio ] , SIGNAL ( " toggled(bool) " ) , self . colorMatchGroupBox ) ,
2011-03-15 11:40:45 +00:00
( self . colorAlphaCheck , SIGNAL ( " stateChanged(int) " ) )
]
)
2011-04-15 18:30:10 +00:00
self . connect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputFileEdit )
self . connect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputFileEdit )
self . connect ( self . configSelector , SIGNAL ( " selectClicked() " ) , self . fillColorConfigFileEdit )
2011-03-15 11:40:45 +00:00
self . connect ( self . modeCombo , SIGNAL ( " currentIndexChanged(int) " ) , self . showModeParams )
def showModeParams ( self , index ) :
self . stackedWidget . setVisible ( index < 4 )
2011-03-15 14:21:29 +00:00
self . algorithmCheck . setVisible ( index < 3 )
if index > = 3 :
self . algorithmCheck . setChecked ( False )
2011-03-15 11:40:45 +00:00
def onLayersChanged ( self ) :
2011-04-15 18:30:10 +00:00
self . inSelector . setLayers ( Utils . LayerRegistry . instance ( ) . getRasterLayers ( ) )
2011-03-15 11:40:45 +00:00
def fillInputFileEdit ( self ) :
lastUsedFilter = Utils . FileFilter . lastUsedRasterFilter ( )
inputFile = Utils . FileDialog . getOpenFileName ( self , self . tr ( " Select the file for DEM " ) , Utils . FileFilter . allRastersFilter ( ) , lastUsedFilter )
2013-06-17 15:27:54 +02:00
if not inputFile :
2011-03-15 11:40:45 +00:00
return
Utils . FileFilter . setLastUsedRasterFilter ( lastUsedFilter )
2011-04-15 18:30:10 +00:00
self . inSelector . setFilename ( inputFile )
2013-01-18 18:20:51 -02:00
self . getArguments ( )
2011-03-15 11:40:45 +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 )
2013-06-17 15:27:54 +02:00
if not outputFile :
2011-03-15 11:40:45 +00:00
return
Utils . FileFilter . setLastUsedRasterFilter ( lastUsedFilter )
self . outputFormat = Utils . fillRasterOutputFormat ( lastUsedFilter , outputFile )
2011-04-15 18:30:10 +00:00
self . outSelector . setFilename ( outputFile )
2013-01-18 18:20:51 -02:00
self . creationOptionsWidget . setFormat ( self . outputFormat )
2011-03-15 11:40:45 +00:00
def fillColorConfigFileEdit ( self ) :
2011-03-23 16:57:22 +00:00
configFile = Utils . FileDialog . getOpenFileName ( self , self . tr ( " Select the color configuration file " ) )
2013-06-17 15:27:54 +02:00
if not configFile :
2011-03-15 11:40:45 +00:00
return
2011-04-15 18:30:10 +00:00
self . configSelector . setFilename ( configFile )
2011-03-15 11:40:45 +00:00
def getArguments ( self ) :
mode = self . modes [ self . modeCombo . currentIndex ( ) ]
2013-06-17 15:27:54 +02:00
arguments = [ ]
arguments . append ( mode )
arguments . append ( self . getInputFileName ( ) )
2011-03-15 11:40:45 +00:00
if mode == " color-relief " :
2013-06-17 15:27:54 +02:00
arguments . append ( self . configSelector . filename ( ) )
2011-04-15 18:30:10 +00:00
outputFn = self . getOutputFileName ( )
2013-06-17 15:27:54 +02:00
arguments . append ( outputFn )
2011-03-15 11:40:45 +00:00
if mode == " hillshade " :
2013-06-19 18:04:52 +02:00
arguments . extend ( [ " -z " , self . hillshadeZFactorSpin . value ( ) ] )
arguments . extend ( [ " -s " , self . hillshadeScaleSpin . value ( ) ] )
arguments . extend ( [ " -az " , self . hillshadeAzimuthSpin . value ( ) ] )
arguments . extend ( [ " -alt " , self . hillshadeAltitudeSpin . value ( ) ] )
2011-03-15 11:40:45 +00:00
elif mode == " slope " :
if self . slopePercentCheck . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -p " )
2013-06-19 18:04:52 +02:00
arguments . extend ( [ " -s " , self . slopeScaleSpin . value ( ) ] )
2011-03-15 11:40:45 +00:00
elif mode == " aspect " :
if self . aspectTrigonometricCheck . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -trigonometric " )
2011-03-15 11:40:45 +00:00
if self . aspectZeroForFlatCheck . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -zero_for_flat " )
2011-03-15 11:40:45 +00:00
elif mode == " color-relief " :
if self . colorAlphaCheck . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -alpha " )
2011-03-15 11:40:45 +00:00
if self . colorMatchGroupBox . isChecked ( ) :
if self . colorExactRadio . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -exact_color_entry " )
2011-03-15 11:40:45 +00:00
elif self . colorNearestRadio . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -nearest_color_entry " )
2011-03-15 14:21:29 +00:00
if self . algorithmCheck . isChecked ( ) :
2013-06-19 18:04:52 +02:00
arguments . extend ( [ " -alg " , " ZevenbergenThorne " ] )
2011-03-15 11:40:45 +00:00
if self . computeEdgesCheck . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -compute_edges " )
2011-03-15 11:40:45 +00:00
if self . bandCheck . isChecked ( ) :
2013-06-19 18:04:52 +02:00
arguments . extend ( [ " -b " , self . bandSpin . value ( ) ] )
2013-06-17 15:27:54 +02:00
if outputFn :
2013-06-19 18:04:52 +02:00
arguments . extend ( [ " -of " , self . outputFormat ] )
2013-01-18 18:20:51 -02:00
if self . creationOptionsGroupBox . isChecked ( ) :
for opt in self . creationOptionsWidget . options ( ) :
2013-06-19 18:04:52 +02:00
arguments . extend ( [ " -co " , opt ] )
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 ( ) )
2011-03-15 11:40:45 +00:00
return arguments
def getInputFileName ( self ) :
2011-04-15 18:30:10 +00:00
return self . inSelector . filename ( )
2011-03-15 11:40:45 +00:00
def getOutputFileName ( self ) :
2011-04-15 18:30:10 +00:00
return self . outSelector . filename ( )
2011-03-15 11:40:45 +00:00
def addLayerIntoCanvas ( self , fileInfo ) :
self . iface . addRasterLayer ( fileInfo . filePath ( ) )