test_numpy.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Jul 30 18:27:22 2015
  4. @author: lucadelu
  5. """
  6. from grass.gunittest.case import TestCase
  7. from grass.gunittest.main import test
  8. from numpy.random import random
  9. from grass.pygrass.raster import raster2numpy, numpy2raster, RasterRow
  10. def check_raster(name):
  11. r = RasterRow(name)
  12. try:
  13. r.open(mode='r')
  14. r.close()
  15. return True
  16. except:
  17. return False
  18. class NumpyTestCase(TestCase):
  19. name = "RasterRowTestCase_map"
  20. @classmethod
  21. def setUpClass(cls):
  22. """Create test raster map and region"""
  23. cls.use_temp_region()
  24. cls.runModule("g.region", n=40, s=0, e=60, w=0, res=1)
  25. cls.runModule("r.mapcalc",
  26. expression="%s = float(row() + (10.0 * col()))" % (cls.name),
  27. overwrite=True)
  28. cls.numpy_obj = raster2numpy(cls.name)
  29. @classmethod
  30. def tearDownClass(cls):
  31. """Remove the generated vector map, if exist"""
  32. cls.runModule("g.remove", flags='f', type='raster',
  33. name=cls.name)
  34. cls.del_temp_region()
  35. def test_type(self):
  36. self.assertTrue(str(self.numpy_obj.dtype), 'float32')
  37. def test_len(self):
  38. self.assertTrue(len(self.numpy_obj), 40)
  39. self.assertTrue(len(self.numpy_obj[0]), 60)
  40. def test_write(self):
  41. ran = random([40, 60])
  42. numpy2raster(ran, 'FCELL', self.name, True)
  43. self.assertTrue(check_raster(self.name))
  44. if __name__ == '__main__':
  45. test()