[GRASS-SVN] r33593 - in grass/trunk/scripts: g.manual r.reclass.area

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 29 10:21:41 EDT 2008


Author: glynn
Date: 2008-09-29 10:21:41 -0400 (Mon, 29 Sep 2008)
New Revision: 33593

Added:
   grass/trunk/scripts/g.manual/g.manual.py
   grass/trunk/scripts/r.reclass.area/r.reclass.area.py
Log:
Convert g.manual, r.reclass.area to Python


Added: grass/trunk/scripts/g.manual/g.manual.py
===================================================================
--- grass/trunk/scripts/g.manual/g.manual.py	                        (rev 0)
+++ grass/trunk/scripts/g.manual/g.manual.py	2008-09-29 14:21:41 UTC (rev 33593)
@@ -0,0 +1,99 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:	g.manual
+# AUTHOR(S):	Markus Neteler
+#               Converted to Python by Glynn Clements
+# PURPOSE:	Display the HTML/MAN pages
+# COPYRIGHT:	(C) 2003,2008 by the GRASS Development Team
+#
+#		This program is free software under the GNU General Public
+#		License (>=v2). Read the file COPYING that comes with GRASS
+#		for details.
+#
+#############################################################################
+
+#%Module
+#%  description: Display the HTML man pages of GRASS
+#%  keywords: general, manual, help
+#%End
+#%flag
+#%  key: i
+#%  description: Display index
+#%End
+#%flag
+#%  key: m
+#%  description: Display as MAN text page instead of HTML page in browser
+#%End
+#%option
+#% key: entry
+#% type: string
+#% description: Manual entry to be displayed
+#% required : no
+#%End
+
+import sys
+import os
+import grass
+
+def start_browser(entry):
+    path = os.path.join(gisbase, 'docs', 'html', entry + '.html')
+    if not os.path.exists(path):
+	grass.fatal("No HTML manual page entry for <%s>." % entry)
+    verbose = os.getenv("GRASS_VERBOSE")
+    if not verbose or int(verbose) > 1:
+	grass.message("Starting browser <%s> for module %s..." % (browser_name, entry))
+    os.execlp(browser, browser_name, "file://%s/docs/html/%s.html" % (gisbase, entry))
+    grass.fatal("Error starting browser <%s> for HTML file <%s>" % (browser, entry))
+
+def start_man(entry):
+    path = os.path.join(gisbase, 'man', 'man1', entry + '.1')
+    for ext in ['', '.gz', '.bz2']:
+	if os.path.exists(path + ext):
+	    os.execlp('man', 'man', path + ext)
+	    grass.fatal("Error starting 'man' for <%s>" % path)
+    grass.fatal("No manual page entry for <%s>." % entry)
+
+def main():
+    global gisbase, browser, browser_name
+
+    index = flags['i']
+    manual = flags['m']
+    entry = options['entry']
+
+    gisbase = os.environ['GISBASE']
+
+    browser = os.getenv('GRASS_HTML_BROWSER')
+
+    if "html_browser_mac.sh" in browser:
+        #hack for MacOSX:
+	browser_name = os.getenv('GRASS_HTML_BROWSER_MACOSX','..').split('.')[2]
+    elif os.getenv('OSTYPE') == "cygwin":
+        #hack for Cygwin:
+	browser_name = grass.basename(browser, 'exe')
+    else:
+	browser_name = grass.basename(browser)
+
+    #keep order!
+    #first test for index...
+    if index:
+	if manual:
+	    start_man('index')
+	else:
+	    start_browser('index')
+	sys.exit(0)
+
+    #... then for help parameter:
+    if not entry:
+	grass.run_command('g.manual', '--help')
+	sys.exit(0)
+
+    if manual:
+	start_man(entry)
+    else:
+	start_browser(entry)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


Property changes on: grass/trunk/scripts/g.manual/g.manual.py
___________________________________________________________________
Name: svn:executable
   + *

