[GRASS-SVN] r65803 - in grass/trunk/lib/python/pygrass/raster: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jul 31 05:55:13 PDT 2015


Author: lucadelu
Date: 2015-07-31 05:55:13 -0700 (Fri, 31 Jul 2015)
New Revision: 65803

Added:
   grass/trunk/lib/python/pygrass/raster/testsuite/test_numpy.py
Modified:
   grass/trunk/lib/python/pygrass/raster/__init__.py
Log:
pygrass: added functions to convert raster to numpy array and vice versa; added also tests

Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py	2015-07-31 06:39:20 UTC (rev 65802)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py	2015-07-31 12:55:13 UTC (rev 65803)
@@ -479,3 +479,32 @@
         random_map.put_row(row_buf)
     random_map.close()
     return random_map
+
+
+def raster2numpy(rastname, mapset=''):
+    """Return a numpy array from a raster map
+
+    :param str rastname: the name of raster map
+    :parar str mapser: the name of mapset containig raster map
+    """
+    with RasterRow(rastname, mapset=mapset, mode='r') as rast:
+        return np.array(rast)
+
+
+def numpy2raster(array, mtype, rastname, overwrite=False):
+    """Save a numpy array to a raster map
+
+    :param obj array: a numpy array
+    :param obj mtype: the datatype of array
+    :param str rastername: the name of output map
+    :param bool overwrite: True to overwrite existing map
+    """
+    reg = Region()
+    if (reg.rows, reg.cols) != array.shape:
+        msg = "Region and array are different: %r != %r"
+        raise TypeError(msg % ((reg.rows, reg.cols), array.shape))
+    with RasterRow(rastname, mode='w', mtype=mtype, overwrite=overwrite) as new:
+        newrow = Buffer((array.shape[1],), mtype=mtype)
+        for row in array:
+            newrow[:] = row[:]
+            new.put_row(newrow)

Added: grass/trunk/lib/python/pygrass/raster/testsuite/test_numpy.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/testsuite/test_numpy.py	                        (rev 0)
+++ grass/trunk/lib/python/pygrass/raster/testsuite/test_numpy.py	2015-07-31 12:55:13 UTC (rev 65803)
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu Jul 30 18:27:22 2015
+
+ at author: lucadelu
+"""
+from grass.gunittest.case import TestCase
+from grass.gunittest.main import test
+from numpy.random import random
+from grass.pygrass.raster import raster2numpy, numpy2raster, RasterRow
+
+
+def check_raster(name):
+    r = RasterRow(name)
+    try:
+        r.open(mode='r')
+        r.close()
+        return True
+    except:
+        return False
+
+
+class NumpyTestCase(TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        """Create a not empty table instance"""
+
+        cls.name = 'elevation'
+        cls.tmp = 'tmp' + cls.name
+        cls.use_temp_region()
+        cls.runModule('g.region', raster=cls.name)
+        cls.numpy_obj = raster2numpy(cls.name)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the generated vector map, if exist"""
+        from grass.pygrass.modules.shortcuts import general as g
+        g.remove(type='raster', name=cls.tmp, flags='f')
+        cls.del_temp_region()
+
+    def test_type(self):
+        self.assertTrue(str(self.numpy_obj.dtype), 'float32')
+
+    def test_len(self):
+        self.assertTrue(len(self.numpy_obj), 1350)
+        self.assertTrue(len(self.numpy_obj[0]), 1500)
+
+    def test_write(self):
+        ran = random([1350, 1500])
+        numpy2raster(ran, 'FCELL', self.tmp, True)
+        self.assertTrue(check_raster(self.tmp))
+
+if __name__ == '__main__':
+    test()



More information about the grass-commit mailing list