[GRASS-dev] Help needed Accessing Grass Modules through python

Dear All,

I am working on accessing grass functionalities through python, but I don’t know which way I should take to achieve this goal.

I have SWIG WIN, GRASS 6.3, Python 2.5, Quantum GIS 0.11

I want to call suppose “mapcalc” of grass from inside python.

Whether this is possible, If yes then how. Whether it can be achieved through g.parser, SWIG or through any other way.

Thank you in advance.

Bishwarup

Hi,

2009/9/14 Bishwarup Banerjee <bishwarup.banerjee@gmail.com>:

I am working on accessing grass functionalities through python, but I don't
know which way I should take to achieve this goal.

I have SWIG WIN, GRASS 6.3, Python 2.5, Quantum GIS 0.11

I want to call suppose "mapcalc" of grass from inside python.

Whether this is possible, If yes then how. Whether it can be achieved
through g.parser, SWIG or through any other way.

I would suggest to use Python scripting library from GRASS >= 6.4 [1].
See raster.mapcalc().

Martin

[1] http://download.osgeo.org/grass/grass6_progman/pythonlib.html
--
Martin Landa <landa.martin gmail.com> * http://gama.fsv.cvut.cz/~landa

Martin Landa wrote:

> I am working on accessing grass functionalities through python, but I don't
> know which way I should take to achieve this goal.
>
> I have SWIG WIN, GRASS 6.3, Python 2.5, Quantum GIS 0.11
>
> I want to call suppose "mapcalc" of grass from inside python.
>
> Whether this is possible, If yes then how. Whether it can be achieved
> through g.parser, SWIG or through any other way.

I would suggest to use Python scripting library from GRASS >= 6.4 [1].
See raster.mapcalc().

The existing grass.mapcalc() function won't work in 6.4. That function
shouldn't have been backported from 7.0, as it requires the 7.0
version of r.mapcalc (which uses G_parser()).

It needs something like:

- if run_command('r.mapcalc', expression = e) != 0:
+ if write_command('r.mapcalc', stdin = e) != 0:

Either that, or use call(['r.mapcalc', expression]).

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

Hi,

2009/9/14 Glynn Clements <glynn@gclements.plus.com>:

[...]

The existing grass.mapcalc() function won't work in 6.4. That function
shouldn't have been backported from 7.0, as it requires the 7.0
version of r.mapcalc (which uses G_parser()).

It needs something like:

- if run_command('r.mapcalc', expression = e) != 0:
+ if write_command('r.mapcalc', stdin = e) != 0:

Either that, or use call(['r.mapcalc', expression]).

right, hopefully fixed in r39203.

Martin

--
Martin Landa <landa.martin gmail.com> * http://gama.fsv.cvut.cz/~landa

Hi Martin and Glynn,

Thank you very much for your response. But as I am newbie to this block, I am not able to make out anything from your reply.
I suppose I am not able to clearly write the exact problem.

I have GRASS 6.3, Python 2.5 and working on Windows platform.

I need to access grass funtionalities from within my python program.
Now the question is How to do this?

On constant searching internet I found some material using g.parser, SWIG or python libs. But I am not sure how to proceed with this.
Or how to configure these, so that I can use them.

It will be very helpful to me if you can step by step tell me how to go ahead.
Thank you once again.

With Regards,
Bishwarup

On Mon, Sep 14, 2009 at 8:38 PM, Glynn Clements <glynn@gclements.plus.com> wrote:

Martin Landa wrote:

I am working on accessing grass functionalities through python, but I don’t
know which way I should take to achieve this goal.

I have SWIG WIN, GRASS 6.3, Python 2.5, Quantum GIS 0.11

I want to call suppose “mapcalc” of grass from inside python.

Whether this is possible, If yes then how. Whether it can be achieved
through g.parser, SWIG or through any other way.

I would suggest to use Python scripting library from GRASS >= 6.4 [1].
See raster.mapcalc().

The existing grass.mapcalc() function won’t work in 6.4. That function
shouldn’t have been backported from 7.0, as it requires the 7.0
version of r.mapcalc (which uses G_parser()).

It needs something like:

  • if run_command(‘r.mapcalc’, expression = e) != 0:
  • if write_command(‘r.mapcalc’, stdin = e) != 0:

Either that, or use call([‘r.mapcalc’, expression]).


Glynn Clements <glynn@gclements.plus.com>

On Tue, Sep 15, 2009 at 7:09 AM, Bishwarup Banerjee
<bishwarup.banerjee@gmail.com> wrote:

Hi Martin and Glynn,

Thank you very much for your response. But as I am newbie to this block, I
am not able to make out anything from your reply.
I suppose I am not able to clearly write the exact problem.

I have GRASS 6.3, Python 2.5 and working on Windows platform.

I need to access grass funtionalities from within my python program.
Now the question is How to do this?

As mentioned by Martin, GRASS 6.3 isn't really suitable. The
entire Python interface was redone in GRASS 6.4 and later, see
http://download.osgeo.org/grass/grass6_progman/pythonlib.html

The page collects a series of examples, too.

Markus

Hi Markus,

Thank you very much for your valuable information. I have downloaded Grass 6.4 and also gone through the pythonlibs documentation. But how to configure the files now? So that I can access them from the python programs.
Thank you in advance.
With Warm Regards,
Bishwarup

On Tue, Sep 15, 2009 at 11:45 AM, Markus Neteler <neteler@osgeo.org> wrote:

On Tue, Sep 15, 2009 at 7:09 AM, Bishwarup Banerjee
<bishwarup.banerjee@gmail.com> wrote:

Hi Martin and Glynn,

Thank you very much for your response. But as I am newbie to this block, I
am not able to make out anything from your reply.
I suppose I am not able to clearly write the exact problem.

I have GRASS 6.3, Python 2.5 and working on Windows platform.

I need to access grass funtionalities from within my python program.
Now the question is How to do this?

As mentioned by Martin, GRASS 6.3 isn’t really suitable. The
entire Python interface was redone in GRASS 6.4 and later, see

http://download.osgeo.org/grass/grass6_progman/pythonlib.html

The page collects a series of examples, too.

Markus