1import os
2import tempfile
3from grass.gunittest.case import TestCase
4from grass.gunittest.main import test
5from grass.script.core import read_command
6
7
8input1 = \
9b"""
10A
11634308.630394 223320.356473
12640640.712946 223092.401501
13641248.592871 217748.123827
14= 10.01 label1
15A
16639576.923077 222256.566604
17639045.028143 216329.737336
18637702.626642 224662.757974
19= -8
20L
21633523.452158 222231.238274
22642565.666041 221218.105066
23641957.786116 222585.834897
24= 3 label2
25"""
26
27
28class TestRInPoly(TestCase):
29
30    rinpoly = 'test_rinpoly'
31
32    @classmethod
33    def setUpClass(cls):
34        cls.use_temp_region()
35        cls.runModule('g.region', raster='elevation')
36
37    @classmethod
38    def tearDownClass(cls):
39        cls.del_temp_region()
40
41    def setUp(self):
42        self.tmpFile = tempfile.NamedTemporaryFile(delete=False)
43
44    def tearDown(self):
45        """Remove rinpoly map after each test method"""
46        self.runModule('g.remove', flags='f', type='raster',
47                       name=self.rinpoly)
48        os.unlink(self.tmpFile.name)
49
50    def testTypeCell(self):
51        """Test type of resulting map"""
52        self.tmpFile.write(input1)
53        self.tmpFile.close()
54        self.assertModule('r.in.poly', input=self.tmpFile.name, output=self.rinpoly, type='CELL')
55        minmax = 'min=-8\nmax=10\ndatatype=CELL'
56        self.assertRasterFitsInfo(raster=self.rinpoly, reference=minmax)
57
58    def testTypeFCell(self):
59        """Test type of resulting map"""
60        self.tmpFile.write(input1)
61        self.tmpFile.close()
62        self.assertModule('r.in.poly', input=self.tmpFile.name, output=self.rinpoly, type='FCELL')
63        minmax = 'min=-8\nmax=10.01\ndatatype=FCELL'
64        self.assertRasterFitsInfo(raster=self.rinpoly, reference=minmax, precision=1e-8)
65
66    def testTypeDCell(self):
67        """Test type of resulting map"""
68        self.tmpFile.write(input1)
69        self.tmpFile.close()
70        self.assertModule('r.in.poly', input=self.tmpFile.name, output=self.rinpoly, type='DCELL')
71        minmax = 'min=-8\nmax=10.01\ndatatype=DCELL'
72        self.assertRasterFitsInfo(raster=self.rinpoly, reference=minmax, precision=1e-8)
73
74    def testTypeCellNull(self):
75        """Test type of resulting map"""
76        self.tmpFile.write(input1)
77        self.tmpFile.close()
78        self.assertModule('r.in.poly', input=self.tmpFile.name, output=self.rinpoly, type='CELL',
79                          null=-8)
80        minmax = 'min=3\nmax=10\ndatatype=CELL'
81        self.assertRasterFitsInfo(raster=self.rinpoly, reference=minmax, precision=1e-8)
82
83    def testTypeDCellNull(self):
84        """Test type of resulting map"""
85        self.tmpFile.write(input1)
86        self.tmpFile.close()
87        self.assertModule('r.in.poly', input=self.tmpFile.name, output=self.rinpoly, type='DCELL',
88                          null=-8)
89        minmax = 'min=3\nmax=10.01\ndatatype=DCELL'
90        self.assertRasterFitsInfo(raster=self.rinpoly, reference=minmax, precision=1e-8)
91
92    def testTypeDCellNull2(self):
93        """Test type of resulting map"""
94        self.tmpFile.write(input1)
95        self.tmpFile.close()
96        self.assertModule('r.in.poly', input=self.tmpFile.name, output=self.rinpoly, type='DCELL',
97                          null=0)
98        minmax = 'min=-8\nmax=10.01\ndatatype=DCELL'
99        self.assertRasterFitsInfo(raster=self.rinpoly, reference=minmax, precision=1e-8)
100
101    def testLabels(self):
102        """Test type of resulting map"""
103        self.tmpFile.write(input1)
104        self.tmpFile.close()
105        self.assertModule('r.in.poly', input=self.tmpFile.name, output=self.rinpoly, type='DCELL')
106        category = read_command('r.category', map=self.rinpoly, values=[-8, 3, 10.01]).strip()
107        self.assertEqual(first="-8\t{newline}3\tlabel2{newline}10.01".format(newline=os.linesep),
108                         second=category, msg="Labels do not match")
109
110
111if __name__ == '__main__':
112    test()
113