Skip to content

Commit 0061dcc

Browse files
committedMar 2, 2016
Merge pull request #2782 from medspx/processing_r_reclass
[processing] add GRASS7 r.reclass inline rules (fix #5583)
2 parents 91e222e + b780d7c commit 0061dcc

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
 

‎python/plugins/processing/algs/grass7/description/r.reclass.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ Creates a new map layer whose category values are based upon a reclassification
33
Raster (r.*)
44
ParameterRaster|input|Input raster layer|False
55
ParameterFile|rules|File containing reclass rules|-
6+
ParameterString|txtrules|Reclass rules text (if rule file not used)|None|True|True
67
OutputRaster|output|Reclassified
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
r_reclass.py
6+
------------
7+
Date : February 2016
8+
Copyright : (C) 2016 by Médéric Ribreux
9+
Email : medspx at medspx dot fr
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__ = 'Médéric Ribreux'
21+
__date__ = 'February 2016'
22+
__copyright__ = '(C) 2016, Médéric Ribreux'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
29+
def checkParameterValuesBeforeExecuting(alg):
30+
""" Verify if we have the right parameters """
31+
if alg.getParameterValue(u'rules') and alg.getParameterValue(u'txtrules'):
32+
return alg.tr("You need to set either a rules file or write directly the rules !")
33+
34+
return None
35+
36+
37+
def processCommand(alg):
38+
""" Handle inline rules """
39+
txtRules = alg.getParameterValue(u'txtrules')
40+
if txtRules:
41+
# Creates a temporary txt file
42+
tempRulesName = alg.getTempFilename()
43+
44+
# Inject rules into temporary txt file
45+
with open(tempRulesName, "w") as tempRules:
46+
tempRules.write(txtRules)
47+
48+
raster = alg.getParameterValue(u'input')
49+
output = alg.getOutputFromName(u'output')
50+
alg.exportedLayers[output.value] = output.name + alg.uniqueSufix
51+
if raster:
52+
raster = alg.exportedLayers[raster]
53+
command = u"r.reclass input={} rules=- output={} --overwrite < {}".format(
54+
raster, output.name + alg.uniqueSufix, tempRulesName)
55+
alg.commands.append(command)
56+
else:
57+
alg.processCommand()

0 commit comments

Comments
 (0)
Please sign in to comment.