def call_async_process_grass(cmd): """ This methode allows to run a separate thread. """ # subprocess.call(shlex.split(cmd)) # This will block until # cmd finishes p = grass.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() return (out, err)
def plot_xgraph(): newline = ['\n'] p = gcore.Popen(['xgraph'], stdin=gcore.PIPE) for point in sine_cosine_replic + newline + outercircle + newline + vector: if isinstance(point, tuple): p.stdin.write("%f %f\n" % point) else: p.stdin.write(point + '\n') p.stdin.close() p.wait()
def plot_xgraph(): newline = ["\n"] p = gcore.Popen(["xgraph"], stdin=gcore.PIPE) for point in sine_cosine_replic + newline + outercircle + newline + vector: if isinstance(point, tuple): p.stdin.write(gcore.encode("%f %f\n" % point)) else: p.stdin.write(gcore.encode(point + "\n")) p.stdin.close() p.wait()
def main(): coords = options['coordinates'] input = options['input'] output = options['output'] fs = options['separator'] proj_in = options['proj_in'] proj_out = options['proj_out'] ll_in = flags['i'] ll_out = flags['o'] decimal = flags['d'] copy_input = flags['e'] include_header = flags['c'] # check for cs2cs if not gcore.find_program('cs2cs'): gcore.fatal( _("cs2cs program not found, install PROJ.4 first: \ http://proj.maptools.org")) # check for overenthusiasm if proj_in and ll_in: gcore.fatal(_("Choose only one input parameter method")) if proj_out and ll_out: gcore.fatal(_("Choose only one output parameter method")) if ll_in and ll_out: gcore.fatal(_("Choose only one auto-projection parameter method")) if output and not gcore.overwrite() and os.path.exists(output): gcore.fatal(_("Output file already exists")) if not coords and not input: gcore.fatal(_("One of <coordinates> and <input> must be given")) if coords and input: gcore.fatal( _("Options <coordinates> and <input> are mutually exclusive")) # parse field separator # FIXME: input_x,y needs to split on multiple whitespace between them if fs == ',': ifs = ofs = ',' else: try: ifs, ofs = fs.split(',') except ValueError: ifs = ofs = fs ifs = separator(ifs) ofs = separator(ofs) # set up projection params s = gcore.read_command("g.proj", flags='j') kv = parse_key_val(s) if "XY location" in kv['+proj'] and (ll_in or ll_out): gcore.fatal(_("Unable to project to or from a XY location")) in_proj = None if ll_in: in_proj = "+proj=longlat +datum=WGS84" gcore.verbose( "Assuming LL WGS84 as input, current projection as output ") if ll_out: in_proj = gcore.read_command('g.proj', flags='jf') if proj_in: if '+' in proj_in: in_proj = proj_in else: gcore.fatal(_("Invalid PROJ.4 input specification")) if not in_proj: gcore.verbose("Assuming current location as input") in_proj = gcore.read_command('g.proj', flags='jf') in_proj = in_proj.strip() gcore.verbose("Input parameters: '%s'" % in_proj) out_proj = None if ll_out: out_proj = "+proj=longlat +datum=WGS84" gcore.verbose( "Assuming current projection as input, LL WGS84 as output ") if ll_in: out_proj = gcore.read_command('g.proj', flags='jf') if proj_out: if '+' in proj_out: out_proj = proj_out else: gcore.fatal(_("Invalid PROJ.4 output specification")) if not out_proj: gcore.fatal(_("Missing output projection parameters ")) out_proj = out_proj.strip() gcore.verbose("Output parameters: '%s'" % out_proj) # set up input file if coords: x, y = coords.split(',') tmpfile = gcore.tempfile() fd = open(tmpfile, "w") fd.write("%s%s%s\n" % (x, ifs, y)) fd.close() inf = file(tmpfile) else: if input == '-': infile = None inf = sys.stdin else: infile = input if not os.path.exists(infile): gcore.fatal(_("Unable to read input data")) inf = file(infile) gcore.debug("input file=[%s]" % infile) # set up output file if not output: outfile = None outf = sys.stdout else: outfile = output outf = open(outfile, 'w') gcore.debug("output file=[%s]" % outfile) # set up output style if not decimal: outfmt = ["-w5"] else: outfmt = ["-f", "%.8f"] if not copy_input: copyinp = [] else: copyinp = ["-E"] # do the conversion # Convert cs2cs DMS format to GRASS DMS format: # cs2cs | sed -e 's/d/:/g' -e "s/'/:/g" -e 's/"//g' cmd = ['cs2cs'] + copyinp + outfmt + \ in_proj.split() + ['+to'] + out_proj.split() p = gcore.Popen(cmd, stdin=gcore.PIPE, stdout=gcore.PIPE) tr = TrThread(ifs, inf, p.stdin) tr.start() if not copy_input: if include_header: outf.write("x%sy%sz\n" % (ofs, ofs)) for line in p.stdout: try: xy, z = line.split(' ', 1) x, y = xy.split('\t') except ValueError: gcore.fatal(line) 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.stdout: inXYZ, x, rest = line.split('\t') inX, inY = inXYZ.split(' ')[:2] y, z = rest.split(' ', 1) 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())) p.wait() if p.returncode != 0: gcore.warning( _("Projection transform probably failed, please investigate"))
def main(): coords = options["coordinates"] input = options["input"] output = options["output"] fs = options["separator"] proj_in = options["proj_in"] proj_out = options["proj_out"] ll_in = flags["i"] ll_out = flags["o"] decimal = flags["d"] copy_input = flags["e"] include_header = flags["c"] # check for cs2cs if not gcore.find_program("cs2cs"): gcore.fatal( _("cs2cs program not found, install PROJ first: \ https://proj.org")) # parse field separator # FIXME: input_x,y needs to split on multiple whitespace between them if fs == ",": ifs = ofs = "," else: try: ifs, ofs = fs.split(",") except ValueError: ifs = ofs = fs ifs = separator(ifs) ofs = separator(ofs) # set up projection params s = gcore.read_command("g.proj", flags="j") kv = parse_key_val(s) if "XY location" in kv["+proj"] and (ll_in or ll_out): gcore.fatal(_("Unable to project to or from a XY location")) in_proj = None if ll_in: in_proj = "+proj=longlat +datum=WGS84" gcore.verbose( "Assuming LL WGS84 as input, current projection as output ") if ll_out: in_proj = gcore.read_command("g.proj", flags="jf") if proj_in: if "+" in proj_in: in_proj = proj_in else: gcore.fatal(_("Invalid PROJ.4 input specification")) if not in_proj: gcore.verbose("Assuming current location as input") in_proj = gcore.read_command("g.proj", flags="jf") in_proj = in_proj.strip() gcore.verbose("Input parameters: '%s'" % in_proj) out_proj = None if ll_out: out_proj = "+proj=longlat +datum=WGS84" gcore.verbose( "Assuming current projection as input, LL WGS84 as output ") if ll_in: out_proj = gcore.read_command("g.proj", flags="jf") if proj_out: if "+" in proj_out: out_proj = proj_out else: gcore.fatal(_("Invalid PROJ.4 output specification")) if not out_proj: gcore.fatal(_("Missing output projection parameters ")) out_proj = out_proj.strip() gcore.verbose("Output parameters: '%s'" % out_proj) # set up input file if coords: x, y = coords.split(",") tmpfile = gcore.tempfile() fd = open(tmpfile, "w") fd.write("%s%s%s\n" % (x, ifs, y)) fd.close() inf = open(tmpfile) else: if input == "-": infile = None inf = sys.stdin else: infile = input if not os.path.exists(infile): gcore.fatal(_("Unable to read input data")) inf = open(infile) gcore.debug("input file=[%s]" % infile) # set up output file if not output: outfile = None outf = sys.stdout else: outfile = output outf = open(outfile, "w") gcore.debug("output file=[%s]" % outfile) # set up output style if not decimal: outfmt = ["-w5"] else: outfmt = ["-f", "%.8f"] if not copy_input: copyinp = [] else: copyinp = ["-E"] # do the conversion # Convert cs2cs DMS format to GRASS DMS format: # cs2cs | sed -e 's/d/:/g' -e "s/'/:/g" -e 's/"//g' cmd = ["cs2cs" ] + copyinp + outfmt + in_proj.split() + ["+to"] + out_proj.split() p = gcore.Popen(cmd, stdin=gcore.PIPE, stdout=gcore.PIPE) tr = TrThread(ifs, inf, p.stdin) tr.start() if not copy_input: if include_header: outf.write("x%sy%sz\n" % (ofs, ofs)) for line in p.stdout: try: xy, z = decode(line).split(" ", 1) x, y = xy.split("\t") except ValueError: gcore.fatal(line) 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.stdout: inXYZ, x, rest = decode(line).split("\t") inX, inY = inXYZ.split(" ")[:2] y, z = rest.split(" ", 1) 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(), )) p.wait() if p.returncode != 0: gcore.warning( _("Projection transform probably failed, please investigate"))