Skip to content

Commit

Permalink
Add good lat/lon values for unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Chahan Kropf committed Nov 24, 2023
1 parent 510f53f commit e108432
Showing 1 changed file with 55 additions and 163 deletions.
218 changes: 55 additions & 163 deletions climada/hazard/centroids/test/test_vec_ras.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,193 +36,85 @@

DATA_DIR = CONFIG.hazard.test_data.dir()


# Note: the coordinates are not directly on the cities, the region id and on land
# otherwise do not work correctly. It is only the closest point.
VEC_LON = np.array([
-59.6250000000000, -59.6250000000000, -59.6250000000000, -59.5416666666667, -59.5416666666667,
-59.4583333333333, -60.2083333333333, -60.2083333333333, -60.2083333333333, -60.2083333333333,
-60.2083333333333, -60.2083333333333, -60.2083333333333, -60.2083333333333, -60.2083333333333,
-60.2083333333333, -60.2083333333333, -60.2083333333333, -60.2083333333333, -60.2083333333333,
-60.2083333333333, -60.1250000000000, -60.1250000000000, -60.1250000000000, -60.1250000000000,
-60.1250000000000, -60.1250000000000, -60.1250000000000, -60.1250000000000, -60.1250000000000,
-60.1250000000000, -60.1250000000000, -60.1250000000000, -60.1250000000000, -60.1250000000000,
-60.1250000000000, -60.1250000000000, -60.0416666666667, -60.0416666666667, -60.0416666666667,
-60.0416666666667, -60.0416666666667, -60.0416666666667, -60.0416666666667, -60.0416666666667,
-60.0416666666667, -60.0416666666667, -60.0416666666667, -60.0416666666667, -60.0416666666667,
-60.0416666666667, -60.0416666666667, -60.0416666666667, -59.9583333333333, -59.9583333333333,
-59.9583333333333, -59.9583333333333, -59.9583333333333, -59.9583333333333, -59.9583333333333,
-59.9583333333333, -59.9583333333333, -59.9583333333333, -59.9583333333333, -59.9583333333333,
-59.9583333333333, -59.9583333333333, -59.9583333333333, -59.9583333333333, -59.8750000000000,
-59.8750000000000, -59.8750000000000, -59.8750000000000, -59.8750000000000, -59.8750000000000,
-59.8750000000000, -59.8750000000000, -59.8750000000000, -59.8750000000000, -59.8750000000000,
-59.8750000000000, -59.8750000000000, -59.8750000000000, -59.8750000000000, -59.8750000000000,
-59.7916666666667, -59.7916666666667, -59.7916666666667, -59.7916666666667, -59.7916666666667,
-59.7916666666667, -59.7916666666667, -59.7916666666667, -59.7916666666667, -59.7916666666667,
-59.7916666666667, -59.7916666666667, -59.7916666666667, -59.7916666666667, -59.7916666666667,
-59.7916666666667, -59.7083333333333, -59.7083333333333, -59.7083333333333, -59.7083333333333,
-59.7083333333333, -59.7083333333333, -59.7083333333333, -59.7083333333333, -59.7083333333333,
-59.7083333333333, -59.7083333333333, -59.7083333333333, -59.7083333333333, -59.7083333333333,
-59.7083333333333, -59.7083333333333, -59.6250000000000, -59.6250000000000, -59.6250000000000,
-59.6250000000000, -59.6250000000000, -59.6250000000000, -59.6250000000000, -59.6250000000000,
-59.6250000000000, -59.6250000000000, -59.6250000000000, -59.6250000000000, -59.6250000000000,
-59.5416666666667, -59.5416666666667, -59.5416666666667, -59.5416666666667, -59.5416666666667,
-59.5416666666667, -59.5416666666667, -59.5416666666667, -59.5416666666667, -59.5416666666667,
-59.5416666666667, -59.5416666666667, -59.5416666666667, -59.5416666666667, -59.4583333333333,
-59.4583333333333, -59.4583333333333, -59.4583333333333, -59.4583333333333, -59.4583333333333,
-59.4583333333333, -59.4583333333333, -59.4583333333333, -59.4583333333333, -59.4583333333333,
-59.4583333333333, -59.4583333333333, -59.4583333333333, -59.4583333333333, -59.3750000000000,
-59.3750000000000, -59.3750000000000, -59.3750000000000, -59.3750000000000, -59.3750000000000,
-59.3750000000000, -59.3750000000000, -59.3750000000000, -59.3750000000000, -59.3750000000000,
-59.3750000000000, -59.3750000000000, -59.3750000000000, -59.3750000000000, -59.3750000000000,
-59.2916666666667, -59.2916666666667, -59.2916666666667, -59.2916666666667, -59.2916666666667,
-59.2916666666667, -59.2916666666667, -59.2916666666667, -59.2916666666667, -59.2916666666667,
-59.2916666666667, -59.2916666666667, -59.2916666666667, -59.2916666666667, -59.2916666666667,
-59.2916666666667, -59.2083333333333, -59.2083333333333, -59.2083333333333, -59.2083333333333,
-59.2083333333333, -59.2083333333333, -59.2083333333333, -59.2083333333333, -59.2083333333333,
-59.2083333333333, -59.2083333333333, -59.2083333333333, -59.2083333333333, -59.2083333333333,
-59.2083333333333, -59.2083333333333, -59.1250000000000, -59.1250000000000, -59.1250000000000,
-59.1250000000000, -59.1250000000000, -59.1250000000000, -59.1250000000000, -59.1250000000000,
-59.1250000000000, -59.1250000000000, -59.1250000000000, -59.1250000000000, -59.1250000000000,
-59.1250000000000, -59.1250000000000, -59.1250000000000, -59.0416666666667, -59.0416666666667,
-59.0416666666667, -59.0416666666667, -59.0416666666667, -59.0416666666667, -59.0416666666667,
-59.0416666666667, -59.0416666666667, -59.0416666666667, -59.0416666666667, -59.0416666666667,
-59.0416666666667, -59.0416666666667, -59.0416666666667, -58.9583333333333, -58.9583333333333,
-58.9583333333333, -58.9583333333333, -58.9583333333333, -58.9583333333333, -58.9583333333333,
-58.9583333333333, -58.9583333333333, -58.9583333333333, -58.9583333333333, -58.9583333333333,
-58.9583333333333, -58.9583333333333, -61.0416666666667, -61.0416666666667, -61.0416666666667,
-61.0416666666667, -61.0416666666667, -61.0416666666667, -61.0416666666667, -60.6250000000000,
-60.6250000000000, -60.6250000000000, -60.6250000000000, -60.6250000000000, -60.6250000000000,
-60.6250000000000, -60.2083333333333, -60.2083333333333, -60.2083333333333, -60.2083333333333,
-59.7916666666667, -59.7916666666667, -59.7916666666667, -59.7916666666667, -59.3750000000000,
-59.3750000000000, -59.3750000000000, -59.3750000000000, -58.9583333333333, -58.9583333333333,
-58.9583333333333, -58.9583333333333, -58.5416666666667, -58.5416666666667, -58.5416666666667,
-58.5416666666667, -58.5416666666667, -58.5416666666667, -58.5416666666667, -58.1250000000000,
-58.1250000000000, -58.1250000000000, -58.1250000000000, -58.1250000000000, -58.1250000000000,
-58.1250000000000,
-175.1883, #Tonga, Nuku'alofa, TON, 776
178.433, #Fidji, Suva, FJI, 242
18.95, #Norway, Tromso, NOR, 578
0, # Ocean, 0
166.6778, #Antarctica, McMurdo station, ATA, 010
-78.5833 #Ecuador, Quito, ECU, 218
])

