Skip to content

Commit 0ec8951

Browse files
author
Médéric RIBREUX
committed
Add r.mapcalc algorithm
1 parent 4831cf8 commit 0ec8951

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
r.mapcalc
2+
Raster map calculator.
3+
Raster (r.*)
4+
ParameterMultipleInput|maps|Raster maps used in the calculator|3|True
5+
ParameterString|expression|Expression to evaluate. The raster names used in expression should be the same than in QGIS|None|True|True
6+
ParameterFile|file|File containing expression(s) to evaluate (same rule for raster names than above)|True
7+
ParameterString|seed|Integer seed for rand() function|None|False|True
8+
*ParameterBoolean|-s|Generate random seed (result is non-deterministic)|False
9+
OutputDirectory|output_dir|Results Directory
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
r_mapcalc.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+
from os import path
29+
30+
31+
def checkParameterValuesBeforeExecuting(alg):
32+
""" Verify if we have the right parameters """
33+
if alg.getParameterValue('expression') and alg.getParameterValue('file'):
34+
return alg.tr("You need to set either inline expression or a rules file !")
35+
36+
return None
37+
38+
39+
def processInputs(alg):
40+
# We need to use the same raster names than in QGIS
41+
if alg.getParameterValue('maps'):
42+
rasters = alg.getParameterValue('maps').split(',')
43+
for raster in rasters:
44+
if raster in alg.exportedLayers.keys():
45+
continue
46+
47+
alg.setSessionProjectionFromLayer(raster, alg.commands)
48+
destFilename = path.splitext(path.basename(raster))[0]
49+
alg.exportedLayers[raster] = destFilename
50+
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
51+
alg.commands.append(command)
52+
53+
alg.setSessionProjectionFromProject(alg.commands)
54+
55+
region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
56+
regionCoords = region.split(',')
57+
command = 'g.region'
58+
command += ' -a'
59+
command += ' n=' + unicode(regionCoords[3])
60+
command += ' s=' + unicode(regionCoords[2])
61+
command += ' e=' + unicode(regionCoords[1])
62+
command += ' w=' + unicode(regionCoords[0])
63+
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
64+
if cellsize:
65+
command += ' res=' + unicode(cellsize)
66+
else:
67+
command += ' res=' + unicode(alg.getDefaultCellsize())
68+
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
69+
if alignToResolution:
70+
command += ' -a'
71+
alg.commands.append(command)
72+
73+
74+
def processCommand(alg):
75+
# Remove output for command
76+
output_dir = alg.getOutputFromName('output_dir')
77+
maps = alg.getParameterFromName('maps')
78+
alg.removeOutputFromName('output_dir')
79+
alg.parameters.remove(maps)
80+
alg.processCommand()
81+
alg.addOutput(output_dir)
82+
alg.addParameter(maps)
83+
84+
85+
def processOutputs(alg):
86+
# Export everything into Output Directory with shell commands
87+
outputDir = alg.getOutputValue('output_dir')
88+
89+
# Get the list of rasters matching the basename
90+
commands = ["for r in $(g.list type=rast); do"]
91+
commands.append(" r.out.gdal input=${{r}} output={}/${{r}}.tif createopt=\"TFW=YES,COMPRESS=LZW\" --overwrite".format(outputDir))
92+
commands.append("done")
93+
alg.commands.extend(commands)
94+
alg.outputCommands.extend(commands)

0 commit comments

Comments
 (0)