[GRASS-user] Python-Loop over points

Hello,

I am performing e.g. the r.stream.basins calcutlation in python:

    grass.run_command("r.stream.basins",
                      dir = "flow_direction",
                      points = "point1",
                      basins = "output")

that's working perfectly in the case of one single point in "point1".
How can I do that multiple times when the point layer consist of e.g. 5
points and I want to create an output for each point (create 5 output rasters).

How can that be done in a python script?

thank you

Johannes

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

Hi Johannes,

You will need to use for loop. Something like this (UNTESTED!)

for p in range(5):
    points_map = "point"+str(p)
    out_map = "output"+str(p)
    grass.run_command("r.streams.basins", dir = "flow_direction,
points=points_map, basins = out_map)

That should work, or at least something similar

Daniel

On Wed, Feb 2, 2011 at 9:39 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hello,

I am performing e.g. the r.stream.basins calcutlation in python:

grass.run_command("r.stream.basins",
dir = "flow_direction",
points = "point1",
basins = "output")

that's working perfectly in the case of one single point in "point1".
How can I do that multiple times when the point layer consist of e.g. 5
points and I want to create an output for each point (create 5 output rasters).

How can that be done in a python script?

thank you

Johannes

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Hello,

-------- Original-Nachricht --------

Datum: Mon, 7 Feb 2011 22:25:31 -0200
Von: Daniel Victoria <daniel.victoria@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass-user@lists.osgeo.org
Betreff: Re: [GRASS-user] Python-Loop over points

Hi Johannes,

You will need to use for loop. Something like this (UNTESTED!)

for p in range(5):
    points_map = "point"+str(p)
    out_map = "output"+str(p)
    grass.run_command("r.streams.basins", dir = "flow_direction,
points=points_map, basins = out_map)

That should work, or at least something similar

Daniel

Thank you for your suggestions Daniel, I also thought about
a for loop. But in your case the loop is using for each iteration
a input-pointmap called "point"+str(p).

The difference in my
case is that I've got only one vector map but with several points
in it and I want to iterate over the single points.
How is it possible to iterate over the single points? The points are
stored in a kind of list format, so it should somehow be possible to
loop over this file, but how?

any suggestions?

/johannes

On Wed, Feb 2, 2011 at 9:39 AM, Johannes Radinger <JRadinger@gmx.at>
wrote:
> Hello,
>
> I am performing e.g. the r.stream.basins calcutlation in python:
>
> grass.run_command("r.stream.basins",
> dir = "flow_direction",
> points = "point1",
> basins = "output")
>
> that's working perfectly in the case of one single point in "point1".
> How can I do that multiple times when the point layer consist of e.g. 5
> points and I want to create an output for each point (create 5 output
rasters).
>
> How can that be done in a python script?
>
>
> thank you
>
> Johannes
>
> --
> NEU: FreePhone - kostenlos mobil telefonieren und surfen!
> Jetzt informieren: http://www.gmx.net/de/go/freephone
> _______________________________________________
> grass-user mailing list
> grass-user@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
>

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

I'm not sure but from what I read in the r.stream.basins description,
you can have more than one point in your vector file in roder to
delineate your basins. Now, in case you have one basin inside another,
what I did once was:
1) get x,y coordinates of each point using v.out.ascii
2) for each point run r.watershed...

Cheers
daniel

On Fri, Feb 18, 2011 at 11:49 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hello,

-------- Original-Nachricht --------

Datum: Mon, 7 Feb 2011 22:25:31 -0200
Von: Daniel Victoria <daniel.victoria@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass-user@lists.osgeo.org
Betreff: Re: [GRASS-user] Python-Loop over points

Hi Johannes,

You will need to use for loop. Something like this (UNTESTED!)

for p in range(5):
points_map = "point"+str(p)
out_map = "output"+str(p)
grass.run_command("r.streams.basins", dir = "flow_direction,
points=points_map, basins = out_map)

That should work, or at least something similar

Daniel

Thank you for your suggestions Daniel, I also thought about
a for loop. But in your case the loop is using for each iteration
a input-pointmap called "point"+str(p).

