[GRASS-dev] problem using pygrass

Pietro peter.zamb at gmail.com
Tue Mar 4 07:25:49 PST 2014


Hi Javier,

the RasterNumpy class is different from other Raster classes in
pygrass, because inherit from the numpy.memmap array.

On Tue, Mar 4, 2014 at 2:38 PM, Javier Martínez-López
<javi.martinez.lopez at gmail.com> wrote:
> out = pygrass.raster.RasterNumpy('eco_pa22')

In the RasterNumpy class you need to specify the raster type with
mtype when you instance the object.
and the open method should be call only after you close the map.

so a working session in the python interpreter could be:

{{{
In [1]: from grass.pygrass.raster import RasterNumpy

In [2]: new = RasterNumpy('newraster', mtype='FCELL')

In [3]: import numpy as np

In [4]: new[:] = np.random.randn(*new.shape)

In [5]: new.min()
Out[5]: RasterNumpy(-5.076531410217285, dtype=float32)

In [6]: new.max()
Out[6]: RasterNumpy(4.842053413391113, dtype=float32)

In [7]: new[:3, :4]
Out[7]:
RasterNumpy([[-0.74486554, -1.80636668, -0.62290537,  1.10075259],
       [ 0.65961069, -0.71186149,  1.31982756,  0.70869118],
       [ 2.11516094, -1.15944469,  0.71266735,  0.52502483]], dtype=float32)

In [8]: new.close()

}}}

> Am I doing something wrong?

You should not use the open method with the RasterNumpy class when you
instantiate the object, and be careful that you have to set the name
of your raster map before to close the map or when you close the map,
see this example:

{{{
In [9]: new.open()  # re-open the closed map

In [10]: new[:3, :4]
Out[10]:
RasterNumpy([[-0.74486554, -1.80636668, -0.62290537,  1.10075259],
       [ 0.65961069, -0.71186149,  1.31982756,  0.70869118],
       [ 2.11516094, -1.15944469,  0.71266735,  0.52502483]], dtype=float32)

In [11]: norm = (new - new.min())/(new.max() - new.min())  # do something

In [12]: norm.min()
Out[12]: RasterNumpy(0.0, dtype=float32)

In [13]: norm.max()
Out[13]: RasterNumpy(1.0, dtype=float32)

In [14]: norm.name  # the name is not set

In [15]: norm.name = 'norm'  # set the name

In [16]: norm.close()  # then close

In [17]: norm.close('norm')  # give the name and close the raster,
only from: r59192 [0]

}}}

> Is there an easy way to get the raster map
> out as a tiff file instead to avoid this problem?

Why not using r.out.tiff [1]?

Thank you for testing.

Pietro


[0] http://trac.osgeo.org/grass/changeset/59192
[1] http://grass.osgeo.org/grass70/manuals/r.out.tiff.html


More information about the grass-dev mailing list