[GRASS-SVN] r63612 - grass/trunk/lib/python/pygrass/raster
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Dec 19 05:51:54 PST 2014
Author: zarch
Date: 2014-12-19 05:51:54 -0800 (Fri, 19 Dec 2014)
New Revision: 63612
Modified:
grass/trunk/lib/python/pygrass/raster/__init__.py
Log:
pygrass: remove RasterNumpy class.
Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py 2014-12-19 13:09:11 UTC (rev 63611)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py 2014-12-19 13:51:54 UTC (rev 63612)
@@ -457,217 +457,6 @@
self._fd = None
-FLAGS = {1: {'b': 'i', 'i': 'i', 'u': 'i'},
- 2: {'b': 'i', 'i': 'i', 'u': 'i'},
- 4: {'f': 'f', 'i': 'i', 'b': 'i', 'u': 'i'},
- 8: {'f': 'd'}, }
-
-
-class RasterNumpy(np.memmap, RasterAbstractBase):
- """Raster_cached_narray": Inherits "Raster_abstract_base" and
- "numpy.memmap". Its purpose is to allow numpy narray like access to
- raster maps without loading the map into the main memory.
-
- * Behaves like a numpy array and supports all kind of mathematical
- operations: __add__, ...
- * Overrides the open and close methods
- * Be aware of the 2Gig file size limit
-
- >>> import grass.pygrass as pygrass
- >>> elev = pygrass.raster.RasterNumpy('elevation')
- >>> elev.open()
- >>> elev[:5, :3]
- RasterNumpy([[ 141.99613953, 141.27848816, 141.37904358],
- [ 142.90461731, 142.39450073, 142.68611145],
- [ 143.81854248, 143.54707336, 143.83972168],
- [ 144.56524658, 144.58493042, 144.86477661],
- [ 144.99488831, 145.22894287, 145.57142639]], dtype=float32)
- >>> el = elev < 144
- >>> el[:5, :3]
- RasterNumpy([[1, 1, 1],
- [1, 1, 1],
- [1, 1, 1],
- [0, 0, 0],
- [0, 0, 0]], dtype=int32)
- >>> el.exist()
- False
- >>> el.close('elev_bool', overwrite=True)
- >>> el.exist()
- True
- >>> el.remove()
- """
- def __new__(cls, name, mapset="", mtype='CELL', mode='r+',
- overwrite=False):
- reg = Region()
- shape = (reg.rows, reg.cols)
- mapset = libgis.G_find_raster(name, mapset)
- gtype = None
- if mapset:
- # map exist, set the map type
- gtype = libraster.Rast_map_type(name, mapset)
- mtype = RTYPE_STR[gtype]
- filename = grasscore.tempfile()
- obj = np.memmap.__new__(cls, filename=filename,
- dtype=RTYPE[mtype]['numpy'],
- mode=mode,
- shape=shape)
- obj.mtype = mtype.upper()
- obj.gtype = gtype if gtype else RTYPE[mtype]['grass type']
- obj._rows = reg.rows
- obj._cols = reg.cols
- obj.filename = filename
- obj._name = name
- obj.mapset = mapset
- obj.reg = reg
- obj.overwrite = overwrite
- return obj
-
- def __array_finalize__(self, obj):
- if hasattr(obj, '_mmap'):
- self._mmap = obj._mmap
- self.filename = grasscore.tempfile()
- self.offset = obj.offset
- self.mode = obj.mode
- self._rows = obj._rows
- self._cols = obj._cols
- self._name = None
- self.mapset = ''
- self.reg = obj.reg
- self.overwrite = obj.overwrite
- self.mtype = obj.mtype
- self._fd = obj._fd
- else:
- self._mmap = None
-
- def _get_mode(self):
- return self._mode
-
- def _set_mode(self, mode):
- if mode.lower() not in ('r', 'w+', 'r+', 'c'):
- raise ValueError(_("Mode type: {0} not supported.").format(mode))
- self._mode = mode
-
- mode = property(fget=_get_mode, fset=_set_mode,
- doc="Set or obtain the opening mode of raster")
-
- def __array_wrap__(self, out_arr, context=None):
- """See:
- http://docs.scipy.org/doc/numpy/user/
- basics.subclassing.html#array-wrap-for-ufuncs"""
- if out_arr.dtype.kind in 'bui':
- # there is not support for boolean maps, so convert into integer
- out_arr = out_arr.astype(np.int32)
- out_arr.mtype = 'CELL'
- #out_arr.p = out_arr.ctypes.data_as(out_arr.pointer_type)
- return np.ndarray.__array_wrap__(self, out_arr, context)
-
- def __init__(self, name, *args, **kargs):
- ## Private attribute `_fd` that return the file descriptor of the map
- self._fd = None
- rows, cols = self._rows, self._cols
- RasterAbstractBase.__init__(self, name)
- self._rows, self._cols = rows, cols
-
- def __unicode__(self):
- return RasterAbstractBase.__unicode__(self)
-
- def __str__(self):
- return self.__unicode__()
-
- def _get_flags(self, size, kind):
- if size in FLAGS:
- if kind in FLAGS[size]:
- return size, FLAGS[size][kind]
- else:
- raise ValueError(_('Invalid type {0}'.format(kind)))
- else:
- raise ValueError(_('Invalid size {0}'.format(size)))
-
- def _read(self):
- """!Read raster map into array
-
- :return: 0 on success
- :return: non-zero code on failure
- """
- with RasterRow(self.name, self.mapset, mode='r') as rst:
- buff = rst[0]
- for i in range(len(rst)):
- self[i] = rst.get_row(i, buff)
-
- def _write(self, name, overwrite):
- """Write the numpy array into map
- """
- if not self.exist() or self.mode != 'r':
- self.flush()
- buff = Buffer(self[0].shape, mtype=self.mtype)
- with RasterRow(name, self.mapset, mode='w',
- mtype=self.mtype, overwrite=overwrite) as rst:
- for i in range(len(rst)):
- buff[:] = self[i][:]
- rst.put_row(buff[:])
- self.name = name
-
- def open(self, mtype='', null=None, overwrite=None):
- """Open the map, if the map already exist: determine the map type
- and copy the map to the segment files;
- else, open a new segment map.
-
- :param mtype: specify the map type, valid only for new maps: CELL,
- FCELL, DCELL;
- :type mtype: str
- :param null:
- :type null:
- :param overwrite: use this flag to set the overwrite mode of existing
- raster maps
- :type overwrite: bool
- """
- if overwrite is not None:
- self.overwrite = overwrite
- self.null = null
- # rows and cols already set in __new__
- if self.exist():
- self.info.read()
- self.cats.mtype = self.mtype
- self.cats.read()
- self.hist.read()
- self._read()
- else:
- if mtype:
- self.mtype = mtype
- self._gtype = RTYPE[self.mtype]['grass type']
- # set _fd, because this attribute is used to check
- # if the map is open or not
- self._fd = 1
-
- def close(self, name='', overwrite=False):
- """Function to close the map
-
- :param name: the name of raster
- :type name: str
- """
- if self.is_open():
- name = name if name else self.name
- if not name:
- raise RuntimeError('Raster name not set neither '
- 'given as parameter.')
- self._write(name, overwrite)
- os.remove(self.filename)
- self._fd = None
-
- def get_value(self, point, region=None):
- """This method returns the pixel value of a given pair of coordinates:
-
- :param point: pair of coordinates in tuple object
- :type point: tuple
- :param region: the region to crop the request
- :type region: Region object
- """
- if not region:
- region = Region()
- x, y = utils.coor2pixel(point.coords(), region)
- return self[x][y]
-
-
def random_map_only_columns(mapname, mtype, overwrite=True, factor=100):
region = Region()
random_map = RasterRow(mapname)
More information about the grass-commit
mailing list