|
| 1 | +import pytest |
| 2 | +from dataclasses import dataclass |
| 3 | +from shapely.geometry import Polygon, MultiPolygon |
| 4 | +from dem_handler.utils.spatial import ( |
| 5 | + BoundingBox, |
| 6 | + get_correct_bounds_from_shape_at_antimeridian, |
| 7 | + check_s1_bounds_cross_antimeridian, |
| 8 | + check_dem_type_in_bounds, |
| 9 | +) |
| 10 | + |
| 11 | +# Separate lists for clarity |
| 12 | +poly1 = Polygon( |
| 13 | + [ |
| 14 | + (178.576126, -71.618423), |
| 15 | + (-178.032867, -70.167343), |
| 16 | + (176.938004, -68.765106), |
| 17 | + (173.430893, -70.119957), |
| 18 | + (178.576126, -71.618423), |
| 19 | + ], |
| 20 | +) |
| 21 | +poly2 = Polygon( |
| 22 | + [ |
| 23 | + (179.5, -10.0), # Just west of the antimeridian |
| 24 | + (-179.8, 5.0), # Just east of the antimeridian |
| 25 | + (-178.5, 15.0), # Further east |
| 26 | + (178.0, 12.0), # West again |
| 27 | + (179.5, -10.0), # Close the ring |
| 28 | + ] |
| 29 | +) |
| 30 | +poly3 = Polygon( |
| 31 | + [ |
| 32 | + (170.0, -55.0), # Western point |
| 33 | + (-175.0, -50.0), # Just east of antimeridian |
| 34 | + (-160.0, -45.0), # Farther east |
| 35 | + (175.0, -40.0), # Back west |
| 36 | + (170.0, -55.0), # Close the loop |
| 37 | + ] |
| 38 | +) |
| 39 | + |
| 40 | +poly4 = MultiPolygon([poly1, poly2]) |
| 41 | + |
| 42 | +shapes = [poly1, poly2, poly3, poly4] |
| 43 | + |
| 44 | +expected_bounds = [ |
| 45 | + BoundingBox(-178.032867, -71.618423, 173.430893, -68.765106), |
| 46 | + BoundingBox(-178.5, -10, 178.0, 15), |
| 47 | + BoundingBox(-160, -55, 170, -40), |
| 48 | + BoundingBox(-178.032867, -71.618423, 173.430893, 15), |
| 49 | +] |
| 50 | + |
| 51 | +# Combine for parameterization |
| 52 | +test_cases = list(zip(shapes, expected_bounds)) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("shape, expected_bound", test_cases) |
| 56 | +def test_get_correct_bounds_from_shape_at_antimeridian(shape, expected_bound): |
| 57 | + # assert the incorrect bounds still cross the AM |
| 58 | + assert check_s1_bounds_cross_antimeridian(shape.bounds) |
| 59 | + result = get_correct_bounds_from_shape_at_antimeridian(shape) |
| 60 | + assert result == expected_bound |
| 61 | + |
| 62 | + |
| 63 | +@dataclass |
| 64 | +class BoundsDEMCheckCase: |
| 65 | + dem_type: str |
| 66 | + resolution: int |
| 67 | + bounds: tuple[float, float, float, float] |
| 68 | + in_bounds: bool |
| 69 | + is_error: bool |
| 70 | + |
| 71 | + |
| 72 | +test_invalid_dem_type = BoundsDEMCheckCase( |
| 73 | + dem_type="spaghetti", |
| 74 | + resolution=90, |
| 75 | + bounds=(161.96252, -70.75924, 162.10388, -70.72293), |
| 76 | + in_bounds=False, |
| 77 | + is_error=True, |
| 78 | +) |
| 79 | + |
| 80 | +test_invalid_dem_resolution = BoundsDEMCheckCase( |
| 81 | + dem_type="REMA", |
| 82 | + resolution=15, |
| 83 | + bounds=(161.96252, -70.75924, 162.10388, -70.72293), |
| 84 | + in_bounds=False, |
| 85 | + is_error=True, |
| 86 | +) |
| 87 | + |
| 88 | +test_antarctic_bounds_with_rema = BoundsDEMCheckCase( |
| 89 | + dem_type="REMA", |
| 90 | + resolution=32, |
| 91 | + bounds=(161.96252, -70.75924, 162.10388, -70.72293), |
| 92 | + in_bounds=True, |
| 93 | + is_error=False, |
| 94 | +) |
| 95 | + |
| 96 | +test_antarctic_bounds_with_cop30 = BoundsDEMCheckCase( |
| 97 | + dem_type="copernicus", |
| 98 | + resolution=30, |
| 99 | + bounds=(161.96252, -70.75924, 162.10388, -70.72293), |
| 100 | + in_bounds=True, |
| 101 | + is_error=False, |
| 102 | +) |
| 103 | + |
| 104 | +test_australia_bounds_with_rema = BoundsDEMCheckCase( |
| 105 | + dem_type="REMA", |
| 106 | + resolution=10, |
| 107 | + bounds=(133.0, -25.0, 134.0, -24.0), |
| 108 | + in_bounds=False, |
| 109 | + is_error=False, |
| 110 | +) |
| 111 | + |
| 112 | +test_heard_island_bounds_with_rema = BoundsDEMCheckCase( |
| 113 | + dem_type="rema_2", |
| 114 | + resolution=32, |
| 115 | + bounds=(73.2144, -53.224, 73.87, -52.962), |
| 116 | + in_bounds=False, |
| 117 | + is_error=False, |
| 118 | +) |
| 119 | + |
| 120 | +test_heard_island_bounds_with_cop30 = BoundsDEMCheckCase( |
| 121 | + dem_type="cop_glo30", |
| 122 | + resolution=30, |
| 123 | + bounds=(73.2144, -53.224, 73.87, -52.962), |
| 124 | + in_bounds=True, |
| 125 | + is_error=False, |
| 126 | +) |
| 127 | + |
| 128 | +test_antimeridian_with_cop30 = BoundsDEMCheckCase( |
| 129 | + dem_type="cop_glo30", |
| 130 | + resolution=30, |
| 131 | + bounds=(-178.032867, -80.618423, 173.430893, -68.765106), |
| 132 | + in_bounds=True, |
| 133 | + is_error=False, |
| 134 | +) |
| 135 | + |
| 136 | +test_antimeridian_with_rema = BoundsDEMCheckCase( |
| 137 | + dem_type="rema", |
| 138 | + resolution=2, |
| 139 | + bounds=(-178.032867, -80.618423, 173.430893, -68.765106), |
| 140 | + in_bounds=True, |
| 141 | + is_error=False, |
| 142 | +) |
| 143 | + |
| 144 | +test_cases = [ |
| 145 | + test_invalid_dem_type, |
| 146 | + test_invalid_dem_resolution, |
| 147 | + test_antarctic_bounds_with_rema, |
| 148 | + test_antarctic_bounds_with_cop30, |
| 149 | + test_australia_bounds_with_rema, |
| 150 | + test_heard_island_bounds_with_rema, |
| 151 | + test_heard_island_bounds_with_cop30, |
| 152 | + test_antimeridian_with_cop30, |
| 153 | + test_antimeridian_with_rema, |
| 154 | +] |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize("test_case", test_cases) |
| 158 | +def test_check_dem_type_in_bounds(test_case: BoundsDEMCheckCase): |
| 159 | + |
| 160 | + if test_case.is_error: |
| 161 | + with pytest.raises(ValueError): |
| 162 | + check_dem_type_in_bounds( |
| 163 | + test_case.dem_type, test_case.resolution, test_case.bounds |
| 164 | + ) |
| 165 | + |
| 166 | + else: |
| 167 | + assert ( |
| 168 | + check_dem_type_in_bounds( |
| 169 | + test_case.dem_type, test_case.resolution, test_case.bounds |
| 170 | + ) |
| 171 | + == test_case.in_bounds |
| 172 | + ) |
0 commit comments