The difference in my
case is that I've got only one vector map but with several points
in it and I want to iterate over the single points.
How is it possible to iterate over the single points? The points are
stored in a kind of list format, so it should somehow be possible to
loop over this file, but how?

any suggestions?

/johannes

On Wed, Feb 2, 2011 at 9:39 AM, Johannes Radinger <JRadinger@gmx.at>
wrote:
> Hello,
>
> I am performing e.g. the r.stream.basins calcutlation in python:
>
> grass.run_command("r.stream.basins",
> dir = "flow_direction",
> points = "point1",
> basins = "output")
>
> that's working perfectly in the case of one single point in "point1".
> How can I do that multiple times when the point layer consist of e.g. 5
> points and I want to create an output for each point (create 5 output
rasters).
>
> How can that be done in a python script?
>
>
> thank you
>
> Johannes
>
> --
> NEU: FreePhone - kostenlos mobil telefonieren und surfen!
> Jetzt informieren: http://www.gmx.net/de/go/freephone
> _______________________________________________
> grass-user mailing list
> grass-user@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
>

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

Am 22.02.2011 um 02:07 schrieb Daniel Victoria:

I'm not sure but from what I read in the r.stream.basins description,
you can have more than one point in your vector file in roder to
delineate your basins. Now, in case you have one basin inside another,
what I did once was:
1) get x,y coordinates of each point using v.out.ascii
2) for each point run r.watershed...

Yes, thats what I want as well, but I want to automatize it and not to run for each
point manually. I also want to run
for each point a grass-function (not necessarily r.stream etc.)
I just want to know how it basically works to loop over points.

to things I thought:
1) loop over the ascii if that is possible
2) use the for loop in combination with v.db.select, but I don't know how that
will work in python

/johannes

Cheers
daniel

On Fri, Feb 18, 2011 at 11:49 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hello,

-------- Original-Nachricht --------

Datum: Mon, 7 Feb 2011 22:25:31 -0200
Von: Daniel Victoria <daniel.victoria@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass-user@lists.osgeo.org
Betreff: Re: [GRASS-user] Python-Loop over points

Hi Johannes,

You will need to use for loop. Something like this (UNTESTED!)

for p in range(5):
    points_map = "point"+str(p)
    out_map = "output"+str(p)
    grass.run_command("r.streams.basins", dir = "flow_direction,
points=points_map, basins = out_map)

That should work, or at least something similar

Daniel

Thank you for your suggestions Daniel, I also thought about
a for loop. But in your case the loop is using for each iteration
a input-pointmap called "point"+str(p).

The difference in my
case is that I've got only one vector map but with several points
in it and I want to iterate over the single points.
How is it possible to iterate over the single points? The points are
stored in a kind of list format, so it should somehow be possible to
loop over this file, but how?

any suggestions?

/johannes

On Wed, Feb 2, 2011 at 9:39 AM, Johannes Radinger <JRadinger@gmx.at>
wrote:

Hello,

I am performing e.g. the r.stream.basins calcutlation in python:

   grass.run_command("r.stream.basins",
                     dir = "flow_direction",
                     points = "point1",
                     basins = "output")

that's working perfectly in the case of one single point in "point1".
How can I do that multiple times when the point layer consist of e.g. 5
points and I want to create an output for each point (create 5 output

rasters).

How can that be done in a python script?

thank you

Johannes

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

Why not create a temporary vector map from the larger vector map in
the for loop; do what needs to me done; clean up.

I would be interested in you solution, and could offer some help if
you would like. I am going to have to process similar data.
HTH

Stephen

On Tue, Feb 22, 2011 at 1:35 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Am 22.02.2011 um 02:07 schrieb Daniel Victoria:

I'm not sure but from what I read in the r.stream.basins description,
you can have more than one point in your vector file in roder to
delineate your basins. Now, in case you have one basin inside another,
what I did once was:
1) get x,y coordinates of each point using v.out.ascii
2) for each point run r.watershed...

Yes, thats what I want as well, but I want to automatize it and not to run for each
point manually. I also want to run
for each point a grass-function (not necessarily r.stream etc.)
I just want to know how it basically works to loop over points.

