[GRASS-dev] grass.script.array.read: reading multiple rasters

Glynn Clements glynn at gclements.plus.com
Thu Jul 31 20:02:59 PDT 2014

Paulo van Breugel wrote:

> To read in a raster with GRASS Python Scripting Library, I can use
> ref = garray.array()
> ref.read('mymap')
> But is it also possible to read in multiple raster layers at once? 
> Something like:
> ref = garray.array()
> ref.read(('map1','map2'))

At present, no.

The grass.script.array module's "array" class is a fairly simple
subclass of numpy.memmap, which stores the data in a memory-mapped

The only extensions provided by the subclass are that the constructor
automatically sets the array size from the current region, and the
addition of read() and write() methods which use r.out.bin/r.in.bin to
export/import GRASS raster maps to/from the mapped file.

> If not, what would be the best way to get an array with data from 
> multiple raster layers.. using a loop?


You can create a 3-dimensional array of the desired size, then read
the individual maps into the layers with e.g.

	import numpy as np
	import grass.script.array as garray

	data = None
	layer = garray.array()

	for i, map in enumerate(maps):
	    if data is None:
	        s = len(maps)
	        r, c = layer.shape
	        data = np.empty((s, r, c), dtype=np.double)
	    data[i,:,:] = layer

	del layer

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-dev mailing list