[GRASS-user] Python-Loop over points

Johannes Radinger JRadinger at gmx.at
Wed Apr 6 16:24:09 EDT 2011


Am 06.04.2011 21:56, schrieb Glynn Clements:
> 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)
Thank you for the info with read_command, i realized that option after i 
wrote
the email. Do you think that is the best way to loop over points..?

/johannes





More information about the grass-user mailing list