[GRASS-dev] grass, python, Pygrass

Dear grass-developers
I'm working with Python, GRASS and Pygrass and I have some small
problems probably due to my poor knowledge of Python.
I would ask your help.

I have this small code:

    grass.run_command("v.to.rast", input=ocs, type="line", output="tmp_ocs", use="val", overwrite=True)
    grass.mapcalc("$outmap = $a", a = dtm, outmap="demcut", overwrite = true)
    grass.run_command("g.remove", rast="visitati")

    np_ocs = raster.RasterNumpy("tmp_ocs")
    np_ocs.open()
    visited=np_ocs*0
    visited.open()

    for i in range(1,new.shape[0]-1):
        for j in range(1,new.shape[1]-1):
            if np_ocs[i,j] == 1:
                visited[i,j] = 1
            else:
                visited[i,j] = 3

    visited.name='visitati'
    visited.close()
    sys.exit("stop")

When I run it I obtain a lot of:

WARNING: rast=visitati,visitati: files could be the same, no rename
         possible

I cannot understand where the warnings arise... and why..

can you help me??

many thanks

Ivan

On 26 June 2013 06:40, Ivan Marchesini <ivan.marchesini@gmail.com> wrote:

Dear grass-developers

Ciao Ivan

I'm working with Python, GRASS and Pygrass and I have some small
problems probably due to my poor knowledge of Python.
I would ask your help.

I have this small code:

this is not your code right?

    grass.run_command("v.to.rast", input=ocs, type="line", output="tmp_ocs", use="val", overwrite=True)
    grass.mapcalc("$outmap = $a", a = dtm, outmap="demcut", overwrite = true)
    grass.run_command("g.remove", rast="visitati")

    np_ocs = raster.RasterNumpy("tmp_ocs")
    np_ocs.open()
    visited=np_ocs*0
    visited.open()

    for i in range(1,new.shape[0]-1):
        for j in range(1,new.shape[1]-1):
            if np_ocs[i,j] == 1:
                visited[i,j] = 1
            else:
                visited[i,j] = 3

    visited.name='visitati'
    visited.close()
    sys.exit("stop")

When I run it I obtain a lot of:

WARNING: rast=visitati,visitati: files could be the same, no rename
         possible

I cannot understand where the warnings arise... and why..

can you help me??

probably your run more than one your script?
probably you have to add visited.overwrite = True before close your map

please let us know

many thanks

Ivan

--
ciao
Luca

http://gis.cri.fmach.it/delucchi/
www.lucadelu.org

Ciao Ivan,

On Wed, Jun 26, 2013 at 5:40 AM, Ivan Marchesini
<ivan.marchesini@gmail.com> wrote:

I have this small code:

    grass.run_command("v.to.rast", input=ocs, type="line", output="tmp_ocs", use="val", overwrite=True)
    grass.mapcalc("$outmap = $a", a = dtm, outmap="demcut", overwrite = true)
    grass.run_command("g.remove", rast="visitati")

    np_ocs = raster.RasterNumpy("tmp_ocs")
    np_ocs.open()
    visited=np_ocs*0
    visited.open()

    for i in range(1,new.shape[0]-1):
        for j in range(1,new.shape[1]-1):
            if np_ocs[i,j] == 1:
                visited[i,j] = 1
            else:
                visited[i,j] = 3

    visited.name='visitati'
    visited.close()
    sys.exit("stop")

Some note on the code:

1) maybe to this kind of things you should consider to use the
r.mapcalc instead of pygrass;
2) Why are you using RasterNumpy?
    - RasterNumpy is quite slow because need to export your map on the
hard disk, load in memory using numpy.memmap... therefore, if
possible, try to avoid this class...
    - Avoid to use cycle in python, they are really slow and are
working only with small region.

Following the first point the cycle will be:

{{{
from grass import script as grass

grass.run_command("r.mapcalc", expression="{outmap} = if({a}==1, 1,
3)".format(a = "tmp_ocs", outmap="visited"), overwrite = True)
}}}

About the second point:

{{{
from grass.pygrass.raster import RasterRow

with RasterRow("tmp_ocs") as ocs:
    new = RasterRow("visited")
    new.open("w", "CELL")
    for row in ocs:
        rbool = (row == 1)
        row[rbool] = 1
        row[~rbool] = 3
        new.write(row)
    new.close()
}}}

In this way only the cycle on the rows is done in python, all the
other cycle are done in C trough numpy.

But to do something so simple I suggest to use r.mapcalc.

When I run it I obtain a lot of:

WARNING: rast=visitati,visitati: files could be the same, no rename
         possible

I cannot understand where the warnings arise... and why..

I think the warning raise, here:

{{{
visited.name='visitati'
}}}

In this piece of code you are assign a new name to the raster, but the
map already exist...
Therefore as Luca said, should be sufficient to add:

{{{
visited.overwrite = True
visited.name = 'visitati'
}}}

let us know.

Pietro

Hi Luca.
thank you for your answer

this is not your code right?

yes it is mine
it is supposed to be part of a larger code I'm developing

