Skip to content
Permalink
Browse files

[processing] add option to keep resolution when clipping raster

  • Loading branch information
alexbruy committed Sep 26, 2013
1 parent c9de713 commit 7628c8f34ff8129dcf679bb50daed53d4d008768
Showing with 13 additions and 0 deletions.
  1. +13 −0 python/plugins/processing/gdal/ClipByMask.py
@@ -25,6 +25,7 @@

import os
from PyQt4 import QtGui
from osgeo import gdal
from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
@@ -45,6 +46,7 @@ class ClipByMask(GeoAlgorithm):
NO_DATA = "NO_DATA"
MASK = "MASK"
ALPHA_BAND = "ALPHA_BAND"
KEEP_RESOLUTION = "KEEP_RESOLUTION"
EXTRA = "EXTRA"

def getIcon(self):
@@ -58,6 +60,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.MASK, "Mask layer", [ParameterVector.VECTOR_TYPE_POLYGON]))
self.addParameter(ParameterString(self.NO_DATA, "Nodata value, leave as none to take the nodata value from input", "none"))
self.addParameter(ParameterBoolean(self.ALPHA_BAND, "Create and output alpha band", False))
self.addParameter(ParameterBoolean(self.KEEP_RESOLUTION, "Keep resolution of output raster", False))
self.addParameter(ParameterString(self.EXTRA, "Additional creation parameters", ""))
self.addOutput(OutputRaster(self.OUTPUT, "Output layer"))

@@ -66,6 +69,7 @@ def processAlgorithm(self, progress):
mask = self.getParameterValue(self.MASK)
noData = str(self.getParameterValue(self.NO_DATA))
addAlphaBand = self.getParameterValue(self.ALPHA_BAND)
keepResolution = self.getParameterValue(self.KEEP_RESOLUTION)
extra = str(self.getParameterValue(self.EXTRA))

arguments = []
@@ -75,6 +79,15 @@ def processAlgorithm(self, progress):
arguments.append("-dstnodata")
arguments.append(noData)

if keepResolution:
r = gdal.Open(self.getParameterValue(self.INPUT))
geoTransform = r.GetGeoTransform()
r = None
arguments.append("-tr")
arguments.append(str(geoTransform[1]))
arguments.append(str(geoTransform[5]))
arguments.append("-tap")

arguments.append("-cutline")
arguments.append(mask)
arguments.append("-crop_to_cutline")

0 comments on commit 7628c8f

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