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()
def test_select_pass(self): """Test set_vector""" centr = Centroids() centr.set_lat_lon(VEC_LAT, VEC_LON) centr.region_id = np.zeros(VEC_LAT.size) centr.region_id[[100, 200]] = 10 fil_centr = centr.select(10) self.assertEqual(fil_centr.size, 2) self.assertEqual(fil_centr.lat[0], VEC_LAT[100]) self.assertEqual(fil_centr.lat[1], VEC_LAT[200]) self.assertEqual(fil_centr.lon[0], VEC_LON[100]) self.assertEqual(fil_centr.lon[1], VEC_LON[200]) self.assertTrue(np.array_equal(fil_centr.region_id, np.ones(2) * 10))
def test_select_extent_pass(self): """Test select extent""" centr = Centroids() centr.set_lat_lon(np.array([-5, -3, 0, 3, 5]), np.array([-180, -175, -170, 170, 175])) centr.check() centr.region_id = np.zeros(len(centr.lat)) ext_centr = centr.select(extent=[-175, -170, -5, 5]) np.testing.assert_array_equal(ext_centr.lon, np.array([-175, -170])) np.testing.assert_array_equal(ext_centr.lat, np.array([-3, 0])) # Cross antimeridian, version 1 ext_centr = centr.select(extent=[170, -175, -5, 5]) np.testing.assert_array_equal(ext_centr.lon, np.array([-180, -175, 170, 175])) np.testing.assert_array_equal(ext_centr.lat, np.array([-5, -3, 3, 5])) # Cross antimeridian, version 2 ext_centr = centr.select(extent=[170, 185, -5, 5]) np.testing.assert_array_equal(ext_centr.lon, np.array([-180, -175, 170, 175])) np.testing.assert_array_equal(ext_centr.lat, np.array([-5, -3, 3, 5]))
def test_centroids_check_pass(self): """Test vector data in Centroids""" data_vec = data_vector() centr = Centroids() centr.lat, centr.lon, centr.geometry = data_vec centr.check() self.assertTrue(u_coord.equal_crs(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()