[GRASS-user] Re: Python-Loop over points

Moritz Lennert mlennert at club.worldonline.be
Wed Apr 13 09:31:20 EDT 2011


Johannes Radinger <JRadinger <at> gmx.at> writes:
> 
> 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
> >>

[snip]

> Do you think that is the best way to loop over points..?

I think it would be easier to loop over the category values of your points,
create a temporary map containing the current point, run r.stream.basins on that
point, etc. Something like this (untested):

listpoints = grass.read_command('v.db.select', map='YourPoints', column=cat,
flags='c')

for pointcat in listpoints.splitlines():
        outputname = 'output' + pointcat
        grass.run_command('v.extract',
                          input='YourPoints',
                          where='cat = ' + pointcat,
                          output='tempmap',
                          overwrite=True)
        grass.run_command('r.stream.basins',
                          dir = 'flow_direction',
                          points = 'tempmap',
                          basins = outputname)

Moritz




More information about the grass-user mailing list