VEC_LAT = np.array([
13.125, 13.20833333, 13.29166667, 13.125, 13.20833333, 13.125, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667, 13.375,
13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667, 13.375,
13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667, 13.375,
13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667, 13.375,
13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667, 13.375,
13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667, 13.375,
13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667, 13.375,
13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333,
12.79166667, 12.875, 12.95833333, 13.04166667, 13.375, 13.45833333, 13.54166667, 13.625,
13.70833333, 13.79166667, 12.54166667, 12.625, 12.70833333, 12.79166667, 12.875, 12.95833333,
13.04166667, 13.29166667, 13.375, 13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667,
12.54166667, 12.625, 12.70833333, 12.79166667, 12.875, 12.95833333, 13.04166667, 13.20833333,
13.29166667, 13.375, 13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667,
12.625, 12.70833333, 12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333,
13.29166667, 13.375, 13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667,
12.625, 12.70833333, 12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333,
13.29166667, 13.375, 13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667,
12.625, 12.70833333, 12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333,
13.29166667, 13.375, 13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667,
12.625, 12.70833333, 12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333,
13.29166667, 13.375, 13.45833333, 13.54166667, 13.625, 13.70833333, 13.79166667, 12.54166667,
12.625, 12.70833333, 12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333,
13.29166667, 13.375, 13.45833333, 13.54166667, 13.625, 13.70833333, 12.54166667, 12.625,
12.70833333, 12.79166667, 12.875, 12.95833333, 13.04166667, 13.125, 13.20833333, 13.29166667,
13.375, 13.45833333, 13.54166667, 13.625, 11.875, 12.29166667, 12.70833333, 13.125,
13.54166667, 13.95833333, 14.375, 11.875, 12.29166667, 12.70833333, 13.125, 13.54166667,
13.95833333, 14.375, 11.875, 12.29166667, 13.95833333, 14.375, 11.875, 12.29166667,
13.95833333, 14.375, 11.875, 12.29166667, 13.95833333, 14.375, 11.875, 12.29166667,
13.95833333, 14.375, 11.875, 12.29166667, 12.70833333, 13.125, 13.54166667, 13.95833333,
14.375, 11.875, 12.29166667, 12.70833333, 13.125, 13.54166667, 13.95833333, 14.375
-21.1736, #Tonga, Nuku'alofa
-18.133, #Fidji, Suvaå
69.6833, #Norway, Tromso
0, #Ocean
-77.85, #Antarctica, McMurdo station
-0.25, #Ecuador, Quito
])

