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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 11 05:07:42 EDT 2009


Author: martinl
Date: 2009-05-11 05:07:42 -0400 (Mon, 11 May 2009)
New Revision: 37159

Modified:
   grass/trunk/scripts/m.proj/m.proj.py
Log:
m.proj: input required
	field separator for output


Modified: grass/trunk/scripts/m.proj/m.proj.py
===================================================================
--- grass/trunk/scripts/m.proj/m.proj.py	2009-05-11 08:50:34 UTC (rev 37158)
+++ grass/trunk/scripts/m.proj/m.proj.py	2009-05-11 09:07:42 UTC (rev 37159)
@@ -20,15 +20,15 @@
 #  - if you send cs2cs a third data column, beware it might be treated as "z"
 
 #%Module
-#%  description: Convert coordinates from one projection to another (cs2cs frontend).
+#%  description: Converts coordinates from one projection to another (cs2cs frontend).
 #%  keywords: miscellaneous, projection
 #%End
 #%option
 #% key: input
 #% type: string
 #% gisprompt: old_file,file,file
-#% description: Input coordinate file (omit to read from stdin)
-#% required : no
+#% description: Input coordinate file ('-' to read from stdin)
+#% required : yes
 #% key_desc : filename
 #%end
 #%option
@@ -42,10 +42,10 @@
 #%option
 #% key: fs
 #% type: string
-#% description: Field separator
+#% description: Field separator (format: input,output)
 #% required : no
-#% key_desc : character
-#% answer : |
+#% key_desc : character,character
+#% answer : |,|
 #%end
 #%option
 #% key: proj_in
@@ -92,34 +92,41 @@
 
     #### check for overenthusiasm
     if proj_in and ll_in:
-	grass.fatal("Chose only one input parameter method")
+	grass.fatal("Choose only one input parameter method")
 
     if proj_out and ll_out:
-	grass.fatal("Chose only one output parameter method") 
+	grass.fatal("Choose only one output parameter method") 
 
     if ll_in and ll_out:
-	grass.fatal("Chose only one auto-projection parameter method")
+	grass.fatal("Choise only one auto-projection parameter method")
 
     if output and not grass.overwrite() and os.path.exists(output):
 	grass.fatal("Output file already exists") 
 
     #### parse field separator
-    if fs.lower() in ["space", "tab"]:
-	fs = ' '
+    ifs, ofs = fs.split(',')
+    if ifs.lower() in ["space", "tab"]:
+	ifs = ' '
     else:
-	fs = fs[0]
-
+	ifs = ifs[0]
+    if ofs.lower() == "space":
+        ofs = ' '
+    elif ofs.lower() == "tab":
+        ofs = '\t'
+    else:
+        ofs = ofs[0]
+    
     #### set up projection params
     s = grass.read_command("g.proj", flags='j')
     kv = grass.parse_key_val(s)
     if "XY location" in kv['+proj'] and (ll_in or ll_out):
-	grass.fatal("Cannot project to or from a XY location.") 
+	grass.fatal("Unable to project to or from a XY location") 
 
     in_proj = None
 
     if ll_in:
 	in_proj = "+proj=longlat +datum=WGS84"
-	grass.verbose("Assuming LL WGS84 as input, current projection as output.")
+	grass.verbose("Assuming LL WGS84 as input, current projection as output ")
 
     if ll_out:
 	in_proj = grass.read_command('g.proj', flags = 'jf')
@@ -128,14 +135,15 @@
 	in_proj = proj_in
 
     if not in_proj:
-	grass.fatal("Missing input projection parameters.")
+	grass.fatal("Missing input projection parameters ")
     in_proj = in_proj.strip()
+    grass.verbose("Input parameters: '%s'" % in_proj)
 
     out_proj = None
 
     if ll_out:
 	out_proj = "+proj=longlat +datum=WGS84"
-	grass.verbose("Assuming current projection as input, LL WGS84 as output.")
+	grass.verbose("Assuming current projection as input, LL WGS84 as output ")
 
     if ll_in:
 	out_proj = grass.read_command('g.proj', flags = 'jf')
@@ -144,20 +152,20 @@
 	out_proj = proj_out
 
     if not out_proj:
-	grass.fatal("Missing output projection parameters.")
+	grass.fatal("Missing output projection parameters ")
     out_proj = out_proj.strip()
-    grass.verbose("output parameters=[%s]" % out_proj)
+    grass.verbose("Output parameters: '%s'" % out_proj)
 
     #### set up input file
-    if input in ['', '-']:
+    if input == '-':
 	infile = None
 	inf = sys.stdin
     else:
 	infile = input
 	if not os.path.exists(infile):
-	    grass.fatal("Unable to read input data.") 
+	    grass.fatal("Unable to read input data")
 	inf = file(infile)
-	grass.verbose("input file=[%s]" % infile)
+	grass.debug("input file=[%s]" % infile)
 
     #### set up output file
     if not output:
@@ -165,8 +173,8 @@
 	outf = sys.stdout
     else:
 	outfile = output
-	outf = file(outfile)
-	grass.message("output file=[%s]" % outfile) 
+	outf = open(outfile, 'w')
+	grass.debug("output file=[%s]" % outfile) 
 
     #### set up output style
     if not decimal:
@@ -179,23 +187,30 @@
     #   cs2cs | sed -e 's/d/:/g' -e "s/'/:/g"  -e 's/"//g'
 
     cmd = ['cs2cs'] + outfmt + in_proj.split() + ['+to'] + out_proj.split()
-    p = grass.Popen(cmd, stdin = grass.PIPE, stdout = outf)
+    p = grass.Popen(cmd, stdin = grass.PIPE, stdout = grass.PIPE)
 
     while True:
 	line = inf.readline()
 	if not line:
 	    break
-	line = line.replace(fs, ' ')
+	line = line.replace(ifs, ' ')
 	p.stdin.write(line)
 	p.stdin.flush()
 
     p.stdin.close()
-
+    p.stdin = None
+    
     exitcode = p.wait()
 
     if exitcode != 0:
-	grass.warning("Projection transform probably failed, please investigate.")
+	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' % \
+                       (x.strip(), ofs, y.strip(), ofs, z.strip()))
+    
 if __name__ == "__main__":
     options, flags = grass.parser()
     main()



More information about the grass-commit mailing list