[GRASS-dev] problem using pygrass

Pietro peter.zamb at gmail.com
Thu Feb 27 13:06:48 PST 2014


Hi Javier,

On Thu, Feb 27, 2014 at 4:45 PM, Javier Martínez-López
<javi.martinez.lopez at gmail.com> wrote:
> I have a couple of problems when using pygrass for analyzing raster
> maps stored in GRASS with the numpy library:

The RasterNumpy class was quite buggy, I did some changes last week
that should fix some of them, which version are you using?

> - First: I cannot close the maps, since I always get the following error:
>
>>>> import numpy
>>>> import grass.pygrass as pygrass
>>>> demx = pygrass.raster.RasterNumpy('dem')
>>>> demx.open('r')
>>>> dem2 = demx.flatten()
>>>> len(dem2)
> 43860
>>>> demx.close()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/local/grass-7.0.y14/grass-7.0.svn/etc/python/grass/pygrass/raster/__init__.py",
> line 654, in close
>     self._write()
>   File "/usr/local/grass-7.0.y14/grass-7.0.svn/etc/python/grass/pygrass/raster/__init__.py",
> line 597, in _write
>     self.tofile(self.filename)
> ValueError: 43860 requested and 0 written

yes, this bug should be fixed in (r59127), I've tried this code using
the North Carolina mapset:

from grass.pygrass.raster import RasterNumpy
elev = RasterNumpy('elevation')
elev.open()
flt = elev.flatten()
len(flt)
elev.close()

and it works, are you in Linux?

> - Second: when I change the region (with g.region) and read again some
> maps, it uses the same extent as before and I always get a "core dump
> error". I print the region in the script and it has in deed changed
> but I always get maps from the same size as before...

in numpy the raster map is read and load into the memory when you open
the map using the current region, so changing the region when your map
is opened will not affect the raster map. Using the other RasterClass
will easily raise some errors.
So it is not a worth idea change a region when one or more raster maps
are opened, the right way to go is: set the region, do something with
the raster map, close then you are free to change the region again.

I would like to introduce the with statement also for the Region
object, to do something like:

{{{
## change resolution
with Region(nsres=100., ewres=100.) as reg:
    # do something with the changed region
    #

## back to the original region
#
}}}


More information about the grass-dev mailing list