[GRASS-dev] Call Grass modules from C++ code

Hi,

I've already posted this question in the User mailing list but maybe, as suggested by somebody, it should be posted here...

The problem is how to call a Grass module from my code written in C++.

I have already received some answers:

1 - Do a sys-call

I would avoid this approach if possible. It is not very reliable and it requires to parse the output of the modules as strings.

2 - Use the keyword extern to call the GRASS library functionalities (http://grasswiki.osgeo.org/wiki/GRASS_and_C++)

I have not understood if this approach is suitable also to call GRASS modules. If it is, can you please suggest me an example of this?

Thank you for your help! And sorry for having posted this twice.
Matteo

Hi,

GRASS modules are independent process running in the system. you can use stdout,stderr redirect using boost process or any other std stream redirect library. This way you can read from grass modules and parse the output.

Hope that helps

···

On Mon, Feb 18, 2013 at 7:01 PM, matteo poletti <pollo1_91@yahoo.it> wrote:

Hi,

I’ve already posted this question in the User mailing list but maybe, as suggested by somebody, it should be posted here…

The problem is how to call a Grass module from my code written in C++.

I have already received some answers:

1 - Do a sys-call

I would avoid this approach if possible. It is not very reliable and it requires to parse the output of the modules as strings.

2 - Use the keyword extern to call the GRASS library functionalities (http://grasswiki.osgeo.org/wiki/GRASS_and_C++)

I have not understood if this approach is suitable also to call GRASS modules. If it is, can you please suggest me an example of this?

Thank you for your help! And sorry for having posted this twice.
Matteo


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

Regards,
Rashad

Hi Matteo,

On 18 February 2013 14:31, matteo poletti <pollo1_91@yahoo.it> wrote:

Hi,

I've already posted this question in the User mailing list but maybe, as suggested by somebody, it should be posted here...

The problem is how to call a Grass module from my code written in C++.

I have already received some answers:

1 - Do a sys-call

I would avoid this approach if possible. It is not very reliable and it requires to parse the output of the modules as strings.

Yes, this not ideal but using Python it is not so bad (see below).

2 - Use the keyword extern to call the GRASS library functionalities (http://grasswiki.osgeo.org/wiki/GRASS_and_C++)

I have not understood if this approach is suitable also to call GRASS modules. If it is, can you please suggest me an example of this?

Unfortunately it is not suitable. Modules are just programs and their
functionality is available only through command line. So, through
library calls, you can only use the core functionality in C and C++.

However, calling grass modules is convenient in Python. If Python is
acceptable for you it is worth consider it. There are several ways how
to access grass functionality including modules from Python [1].

In python you can write things such as:

mapsets = grass.read_command('g.mapsets', flags = 'l', sep =
'newline', quiet = True)

list_of_rasters = grass.list_strings(type = 'rast')

It is possible to call grass library functions and there is also new
interface called pygrass which is pythonic and object-oriented.

If you cannot use Python the only possible ways are those you already
mentioned. (Of course, there is also a possibility to use both Python
and C++. For example, some critical parts can be written as C++
programs and parts which needs grass modules can be written in
Python.)

Thank you for your help! And sorry for having posted this twice.

No problem at all.

Vaclav

[1] http://grasswiki.osgeo.org/wiki/GRASS_and_Python

Matteo

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

Da: Vaclav Petras <wenzeslaus@gmail.com>
Unfortunately it is not suitable. Modules are just programs and their
functionality is available only through command line. So, through
library calls, you can only use the core functionality in C and C++.

Finally,
I've decided to follow this approach: I will use only the core
functionalities including the core library with the keyword extern.

However, calling grass modules is convenient in Python. If Python is
acceptable for you it is worth consider it. There are several ways how
to access grass functionality including modules from Python [1].

Usually I use Python for everything, but this time I need performance :slight_smile:

Thank you for your answers!

I guess I'll need some more help digging in the core libraries :wink:

Bye!
Matteo

On 15 March 2013 14:58, matteo poletti <pollo1_91@yahoo.it> wrote:

Da: Vaclav Petras <wenzeslaus@gmail.com>
Unfortunately it is not suitable. Modules are just programs and their
functionality is available only through command line. So, through
library calls, you can only use the core functionality in C and C++.

Finally,
I've decided to follow this approach: I will use only the core
functionalities including the core library with the keyword extern.

However, calling grass modules is convenient in Python. If Python is
acceptable for you it is worth consider it. There are several ways how
to access grass functionality including modules from Python [1].

Usually I use Python for everything, but this time I need performance :slight_smile:

Hi, thank you for sharing your decisions.

One more thing comes in my mind: Cython [1]. Unfortunately, I have no
personal experience but some say that it can be much much faster than
Python. The code translates to some messy C, so it could be fast.
Obviously, it will not make faster existing Python code and especially
pygrass.modules but simple functions are probably faster. Not sure if
someone tried Cython with GRASS.

[1] http://www.cython.org/

Thank you for your answers!

I guess I'll need some more help digging in the core libraries :wink:

Bye!
Matteo

Hi Vaclav,

On Fri, Mar 15, 2013 at 2:32 PM, Vaclav Petras <wenzeslaus@gmail.com> wrote:

Usually I use Python for everything, but this time I need performance :slight_smile:

Hi, thank you for sharing your decisions.

One more thing comes in my mind: Cython [1]. Unfortunately, I have no
personal experience but some say that it can be much much faster than
Python. The code translates to some messy C, so it could be fast.
Obviously, it will not make faster existing Python code and especially
pygrass.modules but simple functions are probably faster. Not sure if
someone tried Cython with GRASS.

One problem to use Cython is write all the "header" files to link
cython with the GRASS library, the good news is that I've tried to
generate these files automatically using: cwrap[0] and it seems to
work fine through clang frontend. but I don't have enough time to test
it!
I would like to translate some lower part of the pygrass library to
use cython and see if and how much the speed gain is.

Pietro

[0] https://github.com/geggo/cwrap.git