Skip to content

Commit 6b5fef1

Browse files
committed
[processing] add Clip By Mask algorithm
1 parent 71c8672 commit 6b5fef1

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

python/plugins/processing/gdal/ClipByExtent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
ClipByExtent.py
66
---------------------
77
Date : September 2013
8-
Copyright : (C) 2012 by Alexander Bruy
8+
Copyright : (C) 2013 by Alexander Bruy
99
Email : alexander bruy at gmail dot com
1010
***************************************************************************
1111
* *
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ClipByMask.py
6+
---------------------
7+
Date : September 2013
8+
Copyright : (C) 2013 by Alexander Bruy
9+
Email : alexander bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'September 2013'
22+
__copyright__ = '(C) 2013, Alexander Bruy'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
import os
27+
from PyQt4 import QtGui
28+
from qgis.core import *
29+
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
32+
from processing.parameters.ParameterRaster import ParameterRaster
33+
from processing.parameters.ParameterVector import ParameterVector
34+
from processing.parameters.ParameterBoolean import ParameterBoolean
35+
from processing.parameters.ParameterString import ParameterString
36+
37+
from processing.outputs.OutputRaster import OutputRaster
38+
39+
from processing.gdal.GdalUtils import GdalUtils
40+
41+
class ClipByMask(GeoAlgorithm):
42+
43+
INPUT = "INPUT"
44+
OUTPUT = "OUTPUT"
45+
NO_DATA = "NO_DATA"
46+
MASK = "MASK"
47+
ALPHA_BAND = "ALPHA_BAND"
48+
EXTRA = "EXTRA"
49+
50+
def getIcon(self):
51+
filepath = os.path.dirname(__file__) + "/icons/raster-clip.png"
52+
return QtGui.QIcon(filepath)
53+
54+
def defineCharacteristics(self):
55+
self.name = "Clip raster by mask layer"
56+
self.group = "[GDAL] Extraction"
57+
self.addParameter(ParameterRaster(self.INPUT, "Input layer", False))
58+
self.addParameter(ParameterVector(self.MASK, "Mask layer", [ParameterVector.VECTOR_TYPE_POLYGON]))
59+
self.addParameter(ParameterString(self.NO_DATA, "Nodata value, leave as none to take the nodata value from input", "none"))
60+
self.addParameter(ParameterBoolean(self.ALPHA_BAND, "Create and output alpha band", False))
61+
self.addParameter(ParameterString(self.EXTRA, "Additional creation parameters", ""))
62+
self.addOutput(OutputRaster(self.OUTPUT, "Output layer"))
63+
64+
def processAlgorithm(self, progress):
65+
out = self.getOutputValue(self.OUTPUT)
66+
mask = self.getParameterValue(self.MASK)
67+
noData = str(self.getParameterValue(self.NO_DATA))
68+
addAlphaBand = self.getParameterValue(self.ALPHA_BAND)
69+
extra = str(self.getParameterValue(self.EXTRA))
70+
71+
arguments = []
72+
arguments.append("-q")
73+
arguments.append("-of")
74+
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
75+
arguments.append("-dstnodata")
76+
arguments.append(noData)
77+
78+
arguments.append("-cutline")
79+
arguments.append(mask)
80+
arguments.append("-crop_to_cutline")
81+
82+
if addAlphaBand:
83+
arguments.append("-dstalpha")
84+
85+
if len(extra) > 0:
86+
arguments.append(extra)
87+
88+
arguments.append(self.getParameterValue(self.INPUT))
89+
arguments.append(out)
90+
91+
GdalUtils.runGdal(["gdalwarp", GdalUtils.escapeAndJoin(arguments)], progress)

python/plugins/processing/gdal/GdalOgrAlgorithmProvider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from processing.gdal.polygonize import polygonize
4545
from processing.gdal.gdaladdo import gdaladdo
4646
from processing.gdal.ClipByExtent import ClipByExtent
47+
from processing.gdal.ClipByMask import ClipByMask
4748

4849
from processing.gdal.ogr2ogr import Ogr2Ogr
4950
from processing.gdal.ogrinfo import OgrInfo
@@ -92,7 +93,7 @@ def createAlgsList(self):
9293

9394
self.preloadedAlgs = [nearblack(), information(), warp(), translate(),
9495
rgb2pct(), pct2rgb(), merge(), polygonize(),
95-
gdaladdo(), ClipByExtent(),
96+
gdaladdo(), ClipByExtent(), ClipByMask(),
9697
OgrInfo(), Ogr2Ogr(), OgrSql()]
9798

9899
#And then we add those that are created as python scripts

0 commit comments

Comments
 (0)