[GRASS-SVN] r44524 - in grass-addons/raster: . r.diversity

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Dec 2 09:52:30 EST 2010


Author: lucadelu
Date: 2010-12-02 06:52:30 -0800 (Thu, 02 Dec 2010)
New Revision: 44524

Added:
   grass-addons/raster/r.diversity/
   grass-addons/raster/r.diversity/Makefile
   grass-addons/raster/r.diversity/description.html
   grass-addons/raster/r.diversity/r.diversity.py
Log:
new module to calculate diversity indices

Added: grass-addons/raster/r.diversity/Makefile
===================================================================
--- grass-addons/raster/r.diversity/Makefile	                        (rev 0)
+++ grass-addons/raster/r.diversity/Makefile	2010-12-02 14:52:30 UTC (rev 44524)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.diversity
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/raster/r.diversity/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/raster/r.diversity/description.html
===================================================================
--- grass-addons/raster/r.diversity/description.html	                        (rev 0)
+++ grass-addons/raster/r.diversity/description.html	2010-12-02 14:52:30 UTC (rev 44524)
@@ -0,0 +1,47 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.diversity</em> calculates selected diversity indices by calling various
+<em>r.li</em> commands.
+<p>
+This script uses the Pielou, Renyi, Shannon and Simpson indices. The output is a
+map for each index.
+
+<h2>NOTES</h2>
+The user does not need to create a "conf" file with <em>r.li.setup</em> because this file 
+will be created automatically by the script.<br> 
+If the input raster contains NULL value cells, <em>r.diversity</em> 
+returns -1 for these cells.<br> 
+If the user wants to keep NULL values instead, run subsequently on the resulting map:
+<br> 
+<div class="code"><pre> 
+r.null setnull=-1 map=my_map
+</pre></div> 
+
+<h2>EXAMPLES</h2>
+
+To calculate the set of indices from a NDVI map, with a moving window of 3 x 3 pixel, run:
+<div class="code"><pre>
+r.diversity input=ndvi_map out=test alpha=0.5
+</pre></div>
+
+To calculate the set of indices from a NDVI map, with a moving window of 7 x 7 pixel, run:
+<div class="code"><pre>
+r.diversity input=ndvi_map out=test alpha=0.5 size=7
+</pre></div>
+
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.li.html">r.li</a>,
+<a href="r.li.pielou.html">r.li.pielou</a>,
+<a href="r.li.renyi.html">r.li.renyi</a>,
+<a href="r.li.shannon.html">r.li.shannon</a>,
+<a href="r.li.simpson.html">r.li.simpson</a>
+</em> 
+
+<h2>AUTHOR</h2>
+
+Luca Delucchi and Duccio Rocchini, Fondazione E. Mach (Italy)
+
+<p><i>Last changed: $Date$</i>


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

