Esempio n. 1
0
    def test_centroids_att_two_pass(self):
        """ Test set all attributes centroids """
        centr_bang = Centroids()
        centr_bang.set_raster_from_pnt_bounds((89, 21.5, 90, 23), 0.01)
        _set_centroids_att(centr_bang,
                           dist_coast_decay=True,
                           dem_product='SRTM3',
                           as_pixel=True)
        self.assertEqual(centr_bang.dist_coast.size, centr_bang.size)
        self.assertEqual(centr_bang.elevation.size, centr_bang.size)
        self.assertAlmostEqual(centr_bang.elevation.max(), 22.743055555555557)

        centr_bang.elevation = np.array([])
        centr_bang.dist_coast = np.array([])
        _set_centroids_att(centr_bang,
                           dist_coast_decay=True,
                           dem_product='SRTM3',
                           as_pixel=False)
        self.assertEqual(centr_bang.dist_coast.size, centr_bang.size)
        self.assertEqual(centr_bang.elevation.size, centr_bang.size)
        self.assertAlmostEqual(centr_bang.elevation.max(), 28.0)

        centr_bang.set_meta_to_lat_lon()
        centr_bang.meta = dict()
        centr_bang.elevation = np.array([])
        centr_bang.dist_coast = np.array([])
        _set_centroids_att(centr_bang,
                           dist_coast_decay=True,
                           dem_product='SRTM3')
        self.assertEqual(centr_bang.dist_coast.size, centr_bang.size)
        self.assertEqual(centr_bang.elevation.size, centr_bang.size)
        self.assertAlmostEqual(centr_bang.elevation.max(), 28.0)
    def test_centroids_resolution_pass(self):
        """ Test _centroids_resolution """
        bf = BushFire()
        res_centr = bf._centroids_resolution(DEF_CENTROIDS[0])
        res = get_resolution(DEF_CENTROIDS[0].lat, DEF_CENTROIDS[0].lon)
        self.assertAlmostEqual((res[0] + res[1]) / 2, res_centr)

        centroids = Centroids()
        centroids.meta = {'transform': Affine(0.5, 0, -180, 0, -0.5, 90)}
        res_centr = bf._centroids_resolution(centroids)
        self.assertAlmostEqual(0.5, res_centr)
Esempio n. 3
0
    def test_get_closest_point(self):
        """Test get_closest_point"""
        for y_sign in [1, -1]:
            centr_ras = Centroids()
            centr_ras.meta = {
                'width':
                10,
                'height':
                20,
                'transform':
                rasterio.Affine(0.5, 0, 0.1, 0, y_sign * 0.6, y_sign * (-0.3)),
                'crs':
                DEF_CRS,
            }
            test_data = np.array([
                [0.4, 0.1, 0.35, 0.0, 0],
                [-0.1, 0.2, 0.35, 0.0, 0],
                [2.2, 0.1, 2.35, 0.0, 4],
                [1.4, 2.5, 1.35, 2.4, 42],
                [5.5, -0.1, 4.85, 0.0, 9],
            ])
            test_data[:, [1, 3]] *= y_sign
            for x_in, y_in, x_out, y_out, idx_out in test_data:
                x, y, idx = centr_ras.get_closest_point(x_in, y_in)
                self.assertEqual(x, x_out)
                self.assertEqual(y, y_out)
                self.assertEqual(idx, idx_out)
                self.assertEqual(centr_ras.lon[idx], x)
                self.assertEqual(centr_ras.lat[idx], y)

        centr_ras = Centroids()
        centr_ras.set_lat_lon(np.array([0, 0.2, 0.7]),
                              np.array([-0.4, 0.2, 1.1]))
        x, y, idx = centr_ras.get_closest_point(0.1, 0.0)
        self.assertEqual(x, 0.2)
        self.assertEqual(y, 0.2)
        self.assertEqual(idx, 1)
Esempio n. 4
0
    def test_centroids_check_pass(self):
        """Test vector data in Centroids"""
        data_vec = TestVector.data_vector()
        centr = Centroids()
        centr.lat, centr.lon, centr.geometry = data_vec
        centr.check()

        self.assertEqual(centr.crs, data_vec[2].crs)
        self.assertIsInstance(centr.total_bounds, tuple)
        for i in range(4):
            self.assertEqual(centr.total_bounds[i],
                             data_vec[2].total_bounds[i])

        self.assertIsInstance(centr.lat, np.ndarray)
        self.assertIsInstance(centr.lon, np.ndarray)
        self.assertIsInstance(centr.coord, np.ndarray)
        self.assertTrue(np.array_equal(centr.lat, VEC_LAT))
        self.assertTrue(np.array_equal(centr.lon, VEC_LON))
        self.assertTrue(
            np.array_equal(centr.coord,
                           np.array([VEC_LAT, VEC_LON]).transpose()))
        self.assertEqual(centr.size, VEC_LON.size)

        centr.area_pixel = np.array([1])
        with self.assertRaises(ValueError):
            centr.check()

        centr.area_pixel = np.array([])
        centr.dist_coast = np.array([1])
        with self.assertRaises(ValueError):
            centr.check()

        centr.dist_coast = np.array([])
        centr.on_land = np.array([1])
        with self.assertRaises(ValueError):
            centr.check()

        centr.on_land = np.array([])
        centr.region_id = np.array([1])
        with self.assertRaises(ValueError):
            centr.check()

        centr.region_id = np.array([])
        centr.lat = np.array([1])
        with self.assertRaises(ValueError):
            centr.check()

        centr.lat = np.array([])
        centr.lon = np.array([1])
        with self.assertRaises(ValueError):
            centr.check()

        centr.lon = np.array([])
        centr.geometry = gpd.GeoSeries(np.ones(1))
        with self.assertRaises(ValueError):
            centr.check()

        cen = Centroids()
        cen.meta = {
            'width': 10,
            'height': 20,
            'transform': rasterio.Affine(0.1, 0, 0, 0, 0.1, 0),
            'crs': DEF_CRS,
        }
        with self.assertRaises(ValueError):
            cen.check()