1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
"""!@package grass.script.array
@brief GRASS Python scripting module (rasters with numpy)
Functions to use GRASS rasters with NumPy.
Usage:
@code
map = 'elevation'
x = garray.array()
x.read(map)
# calculate something on array
x[...] = x / 50.
x.write(map + ".new")
@endcode
(C) 2010-2011 by Glynn Clements and the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
@author Glynn Clements
"""
import os
import numpy
import core as grass
# i18N
import gettext
gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
class array(numpy.memmap):
def __new__(cls, dtype = numpy.double):
"""!Define new numpy array
@param cls
@param dtype data type (default: numpy.double)
"""
reg = grass.region()
r = reg['rows']
c = reg['cols']
shape = (r, c)
filename = grass.tempfile()
self = numpy.memmap.__new__(
cls,
filename = filename,
dtype = dtype,
mode = 'w+',
shape = shape)
self.filename = filename
return self
def _close(self):
numpy.memmap._close(self)
if isinstance(self, array):
grass.try_remove(self.filename)
def read(self, mapname, null = None):
"""!Read raster map into array
@param mapname name of raster map to be read
@param null null value
@return 0 on success
@return non-zero code on failure
"""
kind = self.dtype.kind
size = self.dtype.itemsize
if kind == 'f':
flags = 'f'
elif kind in 'biu':
flags = 'i'
else:
raise ValueError(_('Invalid kind <%s>') % kind)
if size not in [1,2,4,8]:
raise ValueError(_('Invalid size <%d>') % size)
return grass.run_command(
'r.out.bin',
flags = flags,
input = mapname,
output = self.filename,
bytes = size,
null = null,
quiet = True)
def write(self, mapname, title = None, null = None, overwrite = None):
"""!Write array into raster map
@param mapname name for raster map
@param title title for raster map
@param null null value
@param overwrite True for overwritting existing raster maps
@return 0 on success
@return non-zero code on failure
"""
kind = self.dtype.kind
size = self.dtype.itemsize
if kind == 'f':
if size == 4:
flags = 'f'
elif size == 8:
flags = 'd'
else:
raise ValueError(_('Invalid FP size <%d>') % size)
size = None
elif kind in 'biu':
if size not in [1,2,4]:
raise ValueError(_('Invalid integer size <%d>') % size)
flags = None
else:
raise ValueError(_('Invalid kind <%s>') % kind)
reg = grass.region()
return grass.run_command(
'r.in.bin',
flags = flags,
input = self.filename,
output = mapname,
title = title,
bytes = size,
anull = null,
overwrite = overwrite,
verbose = True,
north = reg['n'],
south = reg['s'],
east = reg['e'],
west = reg['w'],
rows = reg['rows'],
cols = reg['cols'])
|