to things I thought:
1) loop over the ascii if that is possible
2) use the for loop in combination with v.db.select, but I don't know how that
will work in python

/johannes

Cheers
daniel

On Fri, Feb 18, 2011 at 11:49 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hello,

-------- Original-Nachricht --------

Datum: Mon, 7 Feb 2011 22:25:31 -0200
Von: Daniel Victoria <daniel.victoria@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass-user@lists.osgeo.org
Betreff: Re: [GRASS-user] Python-Loop over points

Hi Johannes,

You will need to use for loop. Something like this (UNTESTED!)

for p in range(5):
points_map = "point"+str(p)
out_map = "output"+str(p)
grass.run_command("r.streams.basins", dir = "flow_direction,
points=points_map, basins = out_map)

That should work, or at least something similar

Daniel

Thank you for your suggestions Daniel, I also thought about
a for loop. But in your case the loop is using for each iteration
a input-pointmap called "point"+str(p).

The difference in my
case is that I've got only one vector map but with several points
in it and I want to iterate over the single points.
How is it possible to iterate over the single points? The points are
stored in a kind of list format, so it should somehow be possible to
loop over this file, but how?

any suggestions?

/johannes

On Wed, Feb 2, 2011 at 9:39 AM, Johannes Radinger <JRadinger@gmx.at>
wrote:

Hello,

I am performing e.g. the r.stream.basins calcutlation in python:

grass.run_command("r.stream.basins",
dir = "flow_direction",
points = "point1",
basins = "output")

that's working perfectly in the case of one single point in "point1".
How can I do that multiple times when the point layer consist of e.g. 5
points and I want to create an output for each point (create 5 output

rasters).

How can that be done in a python script?

thank you

Johannes

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
Stephen Sefick
____________________________________
| Auburn University |
| Biological Sciences |
| 331 Funchess Hall |
| Auburn, Alabama |
| 36849 |
|___________________________________|
| sas0025@auburn.edu |
| http://www.auburn.edu/~sas0025 |
|___________________________________|

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods. We are mammals, and have not exhausted the
annoying little problems of being mammals.

                            \-K\. Mullis

"A big computer, a complex algorithm and a long time does not equal science."

                          \-Robert Gentleman

Hello Johannes:

On 02/22/2011 04:57 PM, stephen sefick wrote:

Am 22.02.2011 um 02:07 schrieb Daniel Victoria:

I'm not sure but from what I read in the r.stream.basins description,
you can have more than one point in your vector file in roder to
delineate your basins. Now, in case you have one basin inside another,
what I did once was:
1) get x,y coordinates of each point using v.out.ascii
2) for each point run r.watershed...

Yes, thats what I want as well, but I want to automatize it and not to run for each
point manually. I also want to run
for each point a grass-function (not necessarily r.stream etc.)
I just want to know how it basically works to loop over points.

to things I thought:
1) loop over the ascii if that is possible

Here's an outline of a method I once used to get all points where a stream network crosses a road, then loop thru those points to create drainage basins for each of those points. Maybe you can distill from this what you need.
http://www.surfaces.co.il/?p=241

--
Micha Silver
Arava Development Co. +972-52-3665918
http://surfaces.co.il

Hello,

I want to reactive this thread...
and want just to put in some new ideas...and i still want to use a for-loop
or something similar on a point-vector-file in a Python-script

i want something like Daniel mentioned but want to loop over points within one single pointfile:

   for point in "points_file": --> that is the line I don't know, I want to get point per point and it's ID
  grass.run_command("r.streams.basins", dir = "flow_direction, points=point_ID, basins = out_map_ID)

@Stephen sefick: do you have solved that issue already?

I think that is a quite common problem, therefore I think there must be a quite simple solution....
is a point-file a loopable element? how would an example look like?
I am looking for a similar approach what ArcGIS is using now in their new modeller (Iterator).

/johannes

Am 22.02.2011 um 15:57 schrieb stephen sefick:

Why not create a temporary vector map from the larger vector map in
the for loop; do what needs to me done; clean up.

I would be interested in you solution, and could offer some help if
you would like. I am going to have to process similar data.
HTH

Stephen

On Tue, Feb 22, 2011 at 1:35 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Am 22.02.2011 um 02:07 schrieb Daniel Victoria:

I'm not sure but from what I read in the r.stream.basins description,
you can have more than one point in your vector file in roder to
delineate your basins. Now, in case you have one basin inside another,
what I did once was:
1) get x,y coordinates of each point using v.out.ascii
2) for each point run r.watershed...