ON_LAND = np.array([True, True, True, False, True, True])
REGION_ID = np.array([776, 242, 578, 0, 10, 218])

def data_vector():
vec_data = gpd.GeoDataFrame({
'geometry': [Point(lon, lat) for lon, lat in zip(VEC_LON, VEC_LAT)],
'lon': VEC_LON,
'lat': VEC_LAT
}, crs='epsg:32632')
return vec_data.lat.values, vec_data.lon.values, vec_data.geometry
TEST_CRS = 'EPSG:4326'
ALT_CRS = 'eps:32632' #Europe

class TestVector(unittest.TestCase):
"""Test CentroidsVector class"""

def test_from_lat_lon_pass(self):
"""Test from_lat_lon"""
centr = Centroids.from_lat_lon(VEC_LAT, VEC_LON)
self.assertTrue(np.allclose(centr.lat, VEC_LAT))
self.assertTrue(np.allclose(centr.lon, VEC_LON))
self.assertTrue(u_coord.equal_crs(centr.crs, DEF_CRS))
self.assertTrue(u_coord.equal_crs(centr.geometry.crs, DEF_CRS))
self.assertEqual(centr.geometry.size, 0)
def setUp(self):
self.centr = Centroids(latitude=VEC_LAT,longitude=VEC_LON, crs=TEST_CRS)

centr.set_area_pixel()
self.assertEqual(centr.geometry.size, centr.lat.size)
def test_init_pass(self):
"""Test from_lat_lon"""
self.assertTrue(np.allclose(self.centr.lat, VEC_LAT))
self.assertTrue(np.allclose(self.centr.lon, VEC_LON))
self.assertTrue(u_coord.equal_crs(self.centr.crs, DEF_CRS))
self.assertTrue(u_coord.equal_crs(self.centr.geometry.crs, DEF_CRS))

def test_ne_crs_geom_pass(self):
"""Test _ne_crs_geom"""
lat, lon, geometry = data_vector()
centr = Centroids(lat=lat, lon=lon, geometry=geometry)
xy_vec = centr._ne_crs_geom()
lon, lat = xy_vec.geometry[:].x.values, xy_vec.geometry[:].y.values
self.assertAlmostEqual(4.51072194, lon[0])
self.assertAlmostEqual(0.00011838, lat[0])
self.assertAlmostEqual(4.5107354, lon[-1])
self.assertAlmostEqual(0.0001297, lat[-1])
natural_earth_geom = self.centr._ne_crs_geom()
self.assertEqual(natural_earth_geom.crs, u_coord.NE_CRS)

def test_dist_coast_pass(self):
"""Test set_dist_coast"""
lat, lon, geometry = data_vector()
centr = Centroids(lat=lat, lon=lon, geometry=geometry)
centr.geometry.crs = 'epsg:4326'
centr.set_dist_coast()
self.assertAlmostEqual(2594.2070842031694, centr.dist_coast[1])
self.assertAlmostEqual(166295.87602398323, centr.dist_coast[-2])
dist_coast = self.centr.get_dist_coast()\
# Just checking that the output doesnt change over time.
REF_VALUES = np.array([
5.55578093e+05, 1.64066475e+03, 2.00703835e+03,
4.82264614e+02, 4.50037266e+03, 1.08610274e+05
])
np.testing.assert_array_almost_equal(dist_coast, REF_VALUES, decimal=3)

