test_row_above_below_bug.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: test_row_above_below_bug.py
  5. # AUTHOR: Vaclav Petras
  6. # PURPOSE: Show bug reported in #3067
  7. # COPYRIGHT: (C) 2016 by Vaclav Petras and the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. #
  13. #############################################################################
  14. # #3067
  15. # r.mapcalc gives wrong result when neighborhood modifier takes a cell
  16. # above first
  17. # https://trac.osgeo.org/grass/ticket/3067
  18. from grass.gunittest.case import TestCase
  19. from grass.gunittest.main import test
  20. INPUT = """\
  21. north: 10
  22. south: 8
  23. east: 20
  24. west: 18
  25. rows: 3
  26. cols: 3
  27. 1 0 1
  28. 1 1 0
  29. 1 1 0
  30. """
  31. OUTPUT = """\
  32. north: 10
  33. south: 8
  34. east: 20
  35. west: 18
  36. rows: 3
  37. cols: 3
  38. null: *
  39. * * *
  40. 2 1 1
  41. * * *
  42. """
  43. class TestRowAboveAndBelowBug(TestCase):
  44. # TODO: replace by unified handing of maps
  45. to_remove = []
  46. input = 'r_mapcalc_test_input'
  47. output = 'r_mapcalc_test_output'
  48. output_ref = 'r_mapcalc_test_output_ref'
  49. def setUp(self):
  50. self.use_temp_region()
  51. self.runModule('r.in.ascii', input='-', stdin_=INPUT,
  52. output=self.input, overwrite=True)
  53. self.to_remove.append(self.input)
  54. self.runModule('g.region', raster=self.input)
  55. self.runModule('r.in.ascii', input='-', stdin_=OUTPUT,
  56. output=self.output_ref, overwrite=True)
  57. self.to_remove.append(self.output_ref)
  58. def tearDown(self):
  59. self.del_temp_region()
  60. if 0 and self.to_remove:
  61. self.runModule('g.remove', flags='f', type='raster',
  62. name=','.join(self.to_remove), verbose=True)
  63. def r_mapcalc_with_test(self, expression):
  64. """Expects just RHS and inputs as ``{m}`` for format function"""
  65. expression = expression.format(m=self.input)
  66. expression = "{0} = {1}".format(self.output, expression)
  67. self.assertModule('r.mapcalc', expression=expression, overwrite=True)
  68. self.assertRasterExists(self.output)
  69. self.to_remove.append(self.output)
  70. ref_univar = dict(null_cells=6, cells=9)
  71. self.assertRasterFitsUnivar(raster=self.output,
  72. reference=ref_univar, precision=0)
  73. self.assertRastersNoDifference(actual=self.output,
  74. reference=self.output_ref,
  75. precision=0) # it's CELL type
  76. def test_below_above(self):
  77. self.r_mapcalc_with_test("{m}[1,0] + {m}[-1,0]")
  78. def test_above_below(self):
  79. self.r_mapcalc_with_test("{m}[-1,0] + {m}[1,0]")
  80. if __name__ == '__main__':
  81. test()