[GRASS-SVN] r63513 - in grass-addons/grass7/raster: . r.recode.attr

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Dec 12 13:11:23 PST 2014


Author: pvanbosgeo
Date: 2014-12-12 13:11:22 -0800 (Fri, 12 Dec 2014)
New Revision: 63513

Added:
   grass-addons/grass7/raster/r.recode.attr/
   grass-addons/grass7/raster/r.recode.attr/Makefile
   grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html
   grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py
Log:
recode rasters based on values in user-defined csv (text) file

Added: grass-addons/grass7/raster/r.recode.attr/Makefile
===================================================================
--- grass-addons/grass7/raster/r.recode.attr/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.recode.attr/Makefile	2014-12-12 21:11:22 UTC (rev 63513)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.recode.attr
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/grass7/raster/r.recode.attr/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html
===================================================================
--- grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html	2014-12-12 21:11:22 UTC (rev 63513)
@@ -0,0 +1,14 @@
+
+<h2>DESCRIPTION</h2>
+
+<em>r.recode.attr</em> is a wrapper for r.recode which makes it easier to recode a raster layer based on values in a csv table. The csv file should include at least two columns. The first column should correspond to (part of) the raster values. The other columns should hold the reclassification values. 
+
+<p>For each column in the csv file (except the first one) a new map will be created. This is done by creating recode rules as follows <i>old:old:new</i>. See the help file for <a href="http://grass.osgeo.org/grass70/manuals/r.recode.html">r.recode</a> for more information.
+
+<p>The user can define the names of the output map(s). If only one output name is provided and the rules file contains more than two columns, the function will create output names by appending for each recode layer the corresponding column name to the output name provided by the user. 
+ 
+<h2>AUTHOR</h2>
+
+Paulo van Breugel, paulo at ecodiv.org
+
+<i>Last changed: $Date$</i>


Property changes on: grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py
===================================================================
--- grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py	2014-12-12 21:11:22 UTC (rev 63513)
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+########################################################################
+# 
+# MODULE:       r.recode_attribute
+# AUTHOR(S):    Paulo van Breugel <p.vanbreugel AT gmail.com>
+# PURPOSE:      Recode raster using attribute table (csv file) as input,
+#               using the reclass sheme old:old:new.               
+#               The first column of the table in the csv file is taken 
+#               to be the original value, the next columns are assumed
+#               to be the reclass values. The name of the new layer(s)
+#               are the output + column_name          
+#
+# COPYRIGHT: (C) 2014 Paulo van Breugel
+#            http://ecodiv.org
+#            http://pvanb.wordpress.com/
+# 
+#            This program is free software under the GNU General Public 
+#            License (>=v2). Read the file COPYING that comes with GRASS 
+#            for details. 
+# 
+########################################################################
+#
+#%Module 
+#% description: Recode raster using attribute table (csv file) as input
+#%End 
+
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Input map
+#% key_desc: name
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: basename output layer(s)
+#% key_desc: name
+#% required: yes
+#% multiple: no
+#%end
+
+#%option G_OPT_F_INPUT
+#% key: rules
+#% label: Full path to rules file
+#% required: yes
+#%end
+
+# import libraries
+import os
+import sys
+import numpy as np
+import grass.script as grass
+
+def cleanup():
+	grass.run_command('g.remove', 
+      type = 'rast', 
+      pattern = 'tmp_map',
+      flags='f',
+      quiet = True)
+
+# main function
+def main():
+    
+    # check if GISBASE is set
+    if "GISBASE" not in os.environ:
+    # return an error advice
+       grass.fatal(_("You must be in GRASS GIS to run this program"))
+       
+    # input raster map and parameters   
+    inputmap = options['input']
+    outBase = options['output']
+    rules = options['rules']
+    outNames = outBase.split(',')
+    lengthNames = len(outNames)
+    
+    # Get attribute data
+    myData = np.genfromtxt(rules, delimiter=',', skip_header=1)
+    nmsData = np.genfromtxt(rules, delimiter=',', names=True)
+    dimData = myData.shape
+    nmsData = nmsData.dtype.names
+          
+    # Create recode maps
+    numVar = xrange(dimData[1]-1)
+    for x in numVar:
+        y = x + 1
+        myRecode = np.column_stack((myData[:,0], myData[:,0], myData[:,y]))
+        np.savetxt('.numpy_grass_recode', myRecode, delimiter=":") 
+        
+        if len(numVar) == lengthNames:
+            nmOutput = outNames[x]
+        else:
+            nmOutput = outNames[0] + '_' + nmsData[y]
+            
+        grass.run_command('r.recode',
+                input = inputmap, 
+                output = nmOutput,
+                rules = '.numpy_grass_recode')
+        
+        os.remove('.numpy_grass_recode') 
+
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())
+


Property changes on: grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list