2010-06-04 00:31:48 +00:00
# -*- coding: utf-8 -*-
2012-10-06 13:10:25 +02:00
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
doRasterize . 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 osgeo import ogr
from ui_widgetRasterize 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 . setupUi ( self )
BasePluginWidget . __init__ ( self , self . iface , " gdal_rasterize " )
2011-04-15 18:30:10 +00:00
self . outSelector . setType ( self . outSelector . FILE )
2011-03-13 23:21:00 +00:00
# set the default QSpinBoxes and QProgressBar value
self . widthSpin . setValue ( 3000 )
self . heightSpin . setValue ( 3000 )
2013-03-18 15:24:46 +01:00
self . horizresSpin . setValue ( 1 )
self . vertresSpin . setValue ( 1 )
2011-03-13 23:21:00 +00:00
2010-06-04 00:31:48 +00:00
self . lastEncoding = Utils . getLastUsedEncoding ( )
self . setParamsStatus (
[
2011-04-15 18:30:10 +00:00
( self . inSelector , SIGNAL ( " filenameChanged() " ) ) ,
( self . outSelector , SIGNAL ( " filenameChanged() " ) ) ,
2011-03-13 23:21:00 +00:00
( self . attributeComboBox , SIGNAL ( " currentIndexChanged(int) " ) ) ,
2013-03-18 15:24:46 +01:00
( [ self . widthSpin , self . heightSpin ] , SIGNAL ( " valueChanged(int) " ) ) ,
( [ self . horizresSpin , self . vertresSpin ] , SIGNAL ( " valueChanged(double) " ) )
2010-06-04 00:31:48 +00:00
]
)
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 . inSelector , SIGNAL ( " layerChanged() " ) , self . fillFieldsCombo )
2013-03-18 15:24:46 +01:00
self . connect ( self . radioSetSize , SIGNAL ( " toggled(bool) " ) , self . someValueChanged )
self . connect ( self . radioSetResolution , SIGNAL ( " toggled(bool) " ) , self . someValueChanged )
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 ( ) . getVectorLayers ( ) )
2010-06-04 00:31:48 +00:00
def fillFieldsCombo ( self ) :
2011-04-15 18:30:10 +00:00
if self . inSelector . layer ( ) == None :
2010-06-04 00:31:48 +00:00
return
2011-04-15 18:30:10 +00:00
self . lastEncoding = self . inSelector . layer ( ) . dataProvider ( ) . encoding ( )
2010-06-04 00:31:48 +00:00
self . loadFields ( self . getInputFileName ( ) )
def fillInputFileEdit ( self ) :
lastUsedFilter = Utils . FileFilter . lastUsedVectorFilter ( )
inputFile , encoding = Utils . FileDialog . getOpenFileName ( self , self . tr ( " Select the input file for Rasterize " ) , Utils . FileFilter . allVectorsFilter ( ) , lastUsedFilter , True )
2013-06-16 11:34:42 +02:00
if not inputFile :
2010-06-04 00:31:48 +00:00
return
Utils . FileFilter . setLastUsedVectorFilter ( lastUsedFilter )
2011-04-15 18:30:10 +00:00
self . inSelector . setFilename ( inputFile )
2010-06-04 00:31:48 +00:00
self . lastEncoding = encoding
self . loadFields ( inputFile )
def fillOutputFileEdit ( self ) :
lastUsedFilter = Utils . FileFilter . lastUsedRasterFilter ( )
2011-03-13 23:21:00 +00:00
# rasterize supports output file creation for GDAL 1.8
2013-07-19 23:10:16 +02:00
gdalVersion = Utils . GdalConfig . versionNum ( )
if gdalVersion > = 1800 :
2011-04-15 18:30:10 +00:00
fileDialogFunc = Utils . FileDialog . getSaveFileName
else :
2011-03-13 23:21:00 +00:00
fileDialogFunc = Utils . FileDialog . getOpenFileName
2011-04-15 18:30:10 +00:00
2011-03-13 23:21:00 +00:00
outputFile = fileDialogFunc ( self , self . tr ( " Select the raster file to save the results to " ) , Utils . FileFilter . allRastersFilter ( ) , lastUsedFilter )
2013-06-16 11:34:42 +02:00
if not outputFile :
2010-06-04 00:31:48 +00:00
return
Utils . FileFilter . setLastUsedRasterFilter ( lastUsedFilter )
2011-04-15 18:30:10 +00:00
self . outSelector . setFilename ( outputFile )
2010-06-04 00:31:48 +00:00
2013-05-01 23:45:20 +02:00
# required either -ts or -tr to create the output file
2013-07-19 23:10:16 +02:00
if gdalVersion > = 1800 :
2011-03-13 23:21:00 +00:00
if not QFileInfo ( outputFile ) . exists ( ) :
2013-03-18 15:24:46 +01:00
QMessageBox . information ( self , self . tr ( " Output size or resolution required " ) , self . tr ( " The output file doesn ' t exist. You must set up the output size or resolution to create it. " ) )
self . radioSetSize . setChecked ( True )
2011-03-13 23:21:00 +00:00
2010-06-04 00:31:48 +00:00
def getArguments ( self ) :
2013-06-16 11:34:42 +02:00
arguments = [ ]
2010-06-04 00:31:48 +00:00
if self . attributeComboBox . currentIndex ( ) > = 0 :
2013-06-16 11:34:42 +02:00
arguments . append ( " -a " )
arguments . append ( self . attributeComboBox . currentText ( ) )
2013-03-18 15:24:46 +01:00
if self . radioSetSize . isChecked ( ) :
2013-06-16 11:34:42 +02:00
arguments . append ( " -ts " )
arguments . append ( self . widthSpin . value ( ) )
arguments . append ( self . heightSpin . value ( ) )
2013-03-18 15:24:46 +01:00
if self . radioSetResolution . isChecked ( ) :
2013-06-16 11:34:42 +02:00
arguments . append ( " -tr " )
arguments . append ( self . horizresSpin . value ( ) )
arguments . append ( self . vertresSpin . value ( ) )
2011-04-15 18:30:10 +00:00
inputFn = self . getInputFileName ( )
2013-06-16 11:34:42 +02:00
if inputFn :
arguments . append ( " -l " )
arguments . append ( QFileInfo ( inputFn ) . baseName ( ) )
arguments . append ( inputFn )
arguments . append ( self . getOutputFileName ( ) )
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 loadFields ( self , vectorFile ) :
self . attributeComboBox . clear ( )
2013-06-16 11:34:42 +02:00
if not vectorFile :
2010-06-04 00:31:48 +00:00
return
try :
( fields , names ) = Utils . getVectorFields ( vectorFile )
2011-01-15 12:49:35 +00:00
except Utils . UnsupportedOGRFormat , e :
QErrorMessage ( self ) . showMessage ( e . args [ 0 ] )
2011-04-15 18:30:10 +00:00
self . inSelector . setLayer ( None )
2010-06-04 00:31:48 +00:00
return
ncodec = QTextCodec . codecForName ( self . lastEncoding )
for name in names :
self . attributeComboBox . addItem ( ncodec . toUnicode ( name ) )