Skip to content
Permalink
Browse files

Adds the -crop_to_cutline parameter as optional to fix the gdalwarp d…

…eviation described in #4530. Option added to Clipper tool from GdalTools and Processing. It also adds the -tr xres yres parameter to GdalTools Clipper tool. Also get the original raster resolution.
  • Loading branch information
PedroVenancio committed Oct 23, 2015
1 parent c7eef14 commit 5057f761fa09a3f7095b58acfcb107d279d8c74a
@@ -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
@@ -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):
@@ -46,12 +45,19 @@ 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.keepResolutionRadio),
([self.xRes, self.yRes], SIGNAL("valueChanged(double)"), self.setResolutionRadio),
(self.extentSelector, [SIGNAL("selectionStarted()"), SIGNAL("newExtentDefined()")], self.extentModeRadio),
(self.modeStackedWidget, SIGNAL("currentIndexChanged(int)"))
])
@@ -63,9 +69,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):
@@ -82,6 +90,12 @@ def switchClippingMode(self):
self.modeStackedWidget.setCurrentIndex(index)
self.checkRun()

def switchResolutionMode(self):
if self.keepResolutionRadio.isChecked():
self.resolutionStackedWidget.setCurrentIndex(0)
else:
self.resolutionStackedWidget.setCurrentIndex(1)

def checkRun(self):
if self.extentModeRadio.isChecked():
enabler = self.extentSelector.isCoordsValid()
@@ -135,13 +149,14 @@ 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()))
@@ -150,32 +165,44 @@ def getArgsModeExtent(self):
if not self.getOutputFileName() == '':
arguments.append("-of")
arguments.append(self.outputFormat)
arguments.append(self.getInputFileName())
arguments.append(inputFn)
arguments.append(self.getOutputFileName())
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():
arguments.append("-tr")
resolution = Utils.getRasterResolution(self.getInputFileName())
if resolution is not None:
arguments.append(resolution[0])
arguments.append(resolution[1])
if self.setResolutionRadio.isChecked():
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

0 comments on commit 5057f76

Please sign in to comment.
You can’t perform that action at this time.