Example #1
0
    def test2_raster_data_at_points(self):
        # Write a file to test
        f = tempfile.NamedTemporaryFile(suffix='.aai',
                                        prefix='test_misc',
                                        delete=False,
                                        mode='w+t')
        f.write('ncols 3   \r\n')
        f.write('nrows 2 \r\n')
        f.write('xllcorner +0.   \r\n')
        f.write('yllcorner +8. \r\n')
        f.write('cellsize 1   \r\n')
        f.write('NODATA_value -9999 \r\n')
        f.write('1 2 -9999   \r\n')
        f.write('4 5 6')
        f.close()
        # lon 0 - 3
        # lat 8 - 10

        # Just outside the midpoint of all sides
        lon = asarray([-0.0001, 1.5, 3.0001, 1.5])
        lat = asarray([9., 10.00001, 9.0, 7.99999])
        raster = Raster.from_file(f.name)
        data = raster.raster_data_at_points(lon, lat)
        self.assertTrue(numpy.all(numpy.isnan(data)))

        # Inside lower left corner of No data cell

        lon = asarray([2.0001])
        lat = asarray([9.000019])
        raster = Raster.from_file(f.name)
        data = raster.raster_data_at_points(lon, lat)
        self.assertTrue(numpy.all(numpy.isnan(data)))

        os.remove(f.name)
Example #2
0
    def test3_raster_data_from_array(self):
        # A test based on this info;
        # http://en.wikipedia.org/wiki/Esri_grid
        # Let's hope no one edits the data....
        raster = [[-9999, -9999, 5, 2], [-9999, 20, 100, 36],
                  [3, 8, 35, 10], [32, 42, 50, 6],
                  [88, 75, 27, 9], [13, 5, 1, -9999]]
        upper_left_x = 0.
        upper_left_y = 300.
        cell_size = 50.0
        no_data_value = -9999

        # Just outside the midpoint of all sides
        lon = asarray([125, 125, 125, 125, 125, 125])
        lat = asarray([275, 225, 175, 125, 75, 25])

        raster = Raster.from_array(raster, upper_left_x, upper_left_y,
                                   cell_size, no_data_value)
        self.assertEqual(raster.ul_x, 0)
        self.assertEqual(raster.ul_y, 300)
        self.assertEqual(raster.x_pixel, 50)
        self.assertEqual(raster.y_pixel, -50)
        self.assertEqual(raster.x_size, 4)
        self.assertEqual(raster.y_size, 6)

        data = raster.raster_data_at_points(lon, lat)
        self.assertTrue(allclose(data, asarray([5.0, 100.0, 35.0,
                                                50.0, 27.0, 1.0])))

        # testing extent
        min_long, min_lat, max_long, max_lat = raster.extent()
        self.assertEqual(min_long, 0)
        self.assertEqual(min_lat, 0)
        self.assertEqual(max_long, 200)
        self.assertEqual(max_lat, 300)
Example #3
0
    def test1_raster_data_at_points(self):
        # Write a file to test
        # pylint: disable=R0801
        f = tempfile.NamedTemporaryFile(suffix='.aai',
                                        prefix='test_misc',
                                        delete=False,
                                        mode='w+t')
        f.write('ncols 3  \r\n')
        f.write('nrows 2  \r\n')
        f.write('xllcorner +0.  \r\n')
        f.write('yllcorner +8.  \r\n')
        f.write('cellsize 1  \r\n')
        f.write('NODATA_value -9999  \r\n')
        f.write('1 2 -9999  \r\n')
        f.write('4 5 6  ')
        f.close()
        # lon 0 - 3
        # lat 8 - 10

        lon = asarray([0, 0.9, 1.999])
        lat = asarray([9.9, 9.1, 8.9])

        raster = Raster.from_file(f.name)
        data = raster.raster_data_at_points(lon, lat)
        self.assertEqual(raster.ul_x, 0)
        self.assertEqual(raster.ul_y, 10)
        self.assertEqual(raster.x_pixel, 1)
        self.assertEqual(raster.y_pixel, -1)
        self.assertEqual(raster.x_size, 3)
        self.assertEqual(raster.y_size, 2)
        self.assertTrue(allclose(data, asarray([1., 1., 5.])))

        lon = asarray([0.0001, 0.0001, 2.999, 2.999])
        lat = asarray([8.0001, 9.999, 9.999, 8.0001])
        data = raster.raster_data_at_points(lon, lat)
        index_g = numpy.array([0, 1, 3])
        self.assertTrue(allclose(data[index_g],
                                 asarray([4., 1., 6.])))
        self.assertTrue(numpy.isnan(data[2]))

        os.remove(f.name)