Esempio n. 1
0
    def test_simple_interpolation(self):
        """ Very simple test with a raster object like thus:
             1  2  3  4
             5  6  7  8
             9  10 11 12
             13 14 15 16

            LLC is 0,0 and upper right is 4,4.
            The data are stored in cell centres and we ask for a few coords
            """
        rci = RasterInterpolator(test_file_name1)
        point1 = [0.0, 0.0] # should error
        point2 = [1.5, 2.0] # should return 8
        point3 = [2.0, 3.0] # should return 4.5
        point4 = [3,1] # should return 13.5
        point5 = [1.999999, 2.999999] # should return nearly 4.5
        point6 = [0.5, 0.5] # should return 13 (llc)
        point7 = [1.0, 3.49] # should be 1.5
        self.assertRaises(RasterInterpolatorError, rci.get_val, point2)
        rci.set_band()
        self.assertEqual(rci.get_val(point2),8.0)
        self.assertAlmostEqual(rci.get_val(point3),4.5)
        self.assertAlmostEqual(rci.get_val(point5),4.5,5)
        self.assertEqual(rci.get_val(point4),13.5)
        self.assertEqual(rci.get_val(point6),13)
        self.assertAlmostEqual(rci.get_val(point7),1.5, delta=0.1)
        self.assertRaises(CoordinateError,rci.get_val, point1)
Esempio n. 2
0
    def test_simple_interpolation_limits(self):
        """ Very simple test with a raster object like thus:
             1  2  3  4
             5  6  7  8
             9  10 11 12
             13 14 15 16

            LLC is 0,0 and upper right is 4,4.
            But here we set upper and lower limits
            """
        rci = RasterInterpolator(test_file_name1,minmax=[2,10])
        point1 = [0.0, 0.0] # should error
        point2 = [1.5, 2.0] # should return 8
        point3 = [2.0, 3.0] # should return 4.5
        point4 = [3,1] # should return 13.5, limited to 10
        point5 = [1.999999, 2.999999] # should return nearly 4.5
        point6 = [0.5, 0.5] # should return 13 (llc), limited to 10
        point7 = [1.0, 3.49] # should be 1.5, but limited to 2
        self.assertRaises(RasterInterpolatorError, rci.get_val, point2)
        rci.set_band()
        self.assertEqual(rci.get_val(point2),8.0)
        self.assertAlmostEqual(rci.get_val(point3),4.5)
        self.assertAlmostEqual(rci.get_val(point5),4.5,5)
        self.assertEqual(rci.get_val(point4),10)
        self.assertEqual(rci.get_val(point6),10)
        self.assertEqual(rci.get_val(point7),2)
        self.assertRaises(CoordinateError,rci.get_val, point1)
Esempio n. 3
0
    def test_simple_distance_setres(self):
        """ Very simple test with a raster object like thus:
             1  2  3  4
             5  6  7  8
             9  10 11 12
             13 14 15 16
            (on a 10x10 grid, not 4x4 as above...only so much space
            in comments!)

            LLC is 0,0 and upper right is 4,4. (hence dx is 0.25)
            The data are stored in cell centres and we ask for a few coords.
            We check that the value is 1 at the distance, 0 at the boundary.
            Finally, test that the file is written and readable
            """
        rbuff = CreateBuffer(test_file_name1, 1.5, over=30)
        point1 = [0.03, 3.85]  # should return ~0
        point2 = [1.65, 2]  # should be 1
        point3 = [2, 2]  # should be 1
        point4 = [0.76, 2]  # should be ~0.5
        rbuff.make_buffer(temp_file)
        # we now read in the buffer using the rasterinterpolator class
        rci = RasterInterpolator(temp_file)
        rci.set_band()
        self.assertAlmostEqual(rci.get_val(point1), 0.0, delta=0.01)
        self.assertEqual(rci.get_val(point2), 1.0)
        self.assertEqual(rci.get_val(point3), 1.0)
        self.assertAlmostEqual(rci.get_val(point4), 0.5, delta=0.01)
Esempio n. 4
0
 def test_real_data(self):
     """Using gebco cut out to test interpolation
     """
     raster_file = "tests/real_data/gebco_uk.tif"
     rci = RasterInterpolator(raster_file)
     rci.set_band()
     points = ([842996., 5848009.],
               [834009., 5848207.],
               [832856., 5848273.],
               [828840., 5848306.],
               [823178., 5848503.],
               )
     expected = [-21.3,
                 -25.3,
                 -28.6,
                 -13.5,
                 -13.2,
                 ]
     for p,e in zip(points, expected):
         self.assertAlmostEqual(rci.get_val(p),e,delta=0.75)
Esempio n. 5
0
    def test_simple_distance_nan(self):
        """ Very simple test with a raster object like thus:
             1  2  3  4
             5  6  7  8
             9  NAN NAN 12
             13 14 15 16
            (on a 20x20 grid, not 4x4 as above...only so much space
            in comments!). The slightly-left-of-centre has been
            replaced by NaN

            LLC is 0,0 and upper right is 4,4. (hence dx is 0.25)
            The data are stored in cell centres and we ask for a few coords.
            We check that the value is 1 at the distance, 0 at the boundary
            and, for this test, the value is 0 in the centre.

            The NaNs are in the centre, so the buffer is expanded by one pixel
            automatically to prevent interpolation issues
            """
        rbuff = CreateBuffer(test_file_name2, 1.0)
        point1 = [0.2, 3.85]  # should return ~0
        point2 = [0.7, 2]  # should be 0.2
        point3 = [1.7, 2]  # should be 0 (in the centre)
        rbuff.make_buffer(temp_file)
        # we now read in the buffer using the rasterinterpolator class
        rci = RasterInterpolator(temp_file)
        rci.set_band()
        self.assertAlmostEqual(rci.get_val(point1), 0.0, delta=0.03)
        self.assertAlmostEqual(rci.get_val(point2), 0.2, delta=0.03)
        self.assertEqual(rci.get_val(point3), 0.0)
Esempio n. 6
0
    def test_point_in(self):
        """ Very simple test with a raster object like thus:
             1  2  3  4
             5  6  7  8
             9  10 11 12
             13 14 15 16

            LLC is 0,0 and upper right is 4,4.
            we ask for point in, outside and on boundary
            """
        rci = RasterInterpolator(test_file_name1)
        point1 = [0.0, 0.0] # should return False
        point2 = [2.0, 2.0] # should return True
        point3 = [-1.0, -1.0] # should return False
        point4 = [0.5, 0.5] # should return True
        point5 = [0.0001, 0.0001] # False - outside first cell centre
        rci.set_band()
        self.assertFalse(rci.point_in(point1))
        self.assertTrue(rci.point_in(point2))
        self.assertFalse(rci.point_in(point3))
        self.assertTrue(rci.point_in(point4))
        self.assertFalse(rci.point_in(point5))