| 1 | #!/usr/bin/env python
|
|---|
| 2 |
|
|---|
| 3 | ############################################################################
|
|---|
| 4 | #
|
|---|
| 5 | # MODULE: r_forestfrag_trivial
|
|---|
| 6 | # AUTHOR: Vaclav Petras
|
|---|
| 7 | # PURPOSE: Test using example from the Riitters et al. 2000 paper
|
|---|
| 8 | # COPYRIGHT: (C) 2016 by Vaclav Petras and the GRASS Development Team
|
|---|
| 9 | #
|
|---|
| 10 | # This program is free software under the GNU General Public
|
|---|
| 11 | # License (>=v2). Read the file COPYING that comes with GRASS
|
|---|
| 12 | # for details.
|
|---|
| 13 | #
|
|---|
| 14 | #############################################################################
|
|---|
| 15 |
|
|---|
| 16 | # This test led to discovery of #3067 (r68717) which was an r.mapcalc
|
|---|
| 17 | # row indexing bug.
|
|---|
| 18 |
|
|---|
| 19 | from grass.gunittest.case import TestCase
|
|---|
| 20 | from grass.gunittest.main import test
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | FOREST_RIITTERS = """\
|
|---|
| 24 | north: 10
|
|---|
| 25 | south: 8
|
|---|
| 26 | east: 20
|
|---|
| 27 | west: 18
|
|---|
| 28 | rows: 3
|
|---|
| 29 | cols: 3
|
|---|
| 30 | 1 0 1
|
|---|
| 31 | 1 1 0
|
|---|
| 32 | 1 1 0
|
|---|
| 33 | """
|
|---|
| 34 |
|
|---|
| 35 | FRAG_RIITTERS = """\
|
|---|
| 36 | north: 10
|
|---|
| 37 | south: 8
|
|---|
| 38 | east: 20
|
|---|
| 39 | west: 18
|
|---|
| 40 | rows: 3
|
|---|
| 41 | cols: 3
|
|---|
| 42 | null: N
|
|---|
| 43 | N N N
|
|---|
| 44 | N 4 N
|
|---|
| 45 | N N N
|
|---|
| 46 | """
|
|---|
| 47 |
|
|---|
| 48 | PF_RIITTERS = """\
|
|---|
| 49 | north: 10
|
|---|
| 50 | south: 8
|
|---|
| 51 | east: 20
|
|---|
| 52 | west: 18
|
|---|
| 53 | rows: 3
|
|---|
| 54 | cols: 3
|
|---|
| 55 | null: N
|
|---|
| 56 | N N N
|
|---|
| 57 | N 0.67 N
|
|---|
| 58 | N N N
|
|---|
| 59 | """
|
|---|
| 60 |
|
|---|
| 61 | PFF_RIITTERS = """\
|
|---|
| 62 | north: 10
|
|---|
| 63 | south: 8
|
|---|
| 64 | east: 20
|
|---|
| 65 | west: 18
|
|---|
| 66 | rows: 3
|
|---|
| 67 | cols: 3
|
|---|
| 68 | null: N
|
|---|
| 69 | N N N
|
|---|
| 70 | N 0.45 N
|
|---|
| 71 | N N N
|
|---|
| 72 | """
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | class TestForestFragTrivial(TestCase):
|
|---|
| 76 | # TODO: replace by unified handing of maps
|
|---|
| 77 | to_remove = []
|
|---|
| 78 | forest = 'rff_test_forest_trivial'
|
|---|
| 79 | forest_frag = 'rff_test_forest_frag_trivial'
|
|---|
| 80 | forest_frag_ref = 'rff_test_forest_frag_trivial_ref'
|
|---|
| 81 |
|
|---|
| 82 | def setUp(self):
|
|---|
| 83 | self.use_temp_region()
|
|---|
| 84 |
|
|---|
| 85 | def tearDown(self):
|
|---|
| 86 | self.del_temp_region()
|
|---|
| 87 | if 0 and self.to_remove:
|
|---|
| 88 | self.runModule('g.remove', flags='f', type='raster',
|
|---|
| 89 | name=','.join(self.to_remove), verbose=True)
|
|---|
| 90 |
|
|---|
| 91 | def forest_frag_general(self, forest, window, reference):
|
|---|
| 92 | self.runModule('r.in.ascii', input='-', stdin_=forest,
|
|---|
| 93 | output=self.forest)
|
|---|
| 94 | self.to_remove.append(self.forest)
|
|---|
| 95 | self.runModule('g.region', raster=self.forest)
|
|---|
| 96 | self.assertRasterMinMax(self.forest,
|
|---|
| 97 | refmin=0, refmax=1)
|
|---|
| 98 | self.runModule('r.in.ascii', input='-', stdin_=reference,
|
|---|
| 99 | output=self.forest_frag_ref)
|
|---|
| 100 | self.to_remove.append(self.forest_frag_ref)
|
|---|
| 101 | pf_ref = self.forest_frag_ref + '_pf'
|
|---|
| 102 | self.runModule('r.in.ascii', input='-', stdin_=PF_RIITTERS,
|
|---|
| 103 | output=pf_ref)
|
|---|
| 104 | self.to_remove.append(pf_ref)
|
|---|
| 105 | pff_ref = self.forest_frag_ref + '_pff'
|
|---|
| 106 | self.runModule('r.in.ascii', input='-', stdin_=PFF_RIITTERS,
|
|---|
| 107 | output=pff_ref)
|
|---|
| 108 | self.to_remove.append(pff_ref)
|
|---|
| 109 | # just check if the reference is all right
|
|---|
| 110 | theoretical_min = 0
|
|---|
| 111 | theoretical_max = 6
|
|---|
| 112 | self.assertRasterMinMax(self.forest_frag_ref,
|
|---|
| 113 | refmin=theoretical_min,
|
|---|
| 114 | refmax=theoretical_max)
|
|---|
| 115 | ref_univar = dict(null_cells=8)
|
|---|
| 116 | self.assertRasterFitsUnivar(raster=self.forest_frag_ref,
|
|---|
| 117 | reference=ref_univar, precision=0)
|
|---|
| 118 |
|
|---|
| 119 | # actually run the module
|
|---|
| 120 | pf = self.forest_frag + '_pf'
|
|---|
| 121 | pff = self.forest_frag + '_pff'
|
|---|
| 122 | self.assertModule('r.forestfrag', input=self.forest,
|
|---|
| 123 | output=self.forest_frag, window=window,
|
|---|
| 124 | pf=pf, pff=pff)
|
|---|
| 125 | self.assertRasterExists(self.forest_frag)
|
|---|
| 126 | self.to_remove.append(self.forest_frag)
|
|---|
| 127 | self.assertRasterExists(pf)
|
|---|
| 128 | self.to_remove.append(pf)
|
|---|
| 129 | self.assertRasterExists(pff)
|
|---|
| 130 | self.to_remove.append(pff)
|
|---|
| 131 |
|
|---|
| 132 | # check the basic properties
|
|---|
| 133 | self.assertRasterMinMax(self.forest_frag,
|
|---|
| 134 | refmin=theoretical_min,
|
|---|
| 135 | refmax=theoretical_max)
|
|---|
| 136 | ref_univar = dict(null_cells=0)
|
|---|
| 137 | self.assertRasterFitsUnivar(raster=self.forest_frag,
|
|---|
| 138 | reference=ref_univar, precision=0)
|
|---|
| 139 |
|
|---|
| 140 | self.runModule('g.region', zoom=pff_ref)
|
|---|
| 141 |
|
|---|
| 142 | # check cell by cell
|
|---|
| 143 |
|
|---|
| 144 | self.assertRastersNoDifference(actual=pf,
|
|---|
| 145 | reference=pf_ref,
|
|---|
| 146 | precision=0.01)
|
|---|
| 147 |
|
|---|
| 148 | self.assertRastersNoDifference(actual=pff,
|
|---|
| 149 | reference=pff_ref,
|
|---|
| 150 | precision=0.01)
|
|---|
| 151 |
|
|---|
| 152 | self.assertRastersNoDifference(actual=self.forest_frag,
|
|---|
| 153 | reference=self.forest_frag_ref,
|
|---|
| 154 | precision=0) # it's CELL type
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | def test_riitters(self):
|
|---|
| 158 | self.forest_frag_general(FOREST_RIITTERS, 3, FRAG_RIITTERS)
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | if __name__ == '__main__':
|
|---|
| 162 | test()
|
|---|