Skip to content

Commit a5645bd

Browse files
Médéric RibreuxMédéric RIBREUX
Médéric Ribreux
authored and
Médéric RIBREUX
committed
Add r.category algorithms
1 parent 628a7fa commit a5645bd

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
r.category
2+
r.category.out - Exports category values and labels associated with user-specified raster map layers.
3+
Raster (r.*)
4+
ParameterRaster|map|Name of raster map|False
5+
ParameterString|cats|Category values (for Integer rasters). Example: 1,3,7-9,13|None|False|True
6+
ParameterString|values|Comma separated value list (for float rasters). Example: 1.4,3.8,13|None|False|True
7+
ParameterString|separator|Field separator (Special characters: pipe, comma, space, tab, newline)|tab|False|True
8+
OutputFile|output|Category
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
r.category
2+
Manages category values and labels associated with user-specified raster map layers.
3+
Raster (r.*)
4+
ParameterRaster|map|Name of raster map|False
5+
ParameterString|separator|Field separator (Special characters: pipe, comma, space, tab, newline)|tab|False|True
6+
ParameterFile|rules|File containing category label rules|False|True
7+
ParameterString|txtrules|Inline category label rules|None|True|True
8+
ParameterRaster|raster|Raster map from which to copy category table|True
9+
*ParameterString|format|Default label or format string for dynamic labeling. Used when no explicit label exists for the category|None|False|True
10+
*ParameterString|coefficients|Dynamic label coefficients. Two pairs of category multiplier and offsets, for $1 and $2|None|False|True
11+
OutputRaster|output|Category
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
r_category.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+
import codecs
29+
from processing.tools.system import getTempFilename
30+
31+
def checkParameterValuesBeforeExecuting(alg):
32+
""" Verify if we have the right parameters """
33+
rules = alg.getParameterValue(u'rules')
34+
txtrules = alg.getParameterValue(u'txtrules')
35+
raster = alg.getParameterValue(u'raster')
36+
37+
if rules and txtrules:
38+
return alg.tr("You need to set either a rules file or write directly the rules !")
39+
elif (rules and raster) or (txtrules and raster):
40+
return alg.tr("You need to set either rules or a raster from which to copy categories !")
41+
42+
return None
43+
44+
def processInputs(alg):
45+
# If there is another raster to copy categories from
46+
# we need to import it with r.in.gdal rather than r.external
47+
inputRaster = alg.getParameterValue(u'map')
48+
copyRaster = alg.getParameterValue(u'raster')
49+
if copyRaster:
50+
if copyRaster in alg.exportedLayers.keys():
51+
return
52+
53+
for raster,method in (inputRaster, 'r.external'),(copyRaster, 'r.in.gdal'):
54+
alg.setSessionProjectionFromLayer(raster, alg.commands)
55+
56+
destFilename = alg.getTempFilename()
57+
alg.exportedLayers[raster] = destFilename
58+
command = '{} input={} output={} band=1 --overwrite -o'.format(method, raster, destFilename)
59+
alg.commands.append(command)
60+
61+
alg.setSessionProjectionFromProject(alg.commands)
62+
63+
region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
64+
regionCoords = region.split(',')
65+
command = 'g.region'
66+
command += ' -a'
67+
command += ' n=' + unicode(regionCoords[3])
68+
command += ' s=' + unicode(regionCoords[2])
69+
command += ' e=' + unicode(regionCoords[1])
70+
command += ' w=' + unicode(regionCoords[0])
71+
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
72+
if cellsize:
73+
command += ' res=' + unicode(cellsize)
74+
else:
75+
command += ' res=' + unicode(alg.getDefaultCellsize())
76+
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
77+
if alignToResolution:
78+
command += ' -a'
79+
alg.commands.append(command)
80+
else:
81+
alg.processInputs()
82+
83+
84+
def processCommand(alg):
85+
# We temporary remove the output
86+
out = alg.getOutputFromName('output')
87+
mapParam = alg.getParameterValue('map')
88+
alg.exportedLayers[out.value] = alg.exportedLayers[mapParam]
89+
alg.removeOutputFromName('output')
90+
txtRulesParam = alg.getParameterFromName(u'txtrules')
91+
rules = alg.getParameterFromName(u'rules')
92+
93+
# Handle inline rules
94+
if txtRulesParam.value:
95+
# Creates a temporary txt file
96+
tempRulesName = getTempFilename('txt')
97+
98+
# Inject rules into temporary txt file
99+
with codecs.open(tempRulesName, 'w', 'utf-8') as tempRules:
100+
tempRules.write(txtRulesParam.value)
101+
102+
# Replace rules with temporary file
103+
rules.value = tempRulesName
104+
alg.parameters.remove(txtRulesParam)
105+
106+
alg.processCommand()
107+
108+
# We re-add the new output
109+
alg.addOutput(out)
110+
111+
112+
def processOutputs(alg):
113+
# Output results ('from' table and output table)
114+
out = alg.getOutputValue('output')
115+
command = u"r.out.gdal --overwrite -t -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\"".format(
116+
alg.exportedLayers[out], out)
117+
alg.commands.append(command)
118+
alg.outputCommands.append(command)

0 commit comments

Comments
 (0)