test_numpy.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. @classmethod
  20. def setUpClass(cls):
  21. """Create a not empty table instance"""
  22. cls.name = 'elevation'
  23. cls.tmp = 'tmp' + cls.name
  24. cls.use_temp_region()
  25. cls.runModule('g.region', raster=cls.name)
  26. cls.numpy_obj = raster2numpy(cls.name)
  27. @classmethod
  28. def tearDownClass(cls):
  29. """Remove the generated vector map, if exist"""
  30. from grass.pygrass.modules.shortcuts import general as g
  31. g.remove(type='raster', name=cls.tmp, flags='f')
  32. cls.del_temp_region()
  33. def test_type(self):
  34. self.assertTrue(str(self.numpy_obj.dtype), 'float32')
  35. def test_len(self):
  36. self.assertTrue(len(self.numpy_obj), 1350)
  37. self.assertTrue(len(self.numpy_obj[0]), 1500)
  38. def test_write(self):
  39. ran = random([1350, 1500])
  40. numpy2raster(ran, 'FCELL', self.tmp, True)
  41. self.assertTrue(check_raster(self.tmp))
  42. if __name__ == '__main__':
  43. test()