[GRASS-SVN] r46682 - in grass-addons/grass7/vector: . v.median

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jun 13 11:44:49 EDT 2011


Author: lucadelu
Date: 2011-06-13 08:44:49 -0700 (Mon, 13 Jun 2011)
New Revision: 46682

Added:
   grass-addons/grass7/vector/v.median/
   grass-addons/grass7/vector/v.median/Makefile
   grass-addons/grass7/vector/v.median/v.median.html
   grass-addons/grass7/vector/v.median/v.median.py
Log:
add v.median addons, it create a median point from a cloud of points

Added: grass-addons/grass7/vector/v.median/Makefile
===================================================================
--- grass-addons/grass7/vector/v.median/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.median/Makefile	2011-06-13 15:44:49 UTC (rev 46682)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.median
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/vector/v.median/v.median.html
===================================================================
--- grass-addons/grass7/vector/v.median/v.median.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.median/v.median.html	2011-06-13 15:44:49 UTC (rev 46682)
@@ -0,0 +1,24 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.median</em> create the median point of a cloud of point.
+
+<h2>EXAMPLE</h2>
+
+Return the median point to the standard output
+
+<div class="code"><pre>
+v.median input=random_point
+
+12.588035|42.664944
+</pre></div>
+
+Create a vector with the median point
+
+<div class="code"><pre>
+v.median input=random_point output=median_random_point
+
+</pre></div>
+
+<h2>AUTHORS</h2>
+
+Luca Delucchi, Fondazione E. Mach (Italy)

Added: grass-addons/grass7/vector/v.median/v.median.py
===================================================================
--- grass-addons/grass7/vector/v.median/v.median.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.median/v.median.py	2011-06-13 15:44:49 UTC (rev 46682)
@@ -0,0 +1,91 @@
+ #!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:       v.barycenter
+# AUTHOR(S):    Luca Delucchi, Fondazione E. Mach (Italy)
+#
+# PURPOSE:      Return the barycenter of a cloud of point
+# COPYRIGHT:    (C) 2011 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: Return the barycenter of a cloud of point.
+#% keywords: vector
+#%end
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,vector,vector
+#% description: Name of vector map to pack up
+#% key_desc: name
+#% required : yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new_file,file,output
+#% description: Name for output file ('-' for standard output)
+#% answer: -
+#% required : no
+#%end
+
+import sys, os
+from grass.script import core as grass
+from numpy import transpose, genfromtxt, median
+
+def point_med(filetmp):
+    # function to return the median point, x and y
+    point_list = transpose(genfromtxt(filetmp, delimiter='|',usecols=(0,1)))
+    return median(point_list[0]), median(point_list[1])
+
+def main():
+    # check if input file exists
+    infile = options['input']
+    gfile = grass.find_file(infile, element = 'vector')
+    if not gfile['name']:
+        grass.fatal(_("Vector map <%s> not found") % infile)    
+    # create tempfile and write ascii file of input
+    temp_in = grass.tempfile()
+    inascii = grass.run_command('v.out.ascii', overwrite = True, input=gfile['name'], 
+				output = temp_in)
+    # x and y of median point
+    medx, medy = point_med(temp_in)   
+    grass.try_remove(temp_in)
+    # prepare the output
+    output = "%f|%f" % (medx, medy)
+    map_name = options['output']
+    overwrite = os.getenv('GRASS_OVERWRITE')
+    # if output is not set return to stdout
+    if map_name == '-':
+	grass.message(output)
+    # else
+    else:
+	# output file
+	goutfile = grass.find_file(name = map_name, element = 'vector',
+                            mapset = '.')
+        # output tempfile
+	temp_out = grass.tempfile()
+	file_out = open(temp_out,'w')
+	file_out.write(output)
+	file_out.close()
+	# output file exists and not overwrite
+        if goutfile['file'] and overwrite != '1':
+	    grass.fatal(_("Vector map <%s> already exists") % map_name)
+	# output file exists and overwrite
+	elif goutfile['file'] and overwrite == '1':
+	    grass.warning(_("Vector map <%s> already exists and will be overwritten") % map_name)
+	    outascii = grass.run_command('v.in.ascii', overwrite = True, input=temp_out, 
+					output = map_name)
+	# output file not exists
+	else:
+	    outascii = grass.run_command('v.in.ascii', input=temp_out, output = map_name)
+	grass.try_remove(temp_out)
+   
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())



More information about the grass-commit mailing list