[GRASS-user] Question with Python-SWIG example

Glynn Clements glynn at gclements.plus.com
Sat Apr 17 07:57:44 EDT 2010


Markus Neteler wrote:

> On Thu, Apr 15, 2010 at 1:22 PM, Glynn Clements
> <glynn at gclements.plus.com> 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.

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

-------------- next part --------------
#!/usr/bin/env python
import os, sys, subprocess
from ctypes import *
grass = CDLL("libgrass_gis.so")

input = sys.argv[1]
 
# initialize
s = subprocess.Popen(['g.version','-r'], stdout=subprocess.PIPE).communicate()[0]
for line in s.splitlines():
    if line.startswith('Revision:'):
        version = '$' + line + '$'
grass.G__gisinit(version, '')
 
# find map in search path
mapset = grass.G_find_cell2(input, '')
mapset = c_char_p(mapset).value
 
# determine the inputmap type (CELL/FCELL/DCELL) */
data_type = grass.G_raster_map_type(input, mapset)

if data_type == 0:
    ptype = POINTER(c_int)
elif data_type == 1:
    ptype = POINTER(c_float)
elif data_type == 2:
    ptype = POINTER(c_double)
 
infd = grass.G_open_cell_old(input, mapset)
inrast = grass.G_allocate_raster_buf(data_type)
inrast = cast(c_void_p(inrast), ptype)

rows = grass.G_window_rows()
cols = grass.G_window_cols()
 
for rown in xrange(rows):
    grass.G_get_raster_row(infd, inrast, rown, data_type)
    print rown, inrast[0:cols]
 
grass.G_close_cell(infd)
grass.G_free(inrast)

-------------- next part --------------
#!/usr/bin/env python
import os, sys, subprocess
from ctypes import *
grass = CDLL("libgrass_gis.so")
rast = CDLL("libgrass_raster.so")

input = sys.argv[1]
 
# initialize
s = subprocess.Popen(['g.version','-r'], stdout=subprocess.PIPE).communicate()[0]
for line in s.splitlines():
    if line.startswith('Revision:'):
        version = '$' + line + '$'
grass.G__gisinit(version, '')
 
# find map in search path
mapset = grass.G_find_raster2(input, '')
mapset = c_char_p(mapset).value
 
# determine the inputmap type (CELL/FCELL/DCELL) */
data_type = rast.Rast_map_type(input, mapset)

if data_type == 0:
    ptype = POINTER(c_int)
elif data_type == 1:
    ptype = POINTER(c_float)
elif data_type == 2:
    ptype = POINTER(c_double)
 
infd = rast.Rast_open_old(input, mapset)
inrast = rast.Rast_allocate_buf(data_type)
inrast = cast(c_void_p(inrast), ptype)

rows = rast.Rast_window_rows()
cols = rast.Rast_window_cols()

for rown in xrange(rows):
    rast.Rast_get_row(infd, inrast, rown, data_type)
    print rown, inrast[0:cols]
 
rast.Rast_close(infd)
grass.G_free(inrast)



More information about the grass-user mailing list