[GRASS-SVN] r46480 - grass-addons/raster/mcda/r.to.drsa

svn_grass at osgeo.org svn_grass at osgeo.org
Tue May 31 18:03:10 EDT 2011


Author: gianluca
Date: 2011-05-31 15:03:10 -0700 (Tue, 31 May 2011)
New Revision: 46480

Added:
   grass-addons/raster/mcda/r.to.drsa/r.to.drsa.py
Log:


Added: grass-addons/raster/mcda/r.to.drsa/r.to.drsa.py
===================================================================
--- grass-addons/raster/mcda/r.to.drsa/r.to.drsa.py	                        (rev 0)
+++ grass-addons/raster/mcda/r.to.drsa/r.to.drsa.py	2011-05-31 22:03:10 UTC (rev 46480)
@@ -0,0 +1,127 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:       r.to.drsa
+# AUTHOR:       Gianluca Massei - Antonio Boggia
+# PURPOSE:      Export criteria raster maps and decision raster map in a *.isf
+#               file for dominance rough set approach analysis (DRSA)
+#               Dominance Rough Set Analysis (e.g. 4eMka2,JAMM, jMAF).
+# COPYRIGHT:    c) 2010 Gianluca Massei, Antonio Boggia  and the GRASS 
+#               Development Team. This program is free software under the 
+#               GNU General PublicLicense (>=v2). Read the file COPYING 
+#               that comes with GRASS for details.
+#
+#############################################################################
+
+#%Module
+#% description: Export criteria raster maps and decision raster map in a *.isf file (e.g. 4eMka2, jMAF) for dominance rough set approach analysis
+#% keywords: raster, Dominance Rough Set Approach
+#% keywords: Multi Criteria Decision Analysis (MCDA)
+#%End
+#%option
+#% key: attributes
+#% type: string
+#% multiple: yes
+#% gisprompt: old,cell,raster
+#% key_desc: name
+#% description: Name of criteria raster maps 
+#% required: yes
+#%end
+#%option
+#% key: preferences
+#% type: string
+#% key_desc: character
+#% description: gain,cost,none
+#% required: yes
+#%end
+#%option
+#% key: decision
+#% type: string
+#% gisprompt: old,cell,raster
+#% key_desc: name
+#% description: Name of decision raster map 
+#% required: yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new_file,file,output
+#% key_desc: name
+#% description: Name for output file (*.isf file, Information System)
+#% answer:infosys.isf
+#% required: yes
+#%end
+
+
+import sys
+##from grass.script import core as grass
+import grass.script as grass
+
+def main():
+    attributes = options['attributes'].split(',')
+    preferences=options['preferences'].split(',')
+    decision=options['decision']
+    output = options['output']
+
+    gregion = grass.region()
+    nrows = gregion['rows']
+    ncols = gregion['cols']
+    ewres=int(gregion['ewres'])
+    nsres=int(gregion['nsres'])
+    print nrows, ncols, ewres,nsres
+
+    outf = file(output,"w")
+    outf.write("**ATTRIBUTES\n")
+    for i in range(len(attributes)):
+        outf.write("+ %s: (continuous)\n" % attributes[i])
+    outf.write("+ %s: [" % decision)
+    value=[]
+    value=grass.read_command("r.describe", flags = "1n", map = decision)
+    v=value.split()
+
+    for i in range(len(v)-1):
+        outf.write("%s, " % str(v[i]))
+    outf.write("%s]\n" % str(v[len(v)-1]))
+    outf.write("decision: %s\n" % decision)
+
+    outf.write("\n**PREFERENCES\n")
+    for i in range(len(attributes)):
+        if(preferences[i]==""):
+            preferences[i]="none"
+        outf.write("%s: %s\n" % (attributes[i], preferences[i]))
+    outf.write("%s: gain\n" % decision)
+    
+    
+    outf.write("\n**EXAMPLES\n")
+    examples=[]
+    MATRIX=[]
+    for i in range(len(attributes)):
+        grass.mapcalc("rast=if(isnull(${decision})==0,${attribute},null())",
+                         rast="rast",
+                         decision=decision,
+                         attribute=attributes[i])
+        tmp=grass.read_command("r.stats", flags = "1n", nv="?", input = "rast")
+        example=tmp.split()
+        examples.append(example)
+    tmp=grass.read_command("r.stats", flags = "1n", nv="?", input = decision)
+    example=tmp.split()
+
+    examples.append(example)
+    MATRIX=map(list,zip(*examples))
+
+    MATRIX=[r for r in MATRIX if not '?' in r] #remove all rows with almost one "?"
+    MATRIX=[list(i) for i in set(tuple(j) for j in MATRIX)] #remove duplicate example 
+                
+    print "rows:%d - col:%d" %(len(MATRIX),len(MATRIX[0]))
+    for r in range(len(MATRIX)):
+        for c in range(len(MATRIX[0])):
+            outf.write("%s " %  str(MATRIX[r][c]))
+        outf.write("\n")
+       
+    outf.write("**END")
+    outf.close()
+
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())


Property changes on: grass-addons/raster/mcda/r.to.drsa/r.to.drsa.py
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list