[GRASS-user] Question with Python-SWIG example

Glynn Clements glynn at gclements.plus.com
Sun Apr 18 09:40:01 EDT 2010


Markus Neteler wrote:

> >> > But I wouldn't recommend trying to use the SWIG interface. It's not
> >> > particularly robust and not widely understood. If you really need to
> >> > use GRASS library functions from Python, use the ctypes module
> >> > instead.
> >>
> >> How would
> >>  http://grass.osgeo.org/wiki/GRASS_and_Python#Python-SWIG_examples
> >> look like with ctypes? Perhaps the SWIG interface should not be advertised
> >> to much in the Wiki...
> >
> > I've attached versions using ctypes for 6.x and 7.x; only the latter
> > has been tested.
> 
> Thanks for this. For the easy of management, I have put them into SVN
> into doc/python/ (all 6.4, 6.5 and 7) along with a simple argument parsing.
> Perhaps it should be doxygenized and put elsewhere.

FWIW, a caveat about using ctypes: because nothing is autogenerated
from the C source code, any Python code has to be manually kept in
sync with the C interface. E.g. the use of "g.version -r" to get the
GIS_H_VERSION string to pass to G__gisinit(), and the hardcoded 0/1/2
values for {CELL,FCELL,DCELL}_TYPE. This could become a significant
issue for definitions which are prone to change, e.g. "struct Option"
and the G_OPT_* constants.

One option would be to use the SWIG wrappers to obtain values for
manifest constants. It might also be possible to use the SWIG wrappers
for the structure types, but I can't figure out how to convert between
a bare pointer and the SWIG wrapper.

Nor can I figure out how to make use of any pointers returned from
functions. Typemaps exist to allow strings, arrays, etc to be
converted to pointers when passing them into functions, but it isn't
possible to do the reverse (converting to a pointer discards the size
and type; converting from a pointer would require some way to obtain
this information). The functions generated by cpointer.i and carrays.i
don't seem to work as expected (I get a segfault).

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


More information about the grass-user mailing list