[GRASS-SVN] r49844 - grass-addons/grass6/raster/mcda/r.mcda.ahp

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Dec 20 06:24:33 EST 2011


Author: gianluca
Date: 2011-12-20 03:24:33 -0800 (Tue, 20 Dec 2011)
New Revision: 49844

Added:
   grass-addons/grass6/raster/mcda/r.mcda.ahp/Makefile
   grass-addons/grass6/raster/mcda/r.mcda.ahp/description.html
   grass-addons/grass6/raster/mcda/r.mcda.ahp/r.mcda.ahp.py
Log:


Added: grass-addons/grass6/raster/mcda/r.mcda.ahp/Makefile
===================================================================
--- grass-addons/grass6/raster/mcda/r.mcda.ahp/Makefile	                        (rev 0)
+++ grass-addons/grass6/raster/mcda/r.mcda.ahp/Makefile	2011-12-20 11:24:33 UTC (rev 49844)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.mcda.ahp
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass6/raster/mcda/r.mcda.ahp/description.html
===================================================================
--- grass-addons/grass6/raster/mcda/r.mcda.ahp/description.html	                        (rev 0)
+++ grass-addons/grass6/raster/mcda/r.mcda.ahp/description.html	2011-12-20 11:24:33 UTC (rev 49844)
@@ -0,0 +1,34 @@
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
+</head>
+<body>
+<h2>DESCRIPTION</h2>
+<p>The <em>r.mcda.ahp</em> Generate a raster map classified with analytic hierarchy process (AHP) [Saaty, 1977 and Saaty &amp; Vargas, 1991]</p>
+<em>(GRASS python Script)</em>
+<p></p>
+<h2>NOTES</h2>
+<p>It is mandatory to build a pairwise comparation table with the same order of input of criteria maps in the criteria field.</p>
+<p>Example: r.mcda.ahp criteria=reclass_slope,reclass_sand,reclass_elev pairwise=pairwise output=outputMap</p>
+<p>The file &quot;pairwise&quot; has to have a structure like this:</p>
+<p>#start file</p>
+<p>1.0, 0.2, 3.0</p>
+<p>5.0, 1.0, 5.0</p>
+<p>0.3, 0.2, 1.0</p>
+<p>#comment: order: reclass_slope,reclass_sand,reclass_</p>
+<p>#end file</p>
+The first row and first column are related to the first criteria (reclass_slope in our case); the second row and second column are related to the second criteria (reclass_sand in our case) and the third row and third column are related to the third criteria
+ ( reclass_elev in our cas), and so on.
+<h2>TODO</h2>
+<h2>SEE ALSO</h2>
+<em><a href="UrlBlockedError.aspx" target="_blank">r.roughset</a>, <a href="UrlBlockedError.aspx" target="_blank">
+r.mcda.regime</a>, <a href="UrlBlockedError.aspx" target="_blank">r.mcda.fuzzy</a>
+<a href="UrlBlockedError.aspx" target="_blank">r.mcda.electre</a>, <a href="UrlBlockedError.aspx" target="_blank">
+r.in.drsa</a> <a href="UrlBlockedError.aspx" target="_blank">r.in.drsa</a> <a href="UrlBlockedError.aspx" target="_blank">
+r.in.drsa</a> </em>
+<h2>AUTHORS</h2>
+Antonio Boggia - Gianluca Massei<br>
+Department of Economics and Appraisal - University of Perugia - Italy
+<p><i>Last changed: $Date: 2011-12-15 22:56:45 &#43;0100 ( 15 dic 2011) $</i> </p>
+</body>
+</html>

Added: grass-addons/grass6/raster/mcda/r.mcda.ahp/r.mcda.ahp.py
===================================================================
--- grass-addons/grass6/raster/mcda/r.mcda.ahp/r.mcda.ahp.py	                        (rev 0)
+++ grass-addons/grass6/raster/mcda/r.mcda.ahp/r.mcda.ahp.py	2011-12-20 11:24:33 UTC (rev 49844)
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:       r.in.drsa
+# AUTHOR:       Gianluca Massei - Antonio Boggia
+# PURPOSE:      Generate a raster map classified with  analytic hierarchy process (AHP) [Saaty, 1977 and Saaty & Vargas, 1991]
+# 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: Generate a raster map classified with analytic hierarchy process (AHP).
+#% keywords: raster, analytic hierarchy process (AHP), Multi Criteria Decision Analysis (MCDA)
+#%End
+#%option
+#% key: criteria
+#% type: string
+#% multiple: yes
+#% gisprompt: old,cell,raster
+#% key_desc: name
+#% description: Name of criteria raster maps 
+#% required: yes
+#%end
+#%option
+#% key: pairwise
+#% type: string
+#% gisprompt: old,file,input
+#% description: Pairwise comparison matrix
+#% required: yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new_file,cell,output
+#% description: output classified raster map
+#% required: yes
+#%end
+#%flag
+#% key:k 
+#% description:build a void pairwise comparison matrix and exit (no yet implemented)
+#%end
+
+import sys
+import grass.script as grass
+import numpy as np
+
+def calculateWeight(pairwise):
+    "Define vector of weight based on eigenvector and eigenvalues"
+    pairwise=np.genfromtxt(pairwise, delimiter=",")
+    eigenvalues, eigenvector=np.linalg.eig(pairwise)
+    maxindex=np.argmax(eigenvalues)
+    eigenvalues=np.float32(eigenvalues)
+    eigenvector=np.float32(eigenvector)
+    weight=eigenvector[:, maxindex] #extract vector from eigenvector with max vaue in eigenvalues
+    weight.tolist() #convert array(numpy)  to vector
+    weight=[ w/sum(weight) for w in weight ]
+    return weight, eigenvalues,  eigenvector
+    
+def calculateMap(criteria, weight, outputMap):
+    "Parser a formula for mapcalc and run grass.mapcalc"
+    formula=''
+    for i in range(len(criteria)-1):
+        formula += "%s*%s + " % (criteria[i], weight[i])
+    formula +="%s*%s " % (criteria[len(criteria)-1], weight[len(criteria)-1])
+    grass.mapcalc(outputMap +"=" +formula)
+    return 0
+    
+def Consistency(weight,eigenvalues):
+    "Calculete Consistency index in accord with Saaty (1977)"
+    RI=[0.00, 0.00, 0.00,0.52,0.90,1.12,1.24,1.32,1.41]     #order of matrix: 0,1,2,3,4,5,6,7,8
+    order=len(weight)
+    CI= (MaxEigenvalue-order)/(lorder-1)
+    return CI/RI[order-1]
+    
+def ReportLog():
+    log=open("log.txt", "w")
+    log.write("eigenvalues:\n%s" % eigenvalues)
+    log.write("\neigenvector:\n%s" % eigenvector)
+    log.write("\nweight:\n%s" % weight)
+    log.write("\nconsistency:\n%s" % consistency)
+    log.close()
+    return 0
+    
+def main():
+    "main"
+    criteria = options['criteria'].split(',')
+    pairwise = options['pairwise']
+    outputMap = options['output']
+    gregion = grass.region()
+    nrows = gregion['rows']
+    ncols = gregion['cols']
+    ewres=int(gregion['ewres'])
+    nsres=int(gregion['nsres'])
+    weight = calculateWeight(pairwise)
+    calculateMap(criteria, weight, outputMap)
+    
+    
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())
+
+



More information about the grass-commit mailing list