[GRASS-user] Calling GRASS scripts from GRASS C modules

Glynn Clements glynn at gclements.plus.com
Mon Mar 21 22:26:53 EDT 2011


Markus Neteler wrote:

> > I'm building a segmentation and statistical image analysis module (in C) and
> > I want to call a grass script. Is it possible inside a GRASS C module? If
> > so, How can I do that? (or can anyone give me an online example=
> 
> You can use the popen() function. See for example:
> 
> http://trac.osgeo.org/grass/browser/grass/branches/releasebranch_6_4/raster/r.statistics/o_median.c#L22

Note that code which uses system() or popen() tends to be error-prone,
particularly on Windows. I would strongly discourage anyone from using
either system() or popen() in new code. Significant effort went into
eliminating the use of these functions from 7.0, with good reason.

G_spawn() and G_spawn_ex() provide a more robust interface, but
require more code in the case where you need to communicate with the
child process via a pipe (see lib/db/dbmi_client/start.c for an
example).

7.0 has G_popen_{read,write}, which do most of the work for you, but
these haven't been back-ported to 6.x yet.

Alternatively, you can use a temporary file rather than a pipe for
communicating with the child process.

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


More information about the grass-user mailing list