[GRASS-SVN] r62266 - in grass-addons/grass7/raster: . r.gradient

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 16 01:30:55 PDT 2014


Author: lucadelu
Date: 2014-10-16 01:30:55 -0700 (Thu, 16 Oct 2014)
New Revision: 62266

Added:
   grass-addons/grass7/raster/r.gradient/
   grass-addons/grass7/raster/r.gradient/Makefile
   grass-addons/grass7/raster/r.gradient/r.gradient.html
   grass-addons/grass7/raster/r.gradient/r.gradient.py
Log:
addons: added new script to create gradient map

Added: grass-addons/grass7/raster/r.gradient/Makefile
===================================================================
--- grass-addons/grass7/raster/r.gradient/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.gradient/Makefile	2014-10-16 08:30:55 UTC (rev 62266)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.gradient
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/raster/r.gradient/r.gradient.html
===================================================================
--- grass-addons/grass7/raster/r.gradient/r.gradient.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.gradient/r.gradient.html	2014-10-16 08:30:55 UTC (rev 62266)
@@ -0,0 +1,40 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.gradient</em> create a gradient map. It is able to create
+horizontal, vertical and oblique gradient.
+
+<p>
+
+<h2>EXAMPLES</h2>
+To calculate vertical gradient from North to South
+<div class="code">
+  <pre>
+  r.gradient output=gradient_ns values=0,50 direction=N-S
+  </pre>
+</div>
+
+To calculate horizontal gradient from East to West
+<div class="code">
+  <pre>
+  r.gradient output=gradient_ea values=10,20 direction=E-W
+  </pre>
+</div>
+
+To calculate oblique gradient from North-East to South-West you have to
+set also the <em>percentile</em> option to set the slope of the gradient.
+
+<div class="code">
+  <pre>
+  r.gradient output=gradient_oblique values=10,20 direction=NE-SW
+  </pre>
+</div>
+
+<h2>AUTHOR</h2>
+
+Luca Delucchi, Fondazione E. Mach (Italy)
+<p>
+Thanks to Johannes Radinger for the code of horizontal and vertical gradient
+
+
+<p>
+<i>Last changed: $Date: 2014-05-22 08:52:36 +0200 (Thu, 22 May 2014) $</i>
\ No newline at end of file

Added: grass-addons/grass7/raster/r.gradient/r.gradient.py
===================================================================
--- grass-addons/grass7/raster/r.gradient/r.gradient.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.gradient/r.gradient.py	2014-10-16 08:30:55 UTC (rev 62266)
@@ -0,0 +1,147 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+############################################################################
+#
+# MODULE:        r.gradient
+# AUTHOR(S):     Luca Delucchi
+# PURPOSE:       r.gradient create a gradient map
+#
+# COPYRIGHT:        (C) 2014 by Luca Delucchi
+#
+#                This program is free software under the GNU General Public
+#                License (>=v2). Read the file COPYING that comes with GRASS
+#                for details.
+#
+#############################################################################
+
+#%module
+#% description: Create a gradient map
+#% keywords: raster
+#% keywords: gradient
+#%end
+
+#%option G_OPT_R_OUTPUT
+#%end
+
+#%option
+#% key: direction
+#% type: string
+#% label: The direction of gradient
+#% options: N-S, S-N, W-E, E-W, NW-SE, NE-SW
+#% required: yes
+#%end
+#%option
+#% key: range
+#% type: integer
+#% label: Minimum and maximum values of gradient
+#% required: yes
+#% multiple: yes
+#%end
+#%option
+#% key: percentile
+#% type: double
+#% label: Percentile to calculate (only for oblique gradient)
+#% options: 0-100
+#% required: no
+#%end
+
+
+# TODO add support for SW-NE, SE-NW direction
+
+# import library
+import sys
+import grass.script as grass
+from grass.pygrass.raster import RasterRow
+from grass.pygrass.raster.buffer import Buffer
+
+
+def calculateOblique(reg, mi, ma, perc):
+    """Calculate the oblique gradient"""
+    cols = reg['cols']
+    rows = reg['rows']
+    first_perc = ma * perc / 100.
+    dif_cols_first = (first_perc - mi) / float(cols-1)
+    dif_rows_first = (first_perc - mi) / float(rows-1)
+    dif_rows_last = (ma - first_perc) / float(rows-1)
+    matrix = []
+    for r in range(rows):
+        row = []
+        for c in range(cols):
+            if r == 0 and c == 0:
+                row.append(mi)
+            elif r == 0 and not c == 0:
+                val = row[c - 1] + dif_cols_first
+                row.append(val)
+            elif c == 0 and not r == 0:
+                val = matrix[r - 1][0] + dif_rows_first
+                row.append(val)
+            else:
+                val = row[c - 1] + dif_rows_last
+                row.append(val)
+        matrix.append(row)
+    return matrix
+
+
+def createRast(name, matrix, inverse=False):
+    """Create the new raster map using the output matrix of calculateOblique"""
+    newscratch = RasterRow(name)
+    newscratch.open('w', overwrite=True)
+    try:
+        for r in matrix:
+            if inverse:
+                r.reverse()
+            newrow = Buffer((len(r),), mtype='FCELL')
+            for c in range(len(r)):
+                newrow[c] = r[c]
+            newscratch.put_row(newrow)
+        newscratch.close()
+        return True
+    except:
+        return False
+
+
+def main():
+    """Main function"""
+    regiondict = grass.region()
+
+    output = options['output']
+    values = options['range'].split(',')
+    NewMin = values[0].strip()
+    NewMax = values[1].strip()
+    direction = options['direction']
+
+    # And now we can calculate the graded rasters
+    # for gradient of rows
+    if direction == 'N-S':
+        grass.mapcalc("$newmap = (((row() - $OldMin) * ($NewMax - $NewMin)) / "
+                      "($OldMax - $OldMin)) + $NewMin",
+                      newmap=output, NewMin=NewMin, NewMax=NewMax, OldMin=1,
+                      OldMax=regiondict["rows"], overwrite=True)
+    elif direction == 'S-N':
+        grass.mapcalc("$newmap = (((row() - $OldMin) * ($NewMax - $NewMin)) / "
+                      "($OldMax - $OldMin)) + $NewMin",
+                      newmap=output, NewMin=NewMax, NewMax=NewMin, OldMin=1,
+                      OldMax=regiondict["rows"], overwrite=True)
+    elif direction == 'W-E':
+        grass.mapcalc("$newmap = (((col() - $OldMin) * ($NewMax - $NewMin)) / "
+                      "($OldMax - $OldMin)) + $NewMin",
+                      newmap=output, NewMin=NewMin, NewMax=NewMax, OldMin=1,
+                      OldMax=regiondict["cols"], overwrite=True)
+    elif direction == 'E-W':
+        grass.mapcalc("$newmap = (((col() - $OldMin) * ($NewMax - $NewMin)) / "
+                      "($OldMax - $OldMin)) + $NewMin",
+                      newmap=output, NewMin=NewMax, NewMax=NewMin, OldMin=1,
+                      OldMax=regiondict["cols"], overwrite=True)
+    elif direction == 'NW-SE':
+        mat = calculateOblique(regiondict, NewMin, NewMax)
+        createRast(output, mat)
+    elif direction == 'NE-SW':
+        mat = calculateOblique(regiondict, NewMin, NewMax)
+        createRast(output, mat, True)
+
+
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())



More information about the grass-commit mailing list