[GRASS-SVN] r64068 - in grass-addons/grass7/raster: r.mess r.random.weight r.recode.attr
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Jan 11 15:31:14 PST 2015
Author: pvanbosgeo
Date: 2015-01-11 15:31:14 -0800 (Sun, 11 Jan 2015)
New Revision: 64068
Modified:
grass-addons/grass7/raster/r.mess/r.mess.py
grass-addons/grass7/raster/r.random.weight/r.random.weight.py
grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html
grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py
Log:
small improvements (hopefully)
Modified: grass-addons/grass7/raster/r.mess/r.mess.py
===================================================================
--- grass-addons/grass7/raster/r.mess/r.mess.py 2015-01-11 22:32:27 UTC (rev 64067)
+++ grass-addons/grass7/raster/r.mess/r.mess.py 2015-01-11 23:31:14 UTC (rev 64068)
@@ -149,6 +149,12 @@
grass.run_command("g.remove",
type="rast", name = rast, quiet = True)
+def CheckLayer(envlay):
+ for chl in xrange(len(envlay)):
+ ffile = grass.find_file(envlay[chl], element = 'cell')
+ if ffile['fullname'] == '':
+ grass.fatal("The layer " + envlay[chl] + " does not exist.")
+
# main function
def main():
@@ -182,6 +188,7 @@
# old environmental layers & variable names
ipl = options['env_old']
ipl = ipl.split(',')
+ CheckLayer(ipl)
ipn = [z.split('@')[0] for z in ipl]
ipn = [x.lower() for x in ipn]
@@ -193,6 +200,7 @@
else:
ipl_dif = True
ipl2 = ipl2.split(',')
+ CheckLayer(ipl2)
if len(ipl2) != len(ipl) and len(ipl2) != 0:
grass.fatal('number of old and new environmental variables is not the same')
Modified: grass-addons/grass7/raster/r.random.weight/r.random.weight.py
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight.py 2015-01-11 22:32:27 UTC (rev 64067)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.py 2015-01-11 23:31:14 UTC (rev 64068)
@@ -4,7 +4,7 @@
##############################################################################
#
# MODULE: r.rand.weight
-# AUTHOR(S): paulo van Breugel <paulo at ecodiv.org>
+# AUTHOR(S): paulo van Breugel <paulo at ecodiv.org>
# PURPOSE: Create a layer with weighted random sample
# COPYRIGHT: (C) 2014 Paulo van Breugel
# http://ecodiv.org
@@ -87,23 +87,23 @@
import grass.script as grass
# Runs modules silently
-os.environ['GRASS_VERBOSE']='-1'
+os.environ['GRASS_VERBOSE']='-1'
clean_rast = set()
def cleanup():
for rast in clean_rast:
- grass.run_command("g.remove",
+ grass.run_command("g.remove",
type="rast", name = rast, 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
+ grass.fatal(_("You must be in GRASS GIS to run this program"))
+
+ # input raster map and parameters
minval = options['start']
maxval = options['end']
weight = options['weights']
@@ -111,29 +111,29 @@
subsample = options['subsample']
seed = options['seed']
flag_n = flags['n']
-
+
# setup temporary files and seed
tmp_map = "r_w_rand_" + str(uuid.uuid4())
tmp_map = string.replace(tmp_map, '-', '_')
-
+
# Compute minimum and maximum value raster
- minmax = grass.parse_command('r.univar',
+ minmax = grass.parse_command('r.univar',
map = weight,
flags='g')
-
- if seed == "auto":
- grass.mapcalc("$tmp_map = rand(float(${minval}),float(${maxval}))",
+
+ if seed == "auto":
+ grass.mapcalc("$tmp_map = rand(float(${minval}),float(${maxval}))",
seed='auto',
- minval = minval,
+ minval = minval,
maxval = maxval,
tmp_map = tmp_map)
- else:
- grass.mapcalc("$tmp_map = rand(float(${minval}),float(${maxval}))",
+ else:
+ grass.mapcalc("$tmp_map = rand(float(${minval}),float(${maxval}))",
seed=int(seed),
- minval = minval,
+ minval = minval,
maxval = maxval,
tmp_map = tmp_map)
- clean_rast.add(tmp_map)
+ clean_rast.add(tmp_map)
if flag_n:
grass.mapcalc("${outmap} = if($tmp_map <= ${weight},1,0)",
@@ -145,11 +145,11 @@
weight = weight,
outmap = outmap,
tmp_map = tmp_map)
-
- grass.run_command("g.remove", quiet=True, flags="f", type="raster", name=tmp_map)
- if not subsample == '':
+
+ grass.run_command("g.remove", quiet=True, flags="f", type="raster", name=tmp_map)
+ if not subsample == '':
grass.run_command('r.null',
- map = outmap,
+ map = outmap,
setnull = 0)
grass.run_command('r.random',
input = outmap,
@@ -158,17 +158,16 @@
overwrite=True)
if flag_n:
grass.run_command('r.null',
- map = outmap,
+ map = outmap,
null = 0)
- print("------------------")
- print("Ready!")
- print("The name of raster created is " + outmap)
- if minval > minmax['min'] or maxval < minmax['max']:
- print("Warning!")
- print("You defined the minimum and maximum weights as: " + minval + " & " + maxval)
- print("Value range of weight raster is: " + minmax['min'] + " - " + minmax['max'])
- print("------------------")
+ grass.message("------------------")
+ grass.message("Ready!")
+ grass.message("The name of raster created is " + outmap)
+ #if minval > minmax['min'] or maxval < minmax['max']:
+ grass.warning("You defined the minimum and maximum weights as: "
+ + minval + " & " + maxval + ". Value range of weight raster is: "
+ + minmax['min'] + " - " + minmax['max'])
if __name__ == "__main__":
options, flags = grass.parser()
Modified: grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html
===================================================================
--- grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html 2015-01-11 22:32:27 UTC (rev 64067)
+++ grass-addons/grass7/raster/r.recode.attr/r.recode.attr.html 2015-01-11 23:31:14 UTC (rev 64068)
@@ -20,10 +20,8 @@
<h2>NOTES</h2>
The script uses r.recode to allow the user to reclass integer values
-to floating point values. However, like <a href=
-"http://grass.osgeo.org/grass70/manuals/r.reclass.html">r.reclass</a>
-you can only convert number one to one (and not like <a href=
-"http://grass.osgeo.org/grass70/manuals/r.recode.html">r.recode</a>
+to floating point values. However, like <a href="http://grass.osgeo.org/grass70/manuals/r.reclass.html">r.reclass</a>
+you can only convert number one to one (and not like <a href="http://grass.osgeo.org/grass70/manuals/r.recode.html">r.recode</a>
ranges of values to one value or a new range of values)
<h2>TODO</h2>
@@ -36,18 +34,12 @@
<h2>EXAMPLES</h2>
-<p>See <a href=
-"http://pvanb.wordpress.com/2014/12/13/recode-your-raster-file-in-gras
-s-gis-using-a-csv-file/">this blog post</a> for an example.
+<p>See <a href="http://pvanb.wordpress.com/2014/12/13/recode-your-raster-file-in-grass-gis-using-a-csv-file/">this blog post</a> for an example.
<h2>SEE ALSO</h2>
-<em><a href=
-"http://grass.osgeo.org/grass70/manuals/r.reclass.html">r.reclass
-</a></em>,
-<em><a href=
-"http://grass.osgeo.org/grass70/manuals/r.recode.html">r.recode
-</a></em>
+<em><a href="http://grass.osgeo.org/grass70/manuals/r.reclass.html">r.reclass</a></em>,
+<em><a href="http://grass.osgeo.org/grass70/manuals/r.recode.html">r.recode</a></em>
<h2>AUTHOR</h2>
Modified: grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py
===================================================================
--- grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py 2015-01-11 22:32:27 UTC (rev 64067)
+++ grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py 2015-01-11 23:31:14 UTC (rev 64068)
@@ -6,7 +6,7 @@
# MODULE: r.recode_attribute
# AUTHOR(S): Paulo van Breugel <p.vanbreugel AT gmail.com>
# PURPOSE: Recode raster to one or more new layers using an
-# attribute table (csv file) as input
+# attribute table (csv file) as input
#
# COPYRIGHT: (C) 2014 Paulo van Breugel
# http://ecodiv.org
@@ -52,6 +52,10 @@
import os
import sys
import numpy as np
+from tempfile import gettempdir, gettempprefix
+from os.path import join
+from string import ascii_lowercase, digits
+from random import choice
import grass.script as grass
def cleanup():
@@ -61,6 +65,16 @@
flags='f',
quiet = True)
+def CreateFileName():
+ (suffix1, suffix2) = ("", "")
+ for _ in xrange( 8 ):
+ suffix1 += choice( ascii_lowercase )
+ suffix2 += choice( digits )
+ flname = join(gettempdir(), '-'.join([gettempprefix(), suffix1, suffix2 ]))
+ while os.path.isfile(flname):
+ flname = flname + "1"
+ return flname
+
# main function
def main():
@@ -87,7 +101,8 @@
for x in numVar:
y = x + 1
myRecode = np.column_stack((myData[:,0], myData[:,0], myData[:,y]))
- np.savetxt('.numpy_grass_recode', myRecode, delimiter=":")
+ tmpname = CreateFileName()
+ np.savetxt(tmpname, myRecode, delimiter=":")
if len(numVar) == lengthNames:
nmOutput = outNames[x]
More information about the grass-commit
mailing list