def test_region_id_pass(self):
"""Test set_region_id"""
lat, lon, geometry = data_vector()
geometry.crs = 'epsg:4326'
centr = Centroids(lat=lat, lon=lon, geometry=geometry)
centr.set_region_id()
self.assertEqual(np.count_nonzero(centr.region_id), 6)
self.assertEqual(centr.region_id[0], 52) # 052 for barbados
self.centr.set_region_id()
np.testing.assert_array_equal(
self.centr.region_id,
REGION_ID
)

def test_on_land(self):
"""Test set_on_land"""
lat, lon, geometry = data_vector()
geometry.crs = 'epsg:4326'
centr = Centroids(lat=lat, lon=lon, geometry=geometry)
centr.set_on_land()
centr.set_region_id()
centr.region_id[centr.region_id > 0] = 1
self.assertTrue(np.array_equal(centr.on_land.astype(int),
centr.region_id))
self.centr.set_on_land()
np.testing.assert_array_equal(
self.centr.on_land,
ON_LAND
)

def test_remove_duplicate_pass(self):
"""Test remove_duplicate_points"""
lat, lon, geometry = data_vector()
geometry.crs = 'epsg:4326'
centr = Centroids(lat=lat, lon=lon, geometry=geometry)
# create duplicates manually:
centr.geometry.values[100] = centr.geometry.values[101]
centr.geometry.values[120] = centr.geometry.values[101]
centr.geometry.values[5] = Point([-59.7, 12.5])
centr.geometry.values[133] = Point([-59.7, 12.5])
centr.geometry.values[121] = Point([-59.7, 12.5])
centr.lon = centr.geometry.apply(lambda pt: pt.x).values
centr.lat = centr.geometry.apply(lambda pt: pt.y).values
self.assertEqual(centr.size, 296)
rem_centr = centr.remove_duplicate_points()
self.assertEqual(centr.size, 296)
self.assertEqual(rem_centr.size, 292)
rem2_centr = rem_centr.remove_duplicate_points()
self.assertEqual(rem_centr.size, 292)
self.assertEqual(rem2_centr.size, 292)
centr = Centroids(latitude = np.hstack([VEC_LAT, VEC_LAT]),
longitude = np.hstack([VEC_LON , VEC_LON]),
crs=TEST_CRS)
self.assertTrue(centr.gdf.shape[0] == 2*self.centr.gdf.shape[0])
rem_centr = Centroids.remove_duplicate_points(centr)
self.assertTrue(self.centr == rem_centr)

def test_area_pass(self):
"""Test set_area"""
Expand All @@ -236,7 +128,7 @@ def test_area_pass(self):
'lat': yy.flatten(),
}, crs={'proj': 'cea'})

centr = Centroids.from_lat_lon(vec_data.lat.values, vec_data.lon.values)
centr = Centroids(latitude=vec_data.lat.values, longitude=vec_data.lon.values)
centr.geometry = vec_data.geometry
centr.set_area_pixel()
self.assertTrue(np.allclose(centr.area_pixel, np.ones(centr.size)))
Expand Down Expand Up @@ -264,7 +156,7 @@ def test_set_lat_lon_to_meta_pass(self):
"""Test set_lat_lon_to_meta"""
lat, lon, geometry = data_vector()
geometry.crs = 'epsg:4326'
centr = Centroids(lat=lat, lon=lon, geometry=geometry)
centr = Centroids(latitude=lat, longitude=lon, crs='epsg:4326')

centr.set_lat_lon_to_meta()
self.assertTrue(u_coord.equal_crs(centr.meta['crs'], 'epsg:4326'))
Expand All @@ -281,7 +173,7 @@ def test_get_pixel_polygons_pass(self):
"""Test calc_pixels_polygons"""
lat, lon, geometry = data_vector()
geometry.crs = 'epsg:4326'
centr = Centroids(lat=lat, lon=lon, geometry=geometry)
centr = Centroids(latitude=lat, longitude=lon, geometry=geometry)
poly = centr.calc_pixels_polygons()
self.assertIsInstance(poly[0], Polygon)
self.assertTrue(np.allclose(poly.centroid[:].y.values, centr.lat))
Expand Down

0 comments on commit e108432

Please sign in to comment.