Added: grass-addons/raster/r.diversity/r.diversity.py
===================================================================
--- grass-addons/raster/r.diversity/r.diversity.py	                        (rev 0)
+++ grass-addons/raster/r.diversity/r.diversity.py	2010-12-02 14:52:30 UTC (rev 44524)
@@ -0,0 +1,152 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+############################################################################
+#
+# MODULE:	r.diversity
+# AUTHOR(S):	Luca Delucchi
+# PURPOSE:	It calculates the mostly used indices of diversity based on 
+#		information theory
+#
+# COPYRIGHT:	(C) 2010 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: Calculate some vegetation index using r.li packages and moving windows
+#% keywords: raster
+#%end
+#%option
+#% key: input
+#% type: string
+#% gisprompt: input raster
+#% key_desc: name
+#% description: Name of input raster map 
+#% required: yes
+#%end 
+#%option
+#% key: output
+#% type: string
+#% gisprompt: output raster
+#% key_desc: name
+#% description: Name of output raster map 
+#% required: yes
+#%end 
+#%option
+#% key: alpha
+#% type: double
+#% gisprompt: alpha value
+#% key_desc: alpha value for Renyi entropy
+#% description: Alpha value is the order of the generalized entropy, it'll be > 0 and != 1
+#% required: yes
+#%end
+#%option
+#% key: size
+#% type: integer
+#% gisprompt: resolution
+#% key_desc: moving window
+#% description: Number of pixel used for the moving window, it must be odd number, if not set the module set your resolution to value 3 
+#% required: no
+#%end 
+ 
+
+# import library
+import os, sys, re
+import grass.script as grass
+
+# main function
+def main():
+    # set the home path
+    home=os.path.expanduser('~')
+    # set the name of conf file
+    confilename = homePath+'/.r.li/history/conf_diversity'
+    # check if GISBASE is set
+    if "GISBASE" not in os.environ:
+	# return an error advice
+	print "You must be in GRASS GIS to run this program."
+	sys.exit(1)
+
+    # input raster map
+    map_in = options['input']
+    # output raster map
+    map_out = options['output']
+    # resolution of moving windows
+    res = options['size']
+    # alpha value for r.renyi
+    alpha_value = options['alpha']
+    # resolution
+    if res=='':
+	res=3
+    else:
+	res = int(options['size'])
+
+    # check if is a odd number
+    if res%2==0:
+	# return the error advice
+	print "Your size option must be an odd number"
+	sys.exit(1)
+    # check if ~/.r.li path exists
+    if os.path.exists(home+'/.r.li/'):
+	# check if ~/.r.li/history path exists
+	if os.path.exists(home+'/.r.li/history'):
+	    # create configuration file
+	    createConfFile(res,map_in,home)
+	else:
+	    # create ~/.r.li/history path
+	    os.path.mkdir(home+'/.r.li/history')
+	    # create configuration file
+	    createConfFile(res,map_in,home)
+    else:
+	# create ~/.r.li
+	os.path.mkdir(home+'/.r.li/')
+	# create ~/.r.li/history
+	os.path.mkdir(home+'/.r.li/history')
+	# create configuration file
+	createConfFile(res,map_in,home)
+
+
+    ### calculate r.li indices
+    # if overwrite it is set
+    if grass.overwrite():
+        env['GRASS_OVERWRITE'] = '1'
+
+    simpson = grass.run_command('r.li.simpson', map = map_in, out = map_out + '_simpson', conf = 'conf_diversity')
+    shannon = grass.run_command('r.li.shannon', map = map_in, out = map_out+ '_shannon', conf = 'conf_diversity')
+    pielou = grass.run_command('r.li.pielou', map = map_in, out = map_out+ '_pielou', conf = 'conf_diversity')
+    renyi = grass.run_command('r.li.renyi', map = map_in, out = map_out+ '_renyi', conf = 'conf_diversity', alpha = alpha_value)
+    
+    os.remove(confilename)
+    print 'All works are terminated'
+
+#create configuration file instead using r.li.setup
+def createConfFile(res,inpumap,homePath):
+    # start the text for the conf file
+    outputLine = ['SAMPLINGFRAME 0|0|1|1\n']
+    # return r.info about input file
+    rinfo = grass.raster_info(inpumap)
+    # calculate number of lines
+    rows = (rinfo["north"]-rinfo["south"])/rinfo['nsres']
+    # calculate number of columns
+    columns = (rinfo['east']-rinfo['west'])/rinfo['ewres']
+    # value for row
+    rV = res/rows
+    # value for column
+    cV = res/columns
+    # append the text for the conf file
+    outputLine.append('SAMPLEAREA -1|-1|'+str(rV)+'|'+str(cV)+'\n')
+    outputLine.append('MOVINGWINDOW\n')
+    # open configuration file
+    fileConf=open(confilename,'w')
+    # write file 
+    fileConf.writelines(outputLine)
+    # close file
+    fileConf.close()
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())
+



More information about the grass-commit mailing list