[GRASS-user] passing raster map to Python object

Dear all,

I am building a simulation python code that read a raster maps on grass mapset.
As my maps is not so large (512x512 pixels) I dump it on a text file
(using r.out.ascii), after I read the text file on python, parse it using a function that
I wrote, and after create a matrix-like object on Python.

I would like to know if I can read my raster map on python wihout
save it on external text file. Case yes, a short example are welcome.

cheers

milton
brazil=toronto

Milton Cezar Ribeiro wrote:

I am building a simulation python code that read a
raster maps on grass mapset.
As my maps is not so large (512x512 pixels) I dump it
on a text file
(using r.out.ascii), after I read the text file on
python, parse it using a function that
I wrote, and after create a matrix-like object on Python.

I would like to know if I can read my raster map on python wihout
save it on external text file. Case yes, a short example are welcome.

you might look at NumPy http://numpy.scipy.org/ + NumPtr python
extension. NumPtr can be found in the swig/python/ dir in the grass
source code. (also examples/m.distance + rasteraccess.py there)

NumPy is also used by the wxGUI profile tool.

SciPy is another thing to check out.

please post back to the list with what you learn :slight_smile: & if you succeed
maybe add a page to the grass wiki with steps?

Hamish

Hi Hamish,

Thanks for your encourager’s email.
I started to play with numpy for other purposes,
but I will give a look in deep for this.

If I get success I will be happy report it here.

bests

milton

2009/8/12 Hamish <hamish_b@yahoo.com>

Milton Cezar Ribeiro wrote:

I am building a simulation python code that read a
raster maps on grass mapset.
As my maps is not so large (512x512 pixels) I dump it
on a text file
(using r.out.ascii), after I read the text file on
python, parse it using a function that
I wrote, and after create a matrix-like object on Python.

I would like to know if I can read my raster map on python wihout
save it on external text file. Case yes, a short example are welcome.

you might look at NumPy http://numpy.scipy.org/ + NumPtr python
extension. NumPtr can be found in the swig/python/ dir in the grass
source code. (also examples/m.distance + rasteraccess.py there)

NumPy is also used by the wxGUI profile tool.

SciPy is another thing to check out.

please post back to the list with what you learn :slight_smile: & if you succeed
maybe add a page to the grass wiki with steps?

Hamish

Milton Cezar Ribeiro wrote:

Thanks for your encourager's email.
I started to play with numpy for other purposes,
but I will give a look in deep for this.

If I get success I will be happy report it here.

it should work. one thing, be sure to translate NULL cells to numpy.nan,

e.g.

import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]], np.float64)
x

array([[ 1., 2., 3.],
       [ 4., 5., 6.]])

x[0,1]

2.0

x[0,1] = np.nan
x

array([[ 1. , nan, 3. ],
       [ 4. , 5. , 6. ]])

see swig/python/examples/rasteraccess.py for reading raster row by row.

Hamish