Yes, thats what I want as well, but I want to automatize it and not to run for each
point manually. I also want to run
for each point a grass-function (not necessarily r.stream etc.)
I just want to know how it basically works to loop over points.

to things I thought:
1) loop over the ascii if that is possible
2) use the for loop in combination with v.db.select, but I don't know how that
will work in python

/johannes

Cheers
daniel

On Fri, Feb 18, 2011 at 11:49 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hello,

-------- Original-Nachricht --------

Datum: Mon, 7 Feb 2011 22:25:31 -0200
Von: Daniel Victoria <daniel.victoria@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass-user@lists.osgeo.org
Betreff: Re: [GRASS-user] Python-Loop over points

Hi Johannes,

You will need to use for loop. Something like this (UNTESTED!)

for p in range(5):
    points_map = "point"+str(p)
    out_map = "output"+str(p)
    grass.run_command("r.streams.basins", dir = "flow_direction,
points=points_map, basins = out_map)

That should work, or at least something similar

Daniel

Thank you for your suggestions Daniel, I also thought about
a for loop. But in your case the loop is using for each iteration
a input-pointmap called "point"+str(p).

The difference in my
case is that I've got only one vector map but with several points
in it and I want to iterate over the single points.
How is it possible to iterate over the single points? The points are
stored in a kind of list format, so it should somehow be possible to
loop over this file, but how?

any suggestions?

/johannes

On Wed, Feb 2, 2011 at 9:39 AM, Johannes Radinger <JRadinger@gmx.at>
wrote:

Hello,

I am performing e.g. the r.stream.basins calcutlation in python:

   grass.run_command("r.stream.basins",
                     dir = "flow_direction",
                     points = "point1",
                     basins = "output")

that's working perfectly in the case of one single point in "point1".
How can I do that multiple times when the point layer consist of e.g. 5
points and I want to create an output for each point (create 5 output

rasters).

How can that be done in a python script?

thank you

Johannes

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
Stephen Sefick
____________________________________
| Auburn University |
| Biological Sciences |
| 331 Funchess Hall |
| Auburn, Alabama |
| 36849 |
|___________________________________|
| sas0025@auburn.edu |
| http://www.auburn.edu/~sas0025 |
|___________________________________|

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods. We are mammals, and have not exhausted the
annoying little problems of being mammals.

                                -K. Mullis

"A big computer, a complex algorithm and a long time does not equal science."

                              -Robert Gentleman

Hej again,

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? is there a simpler way to loop over points to use them in other modules?

Am 06.04.2011 um 12:48 schrieb Johannes Radinger:

Hello,

I want to reactive this thread...
and want just to put in some new ideas...and i still want to use a for-loop
or something similar on a point-vector-file in a Python-script

i want something like Daniel mentioned but want to loop over points within one single pointfile:

  for point in "points_file": --> that is the line I don't know, I want to get point per point and it's ID
  grass.run_command("r.streams.basins", dir = "flow_direction, points=point_ID, basins = out_map_ID)

@Stephen sefick: do you have solved that issue already?

I think that is a quite common problem, therefore I think there must be a quite simple solution....
is a point-file a loopable element? how would an example look like?
I am looking for a similar approach what ArcGIS is using now in their new modeller (Iterator).

/johannes

Am 22.02.2011 um 15:57 schrieb stephen sefick:

Why not create a temporary vector map from the larger vector map in
the for loop; do what needs to me done; clean up.

I would be interested in you solution, and could offer some help if
you would like. I am going to have to process similar data.
HTH

Stephen

On Tue, Feb 22, 2011 at 1:35 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Am 22.02.2011 um 02:07 schrieb Daniel Victoria:

I'm not sure but from what I read in the r.stream.basins description,
you can have more than one point in your vector file in roder to
delineate your basins. Now, in case you have one basin inside another,
what I did once was:
1) get x,y coordinates of each point using v.out.ascii
2) for each point run r.watershed...

