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_area_pass(self): """Test set_area""" ulx, xres, lrx = 60, 1, 90 uly, yres, lry = 0, 1, 20 xx, yy = np.meshgrid(np.arange(ulx + xres / 2, lrx, xres), np.arange(uly + yres / 2, lry, yres)) vec_data = gpd.GeoDataFrame(crs={'proj': 'cea'}) vec_data['geometry'] = list(zip(xx.flatten(), yy.flatten())) vec_data['geometry'] = vec_data['geometry'].apply(Point) vec_data['lon'] = xx.flatten() vec_data['lat'] = yy.flatten() centr = Centroids() centr.set_lat_lon(vec_data.lat.values, vec_data.lon.values) centr.geometry = vec_data.geometry centr.set_area_pixel() self.assertTrue(np.allclose(centr.area_pixel, np.ones(centr.size)))
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()