[GRASS-dev] Call Grass modules from C++ code
Vaclav Petras
wenzeslaus at gmail.com
Mon Feb 18 09:10:26 PST 2013
Hi Matteo,
On 18 February 2013 14:31, matteo poletti <pollo1_91 at 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 at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-dev
More information about the grass-dev
mailing list