Yes, thats what I want as well, but I want to automatize it and not to run for each
point manually. I also want to run
for each point a grass-function (not necessarily r.stream etc.)
I just want to know how it basically works to loop over points.

to things I thought:
1) loop over the ascii if that is possible
2) use the for loop in combination with v.db.select, but I don't know how that
will work in python

/johannes

Cheers
daniel

On Fri, Feb 18, 2011 at 11:49 AM, Johannes Radinger <JRadinger@gmx.at> wrote:

Hello,

-------- Original-Nachricht --------

Datum: Mon, 7 Feb 2011 22:25:31 -0200
Von: Daniel Victoria <daniel.victoria@gmail.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass-user@lists.osgeo.org
Betreff: Re: [GRASS-user] Python-Loop over points

Hi Johannes,

You will need to use for loop. Something like this (UNTESTED!)

for p in range(5):
   points_map = "point"+str(p)
   out_map = "output"+str(p)
   grass.run_command("r.streams.basins", dir = "flow_direction,
points=points_map, basins = out_map)

That should work, or at least something similar

Daniel

Thank you for your suggestions Daniel, I also thought about
a for loop. But in your case the loop is using for each iteration
a input-pointmap called "point"+str(p).

The difference in my
case is that I've got only one vector map but with several points
in it and I want to iterate over the single points.
How is it possible to iterate over the single points? The points are
stored in a kind of list format, so it should somehow be possible to
loop over this file, but how?

any suggestions?

/johannes

On Wed, Feb 2, 2011 at 9:39 AM, Johannes Radinger <JRadinger@gmx.at>
wrote:

Hello,

I am performing e.g. the r.stream.basins calcutlation in python:

  grass.run_command("r.stream.basins",
                    dir = "flow_direction",
                    points = "point1",
                    basins = "output")

that's working perfectly in the case of one single point in "point1".
How can I do that multiple times when the point layer consist of e.g. 5
points and I want to create an output for each point (create 5 output

rasters).

How can that be done in a python script?

thank you

Johannes

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
Stephen Sefick
____________________________________
| Auburn University |
| Biological Sciences |
| 331 Funchess Hall |
| Auburn, Alabama |
| 36849 |
|___________________________________|
| sas0025@auburn.edu |
| http://www.auburn.edu/~sas0025 |
|___________________________________|

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods. We are mammals, and have not exhausted the
annoying little problems of being mammals.

                               -K. Mullis

"A big computer, a complex algorithm and a long time does not equal science."

                             -Robert Gentleman

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

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@gclements.plus.com>

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

Johannes Radinger wrote:

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

Only if the number of points is small. Otherwise, you're better off
putting them into a table and using a join.

--
Glynn Clements <glynn@gclements.plus.com>

-------- Original-Nachricht --------

Datum: Fri, 8 Apr 2011 00:26:03 +0100
Von: Glynn Clements <glynn@gclements.plus.com>
An: Johannes Radinger <JRadinger@gmx.at>
CC: grass-user@lists.osgeo.org
Betreff: Re: [GRASS-user] Python-Loop over points

Johannes Radinger wrote:

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

Only if the number of points is small. Otherwise, you're better off
putting them into a table and using a join.

What do you mean with your method (table and join)?

--
Glynn Clements <glynn@gclements.plus.com>

--
NEU: FreePhone - kostenlos mobil telefonieren und surfen!
Jetzt informieren: http://www.gmx.net/de/go/freephone

Johannes Radinger wrote:

> > Do you think that is the best way to loop over points..?
>
> Only if the number of points is small. Otherwise, you're better off
> putting them into a table and using a join.

What do you mean with your method (table and join)?

Rather than executing a separate query for each rowid, create a table
containing all of the rowids then (via db.select):

  SELECT input.rowid, input.x, input.y
  FROM input, rowids
  WHERE input.rowid = rowids.rowid

Note: the DBF driver doesn't support joins (which is a good reason not
to use the DBF driver).

--
Glynn Clements <glynn@gclements.plus.com>

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