| 1 | #!/usr/bin/env python
|
|---|
| 2 |
|
|---|
| 3 | ############################################################################
|
|---|
| 4 | #
|
|---|
| 5 | # MODULE: r_forestfrag_ncspm
|
|---|
| 6 | # AUTHOR: Vaclav Petras
|
|---|
| 7 | # PURPOSE: Test using NC SPM data
|
|---|
| 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 | from grass.gunittest.case import TestCase
|
|---|
| 17 | from grass.gunittest.main import test
|
|---|
| 18 | import grass.script.raster as gr
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | class TestForestFrag(TestCase):
|
|---|
| 22 | # TODO: replace by unified handing of maps
|
|---|
| 23 | to_remove = []
|
|---|
| 24 | landclass = 'landclass96' # in full NC SPM
|
|---|
| 25 | forest = 'rff_test_forest'
|
|---|
| 26 | forest_frag = 'rff_test_forest_frag'
|
|---|
| 27 |
|
|---|
| 28 | def setUp(self):
|
|---|
| 29 | self.use_temp_region()
|
|---|
| 30 | self.runModule('g.region', raster=self.landclass)
|
|---|
| 31 | gr.mapcalc("{f} = if({c} == 5, 1, 0)".format(c=self.landclass,
|
|---|
| 32 | f=self.forest))
|
|---|
| 33 | self.to_remove.append(self.forest)
|
|---|
| 34 |
|
|---|
| 35 | def tearDown(self):
|
|---|
| 36 | self.del_temp_region()
|
|---|
| 37 | if self.to_remove:
|
|---|
| 38 | self.runModule('g.remove', flags='f', type='raster',
|
|---|
| 39 | name=','.join(self.to_remove), verbose=True)
|
|---|
| 40 |
|
|---|
| 41 | def test_3x3(self):
|
|---|
| 42 | self.assertModule('r.forestfrag', input=self.forest,
|
|---|
| 43 | output=self.forest_frag, window=3)
|
|---|
| 44 | self.assertRasterExists(self.forest_frag)
|
|---|
| 45 | self.to_remove.append(self.forest_frag)
|
|---|
| 46 | self.assertRasterMinMax(self.forest_frag, refmin=0, refmax=6)
|
|---|
| 47 | # we don't have a check sum test for raster, so we just test all
|
|---|
| 48 | ref = dict(north=228527.25, south=215018.25,
|
|---|
| 49 | east=644971, west=629980,
|
|---|
| 50 | nsres=28.5, ewres=28.5, rows=474, cols=526,
|
|---|
| 51 | cells=249324, datatype='CELL', ncats=6)
|
|---|
| 52 | self.assertRasterFitsInfo(raster=self.forest_frag,
|
|---|
| 53 | reference=ref, precision=0.0006)
|
|---|
| 54 | ref = dict(n=249323, null_cells=1, cells=249324, min=0, max=5,
|
|---|
| 55 | range=5, mean=2.2925, mean_of_abs=2.2925,
|
|---|
| 56 | stddev=2.3564, variance=5.5527, coeff_var=102.7879,
|
|---|
| 57 | sum=571572)
|
|---|
| 58 | self.assertRasterFitsUnivar(raster=self.forest_frag,
|
|---|
| 59 | reference=ref, precision=0.0006)
|
|---|
| 60 |
|
|---|
| 61 | if __name__ == '__main__':
|
|---|
| 62 | test()
|
|---|