def test_calc_admin1(self): """test function _calc_admin1 for Switzerland. All required functions are tested in unit tests""" resolution = 300 curr_country = 'CHE' country_info = dict() admin1_info = dict() country_info[curr_country], admin1_info[curr_country] = \ lp._get_country_info(curr_country) curr_shp = lp._get_country_shape(curr_country, 0) for cntry_iso, cntry_val in country_info.items(): _, total_asset_val = lp.gdp(cntry_iso, 2016, curr_shp) cntry_val.append(total_asset_val) lp._get_gdp2asset_factor(country_info, 2016, curr_shp, fin_mode='gdp') cut_bbox = lp._get_country_shape(curr_country, 1)[0] all_coords = lp._litpop_box2coords(cut_bbox, resolution, 1) mask = lp._mask_from_shape(curr_shp, resolution=resolution,\ points2check=all_coords) litpop_data = lp._get_litpop_box(cut_bbox, resolution, 0, 2016, \ [3, 0]) litpop_curr = litpop_data[mask.sp_index.indices] lon, lat = zip(*np.array(all_coords)[mask.sp_index.indices]) litpop_curr = lp._calc_admin1(curr_country, country_info[curr_country],\ admin1_info[curr_country], litpop_curr,\ list(zip(lon, lat)), resolution, 0, conserve_cntrytotal=0, \ check_plot=0, masks_adm1=[], return_data=1) self.assertEqual(len(litpop_curr), 699) self.assertAlmostEqual(max(litpop_curr), 80178713117.21936, places=3)
def test_get_country_bbox(self): """test _get_country_shape function: get bbox for Swaziland""" bbox, lat, lon = lp._get_country_shape('SWZ', only_geo=1) self.assertEqual(len(lat), len(lon)) self.assertIn(len(lat), [84, 85, 86, 87]) self.assertEqual(len(bbox), 4) self.assertIn(_rnd(32.117398316000106), _rnd(bbox))
def test_mask_from_shape(self): """test function _mask_from_shape for Swaziland""" curr_country = 'SWZ' curr_shp = lp._get_country_shape(curr_country, 0) mask = lp._mask_from_shape(curr_shp, resolution=60) self.assertEqual(mask.sp_index.indices.size, 5591) self.assertTrue(mask.values.max()) self.assertIn(140 and 7663, mask.sp_index.indices)
def test_getcountryshape_NZL(self): """test _get_country_shape function: get shape and bbox for New Zealand""" shp = lp._get_country_shape('NZL', only_geo=0) self.assertEqual(_rnd(shp.bbox[0]), _rnd(-177.95799719999985)) self.assertEqual(len(shp.parts), 25) # self.assertEqual(max(shp.parts), 4500) # 20190403: fails on Jenkins self.assertIn(len(shp.points), [4507, 4508, 4509, 4510]) self.assertEqual(_rnd(min(shp.points)[1]), _rnd(-29.22275155999992)) self.assertEqual(shp.shapeType, 5)
def test_litpop_box2coords(self): """test function _litpop_box2coords for Taiwan""" curr_country = 'TWN' resolution = 3000 cut_bbox = lp._get_country_shape(curr_country, 1)[0] all_coords = lp._litpop_box2coords(cut_bbox, resolution, 1) self.assertEqual(len(all_coords), 25) self.assertIn(_rnd(117.91666666666666) and _rnd(22.08333333333333),\ _rnd(min(all_coords)))
def test_getcountryshape_BEL(self): """test _get_country_shape function: get shape and bbox for Belgium""" shp = lp._get_country_shape('BEL', only_geo=0) self.assertEqual(len(shp.bbox), 4) self.assertEqual(_rnd(shp.bbox[0]), _rnd(2.5217999276904663)) self.assertEqual(_rnd(shp.bbox[3]), _rnd(51.49623769100013)) self.assertIn(0, shp.parts) self.assertEqual(len(shp.parts), 1) self.assertIn(len(shp.points), np.arange(640, 660)) self.assertEqual(_rnd(max(shp.points)[0]), _rnd(6.374525187000074)) self.assertEqual(shp.shapeType, 5)
def test_get_litpop_box(self): """test functions _litpop_box2coords and _get_litpop_box for Taiwan""" curr_country = 'TWN' resolution = 3000 cut_bbox = lp._get_country_shape(curr_country, 1)[0] all_coords = lp._litpop_box2coords(cut_bbox, resolution, 1) self.assertEqual(len(all_coords), 25) self.assertTrue(117.91666666666666 and 22.08333333333333 in min(all_coords)) litpop_data = lp._get_litpop_box(cut_bbox, resolution, 0, 2016, [1, 1]) self.assertEqual(len(litpop_data), 25) self.assertIn(max(litpop_data), [544316890, 594091108.0, 594091108])