[GRASS-user] Python-Loop over points
Glynn Clements
glynn at gclements.plus.com
Wed Apr 6 15:56:56 EDT 2011
Johannes Radinger wrote:
> I've got a first basic idea but it isn't working yet completetly.
> I wanted to loop over the pointfile to use the single points for other operations
> like the r.stream.basins module. Most of these modules do also except coordinates.
>
> So here is one solution but hopefully someone know a better one:
>
> 1) add two new columns to the pointsfile and populate it with the X
> and Y coordinate (v.db.addcol and v.to.db)
>
> 2) use v.db.select-command in a for loop and pipe the X.Y
> coordinate-pair to a variable, like (to loop over the first 5 points
> in a pointfile):
>
> def main():
> for i in range(5):
> where ="rowid = "+str(i)
> p = grass.pipe_command("v.db.select", flags="c", map=options['input'], columns="X,Y", where=where, fs=",")
> print p
>
>
> It isn't working at the moment (I get <grass.script.core.Popen object
> at 0x53f3f0>) but I don't think that is a problem of the for loop, it
> is more a problem of the pipe-command because it is working when i use
> run_command instead of pipe_command.
>
> what is the problem with the pipe command?
pipe_command() returns the process object, not its output. You can
read the process' output via p.stdout. If you just want the process'
output as a string, use read_command() instead.
read_command() is roughly equivalent to `backticks` in the shell.
pipe_command() is useful if the process will return a lot of output or
if you want to process the output as it is generated (read_command()
won't return until the command has terminated).
--
Glynn Clements <glynn at gclements.plus.com>
More information about the grass-user
mailing list