[GRASS-SVN] r37278 - grass/trunk/scripts/m.proj

svn_grass at osgeo.org svn_grass at osgeo.org
Tue May 19 08:42:58 EDT 2009


Author: hamish
Date: 2009-05-19 08:42:58 -0400 (Tue, 19 May 2009)
New Revision: 37278

Modified:
   grass/trunk/scripts/m.proj/m.proj.py
Log:
make -e flag work, add -c flag

Modified: grass/trunk/scripts/m.proj/m.proj.py
===================================================================
--- grass/trunk/scripts/m.proj/m.proj.py	2009-05-19 11:24:54 UTC (rev 37277)
+++ grass/trunk/scripts/m.proj/m.proj.py	2009-05-19 12:42:58 UTC (rev 37278)
@@ -8,7 +8,7 @@
 #               Converted to Python by Glynn Clements
 # PURPOSE:      cs2cs reprojection frontend for a list of coordinates.
 #		Replacement for m.proj2 from GRASS 5
-# COPYRIGHT:	(c) 2006,2008 Hamish Bowman, and the GRASS Development Team
+# COPYRIGHT:	(c) 2006-2009 Hamish Bowman, and 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.
@@ -48,10 +48,10 @@
 #% key: fs
 #% type: string
 #% label: Field separator (format: input[,output])
-#% description: Valid field separators are also "space", "tab" or "comma"
+#% description: Valid field separators are also "space", "tab", or "comma"
 #% required : no
 #% key_desc : string
-#% answer : |,|
+#% answer : |
 #%end
 #%option
 #% key: proj_in
@@ -81,7 +81,12 @@
 #% key: e
 #% description: Include input coordinates in output file
 #%end
+#%flag
+#% key: c
+#% description: Include column names in output file
+#%end
 
+
 import sys
 import os
 import grass
@@ -96,6 +101,7 @@
     ll_out = flags['o']
     decimal = flags['d']
     copy_input = flags['e']
+    include_header = flags['c']
 
     #### check for cs2cs
     if not grass.find_program('cs2cs'):
@@ -115,11 +121,12 @@
 	grass.fatal("Output file already exists") 
 
     #### parse field separator
+    # FIXME: input_x,y needs to split on multiple whitespace between them
     try:
         ifs, ofs = fs.split(',')
     except ValueError:
         ifs = ofs = fs
-    
+
     ifs = ifs.lower()
     ofs = ofs.lower()
     
@@ -134,7 +141,7 @@
             ifs = ifs[0]
         except IndexError:
             grass.fatal("Invalid field separator '%s'" % ifs)
-        
+   
     if ofs.lower() == 'space':
         ofs = ' '
     elif ofs.lower() == 'tab':
@@ -148,7 +155,7 @@
             ofs = ofs[0]
         except IndexError:
             grass.fatal("Invalid field separator '%s'" % ifs)
-    
+
     #### set up projection params
     s = grass.read_command("g.proj", flags='j')
     kv = grass.parse_key_val(s)
@@ -242,12 +249,26 @@
     if exitcode != 0:
 	grass.warning("Projection transform probably failed, please investigate")
 
-    for line in p.communicate()[0].splitlines():
-        x, yz = line.split('\t')
-        y, z = yz.split(' ')
-        outf.write('%s%s%s%s%s\n' % \
+
+    if not copy_input:
+	if include_header:
+	    outf.write("x%sy%sz\n" % (ofs, ofs))
+	for line in p.communicate()[0].splitlines():
+	    x, yz = line.split('\t')
+	    y, z = yz.split(' ')
+	    outf.write('%s%s%s%s%s\n' % \
                        (x.strip(), ofs, y.strip(), ofs, z.strip()))
-    
+    else:
+	if include_header:
+	    outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs))
+	for line in p.communicate()[0].splitlines():
+	    inX, therest, z = line.split(' ')
+	    inY, x, y = therest.split('\t')
+	    outf.write('%s%s%s%s%s%s%s%s%s\n' % \
+                       (inX.strip(), ofs, inY.strip(), ofs, x.strip(), \
+		        ofs, y.strip(), ofs, z.strip()))
+
+
 if __name__ == "__main__":
     options, flags = grass.parser()
     main()



More information about the grass-commit mailing list