> grass.run_command("v.to.rast", input=ocs, type="line", output="tmp_ocs", use="val", overwrite=True)
> grass.mapcalc("$outmap = $a", a = dtm, outmap="demcut", overwrite = true)
> grass.run_command("g.remove", rast="visitati")
>
> np_ocs = raster.RasterNumpy("tmp_ocs")
> np_ocs.open()
> visited=np_ocs*0
> visited.open()
>
> for i in range(1,new.shape[0]-1):
> for j in range(1,new.shape[1]-1):
> if np_ocs[i,j] == 1:
> visited[i,j] = 1
> else:
> visited[i,j] = 3
>
> visited.name='visitati'
> visited.close()
> sys.exit("stop")
>
>
> When I run it I obtain a lot of:
>
> WARNING: rast=visitati,visitati: files could be the same, no rename
> possible
>
> I cannot understand where the warnings arise... and why..
>
> can you help me??

probably you have to add visited.overwrite = True before close your map

I have just tried something ad you have to believe me that I was really
surprised when I discovered that all depends on the fact that the
"visited.name='visitati'" row was indented using tab in place of 4
spaces. I had not indentation error (as I usually obtain) but the code
was running regularly (apart from the error)

concerning this problem I would ask (with the risk to be OT) which is
the best python editor to work with.
At the moment I'm working with Geany but I'm not so happy about that.
What a really miss is an editor that, hopefully, can suggest the methods
and the properties of an object during coding. I know that ipython is
good for this but it is not so good for saving the code as a grass
script.

Many thanks for now...

ciao

Hi Ivan,

On Wed, Jun 26, 2013 at 2:16 PM, Ivan Marchesini
<ivan.marchesini@gmail.com> wrote:

concerning this problem I would ask (with the risk to be OT) which is
the best python editor to work with.
At the moment I'm working with Geany but I'm not so happy about that.
What a really miss is an editor that, hopefully, can suggest the methods
and the properties of an object during coding. I know that ipython is
good for this but it is not so good for saving the code as a grass
script.

I'm using spyder: https://code.google.com/p/spyderlib/

it is a multiplataform IDE with autocompletition and much more, but
you need to install:

pyflakes v0.5.0+ (real-time code analysis)
rope 0.9.2+ (editor code completion, calltips and go-to-definition)
sphinx 0.6+ (object inspector’s rich text mode)
numpy (N-dimensional arrays)
scipy (signal/image processing)
matplotlib (2D/3D plotting)
psutil 0.3+ (memory/CPU usage in status bar)

Optional modules:

IPython 0.13 (enhanced Python interpreter)
pylint (code analysis)
pep8 (style analysis)

Pietro

Hi Pietro
many thanks for your answer

Some note on the code:

1) maybe to this kind of things you should consider to use the
r.mapcalc instead of pygrass;

Yes you are true but later, in the code, I need to do some more
complicated stuf, like increasing or decreasing the value of a cell
based on
1) the value of direction and inclination stored in the same position
into two other maps and and
2) the three cells that are in the opposite side respect to the
direction.

As an example: Suppose I'm considering the cell[i,j].

From the map of directions (drainage) I know that the direction[i,j] is

towards cell[1,j+1] (towards east). From the inclination map I know that
the inclination[i,j] is 45°.
In my algorithm I would like to estimate a new value of the elevation
for the cell[i,j] based on the contribution of the elevation values
stored on the cells [i-1,j-1], [1,j-1],[i+1,j-1].

2) Why are you using RasterNumpy?
    - RasterNumpy is quite slow because need to export your map on the
hard disk, load in memory using numpy.memmap... therefore, if
possible, try to avoid this class...
    - Avoid to use cycle in python, they are really slow and are
working only with small region.

ok but it is possible to work with indexed cells [i,j] as I described
before.

Following the first point the cycle will be:

{{{
from grass import script as grass

grass.run_command("r.mapcalc", expression="{outmap} = if({a}==1, 1,
3)".format(a = "tmp_ocs", outmap="visited"), overwrite = True)
}}}

ok.. thank you. I was stupid not to think at that for this simple
cycle... It happened because I was already thinking about the second
part.. the one of direction and inclination

About the second point:

{{{
from grass.pygrass.raster import RasterRow

with RasterRow("tmp_ocs") as ocs:
    new = RasterRow("visited")
    new.open("w", "CELL")
    for row in ocs:
        rbool = (row == 1)
        row[rbool] = 1
        row[~rbool] = 3
        new.write(row)
    new.close()
}}}

yes.. good.. this is also another way. thank you

In this piece of code you are assign a new name to the raster, but the
map already exist...
Therefore as Luca said, should be sufficient to add:

{{{
visited.overwrite = True
visited.name = 'visitati'
}}}

let us know.

As I said to luca probably the problem was simply due to 4 spaces in
place of a tab

Many many thanks and please let me know if you think that for the
problem of direction and inclination the use of numpy is correct

Ivan

I'm using spyder: https://code.google.com/p/spyderlib/

Many tanks Pietro...

I will try...

:slight_smile:

Hi Ivan,

On Wed, Jun 26, 2013 at 3:44 PM, Ivan Marchesini
<ivan.marchesini@gmail.com> wrote:

Many many thanks and please let me know if you think that for the
problem of direction and inclination the use of numpy is correct

I think that use the RasterSegment class should be faster, but you
should test it, RasterSegment allows you to open a map in 'rw' read
and write mode, that is specially useful for the map "visited".

have fun!

Pietro