test_numpy.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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(
  26. "r.mapcalc",
  27. expression="%s = float(row() + (10.0 * col()))" % (cls.name),
  28. overwrite=True,
  29. )
  30. cls.numpy_obj = raster2numpy(cls.name)
  31. @classmethod
  32. def tearDownClass(cls):
  33. """Remove the generated vector map, if exist"""
  34. cls.runModule("g.remove", flags="f", type="raster", name=cls.name)
  35. cls.del_temp_region()
  36. def test_type(self):
  37. self.assertTrue(str(self.numpy_obj.dtype), "float32")
  38. def test_len(self):
  39. self.assertTrue(len(self.numpy_obj), 40)
  40. self.assertTrue(len(self.numpy_obj[0]), 60)
  41. def test_write(self):
  42. ran = random([40, 60])
  43. numpy2raster(ran, "FCELL", self.name, True)
  44. self.assertTrue(check_raster(self.name))
  45. if __name__ == "__main__":
  46. test()