Added: grass/trunk/scripts/r.reclass.area/r.reclass.area.py
===================================================================
--- grass/trunk/scripts/r.reclass.area/r.reclass.area.py	                        (rev 0)
+++ grass/trunk/scripts/r.reclass.area/r.reclass.area.py	2008-09-29 14:21:41 UTC (rev 33593)
@@ -0,0 +1,121 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       r.reclass.area
+# AUTHOR(S):    NRCS
+#               Converted to Python by Glynn Clements
+# PURPOSE:      Reclasses a raster map greater or less than user specified area size (in hectares)
+# COPYRIGHT:    (C) 1999,2008 by the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+# 3/2007: added label support MN
+# 3/2004: added parser support MN
+# 11/2001 added mapset support markus
+# 2/2001 fixes markus
+# 2000: updated to GRASS 5
+# 1998 from NRCS, slightly modified for GRASS 4.2.1
+
+#%Module
+#%  description: Reclasses a raster map greater or less than user specified area size (in hectares)
+#%  keywords: raster, statistics, aggregation
+#%End
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: raster input map
+#% required : yes
+#%END
+#%option
+#% key: lesser
+#% type: double
+#% description: lesser val option that sets the <= area size limit [hectares]
+#%END
+#%option
+#% key: greater
+#% type: double
+#% description: greater val option that sets the >= area size limit [hectares]
+#%END
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,cell,raster
+#% description: reclass raster output map
+#% required : yes
+#%END
+
+import sys
+import os
+import grass
+
+def main():
+    infile = options['input']
+    lesser = options['lesser']
+    greater = options['greater']
+    outfile = options['output']
+
+    s = grass.read_command("g.region", flags = 'p')
+    kv = grass.parse_key_val(s, sep = ':')
+    s = kv['projection'].strip().split()
+    if s == '0':
+	grass.fatal("xy-locations are not supported.")
+	grass.fatal("Need projected data with grids in meters.")
+
+    if not lesser and not greater:
+	grass.fatal("you have to specify either lesser= or greater=")
+    if lesser and greater:
+	grass.fatal("lesser= and greater= are mutually exclusive")
+    if lesser:
+	limit = float(lesser)
+    if greater:
+	limit = float(greater)
+
+    if not grass.find_file(infile)['name']:
+	grass.fatal("Raster map <%s> does not exist." % infile)
+
+    clumpfile = "%s.clump.%s" % (infile.split('@')[0], outfile)
+
+    if not grass.overwrite():
+	if grass.find_file(clumpfile)['name']:
+	    grass.fatal("Temporary raster map <%s> exists." % clumpfile)
+
+    grass.message("Generating a clumped raster file ...")
+    grass.run_command('r.clump', input = infile, output = clumpfile)
+
+    if lesser:
+	grass.message("Generating a reclass map with area size less than or equal to %f hectares" % limit)
+    else:
+	grass.message("Generating a reclass map with area size greater than or equal to %f hectares" % limit)
+
+    recfile = outfile + '.recl'
+
+    p1 = grass.pipe_command('r.stats', flags = 'aln', input = (clumpfile, infile), fs = '|')
+    p2 = grass.feed_command('r.reclass', input = clumpfile, output = recfile)
+    for line in p1.stdout:
+	f = line.rstrip('\r\n').split('|')
+	if len(f) < 5:
+	    continue
+	hectares = float(f[4]) * 0.0001
+	if lesser:
+	    test = hectares <= limit
+	else:
+	    test = hectares >= limit
+	if test:
+	    p2.stdin.write("%s = %s %s\n" % (f[0], f[2], f[3]))
+    p1.wait()
+    p2.stdin.close()
+    p2.wait()
+
+    grass.message("Written: %s" % outfile)
+
+    grass.run_command('r.mapcalc', expression = "%s = %s" % (outfile, recfile))
+    grass.run_command('g.remove', rast = [recfile, clumpfile], quiet = True)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


Property changes on: grass/trunk/scripts/r.reclass.area/r.reclass.area.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the grass-commit mailing list