Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2394 from PedroVenancio
  • Loading branch information
brushtyler committed Oct 26, 2015
2 parents 3d93237 + 22aea32 commit ad45f32
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 64 deletions.
16 changes: 16 additions & 0 deletions python/plugins/GdalTools/tools/GdalTools_utils.py
Expand Up @@ -364,6 +364,22 @@ def getRasterExtent(parent, fileName):
return QgsRectangle(xUL, yLR, xLR, yUL)


# get raster resolution

def getRasterResolution(fileName):
ds = gdal.Open(fileName)
if ds is None:
return

gt = ds.GetGeoTransform()

if gt is None:
return
else:
xRes = abs(gt[1])
yRes = abs(gt[5])
return (xRes, yRes)

# This class is used to replace the QFileDialog class.
# Its static methods are used in place of the respective QFileDialog ones to:
# 1. set the last used directory
Expand Down
46 changes: 36 additions & 10 deletions python/plugins/GdalTools/tools/doClipper.py
Expand Up @@ -31,7 +31,6 @@
from widgetPluginBase import GdalToolsBasePluginWidget as BasePluginWidget
import GdalTools_utils as Utils


class GdalToolsDialog(QWidget, Ui_Widget, BasePluginWidget):

def __init__(self, iface):
Expand All @@ -46,12 +45,18 @@ def __init__(self, iface):
self.extentSelector.setCanvas(self.canvas)
self.outputFormat = Utils.fillRasterOutputFormat()

# set the default QDoubleSpinBoxes
self.xRes.setValue(12.5)
self.yRes.setValue(12.5)

self.setParamsStatus([
(self.inSelector, SIGNAL("filenameChanged()")),
(self.outSelector, SIGNAL("filenameChanged()")),
(self.noDataSpin, SIGNAL("valueChanged(int)"), self.noDataCheck, 1700),
(self.maskSelector, SIGNAL("filenameChanged()"), self.maskModeRadio, 1600),
(self.alphaBandCheck, SIGNAL("stateChanged( int )")),
(self.alphaBandCheck, SIGNAL("stateChanged(int)")),
(self.cropToCutlineCheck, SIGNAL("stateChanged(int)")),
([self.xRes, self.yRes], SIGNAL("valueChanged(double)"), self.setResolutionRadio),
(self.extentSelector, [SIGNAL("selectionStarted()"), SIGNAL("newExtentDefined()")], self.extentModeRadio),
(self.modeStackedWidget, SIGNAL("currentIndexChanged(int)"))
])
Expand All @@ -63,9 +68,11 @@ def __init__(self, iface):
self.connect(self.extentSelector, SIGNAL("selectionStarted()"), self.checkRun)

self.connect(self.extentModeRadio, SIGNAL("toggled(bool)"), self.switchClippingMode)
self.connect(self.keepResolutionRadio, SIGNAL("toggled(bool)"), self.switchResolutionMode)

def show_(self):
self.switchClippingMode()
self.switchResolutionMode()
BasePluginWidget.show_(self)

def onClosing(self):
Expand All @@ -82,6 +89,12 @@ def switchClippingMode(self):
self.modeStackedWidget.setCurrentIndex(index)
self.checkRun()

def switchResolutionMode(self):
if self.keepResolutionRadio.isChecked():
self.resolutionWidget.hide()
else:
self.resolutionWidget.show()

def checkRun(self):
if self.extentModeRadio.isChecked():
enabler = self.extentSelector.isCoordsValid()
Expand Down Expand Up @@ -135,47 +148,60 @@ def getArguments(self):

def getArgsModeExtent(self):
self.base.setPluginCommand("gdal_translate")
inputFn = self.getInputFileName()
arguments = []
if self.noDataCheck.isChecked():
arguments.append("-a_nodata")
arguments.append(unicode(self.noDataSpin.value()))
if self.extentModeRadio.isChecked() and self.extentSelector.isCoordsValid():
rect = self.extentSelector.getExtent()
if rect is not None:
if rect is not None and not inputFn == '':
arguments.append("-projwin")
arguments.append(unicode(rect.xMinimum()))
arguments.append(unicode(rect.yMaximum()))
arguments.append(unicode(rect.xMaximum()))
arguments.append(unicode(rect.yMinimum()))
if not self.getOutputFileName() == '':
outputFn = self.getOutputFileName()
if not outputFn == '':
arguments.append("-of")
arguments.append(self.outputFormat)
arguments.append(self.getInputFileName())
arguments.append(self.getOutputFileName())
arguments.append(inputFn)
arguments.append(outputFn)
return arguments

def getArgsModeMask(self):
self.base.setPluginCommand("gdalwarp")
inputFn = self.getInputFileName()
arguments = []
if self.noDataCheck.isChecked():
arguments.append("-dstnodata")
arguments.append(unicode(self.noDataSpin.value()))
if self.maskModeRadio.isChecked():
mask = self.maskSelector.filename()
if not mask == '':
if not mask == '' and not inputFn == '':
arguments.append("-q")
arguments.append("-cutline")
arguments.append(mask)
if Utils.GdalConfig.versionNum() >= 1800:
arguments.append("-crop_to_cutline")
if self.cropToCutlineCheck.isChecked():
arguments.append("-crop_to_cutline")
if self.alphaBandCheck.isChecked():
arguments.append("-dstalpha")

if self.keepResolutionRadio.isChecked():
resolution = Utils.getRasterResolution(inputFn)
if resolution is not None:
arguments.append("-tr")
arguments.append(resolution[0])
arguments.append(resolution[1])
else:
arguments.append("-tr")
arguments.append(unicode(self.xRes.value()))
arguments.append(unicode(self.yRes.value()))
outputFn = self.getOutputFileName()
if not outputFn == '':
arguments.append("-of")
arguments.append(self.outputFormat)
arguments.append(self.getInputFileName())
arguments.append(inputFn)
arguments.append(outputFn)
return arguments

Expand Down

0 comments on commit ad45f32

Please sign in to comment.