|
| 1 | +import pytest |
| 2 | +import shapely |
| 3 | + |
| 4 | +from openeo.extra.job_management.job_splitting import ( |
| 5 | + BoundingBox, |
| 6 | + JobSplittingFailure, |
| 7 | + SizeBasedTileGrid, |
| 8 | + reproject_bounding_box, |
| 9 | + split_area, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_polygon_wgs(): |
| 15 | + return shapely.geometry.box(0.0, 0.0, 1.0, 1.0) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_polygon_utm(): |
| 20 | + return shapely.geometry.box(0.0, 0.0, 100_000.0, 100_000.0) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def mock_dict_no_crs(): |
| 25 | + return { |
| 26 | + "west": 0.0, |
| 27 | + "south": 0.0, |
| 28 | + "east": 1.0, |
| 29 | + "north": 1.0, |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def mock_dict_with_crs_utm(): |
| 35 | + return { |
| 36 | + "west": 0.0, |
| 37 | + "south": 0.0, |
| 38 | + "east": 100_000.0, |
| 39 | + "north": 100_000.0, |
| 40 | + "crs": "EPSG:3857", |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize( |
| 45 | + ["crs", "bbox"], |
| 46 | + [ |
| 47 | + ( |
| 48 | + "EPSG:32631", |
| 49 | + {"west": 640800, "south": 5676000, "east": 642200, "north": 5677000}, |
| 50 | + ), |
| 51 | + ("EPSG:4326", {"west": 5.01, "south": 51.2, "east": 5.1, "north": 51.5}), |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_reproject_bounding_box_same(crs, bbox): |
| 55 | + reprojected = reproject_bounding_box(bbox, from_crs=crs, to_crs=crs) |
| 56 | + assert reprojected == dict(crs=crs, **bbox) |
| 57 | + |
| 58 | + |
| 59 | +def test_reproject_bounding_box(): |
| 60 | + bbox = {"west": 640800, "south": 5676000, "east": 642200.0, "north": 5677000.0} |
| 61 | + reprojected = reproject_bounding_box(bbox, from_crs="EPSG:32631", to_crs="EPSG:4326") |
| 62 | + assert reprojected == { |
| 63 | + "west": pytest.approx(5.016118467277098), |
| 64 | + "south": pytest.approx(51.217660146353246), |
| 65 | + "east": pytest.approx(5.036548264535997), |
| 66 | + "north": pytest.approx(51.22699369149726), |
| 67 | + "crs": "EPSG:4326", |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +class TestBoundingBox: |
| 72 | + def test_basic(self): |
| 73 | + bbox = BoundingBox(1, 2, 3, 4) |
| 74 | + assert bbox.west == 1 |
| 75 | + assert bbox.south == 2 |
| 76 | + assert bbox.east == 3 |
| 77 | + assert bbox.north == 4 |
| 78 | + assert bbox.crs == "EPSG:4326" |
| 79 | + |
| 80 | + def test_from_dict(self): |
| 81 | + bbox = BoundingBox.from_dict({"west": 1, "south": 2, "east": 3, "north": 4, "crs": "epsg:32633"}) |
| 82 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 83 | + assert bbox.crs == "epsg:32633" |
| 84 | + |
| 85 | + def test_from_dict_defaults(self): |
| 86 | + bbox = BoundingBox.from_dict({"west": 1, "south": 2, "east": 3, "north": 4}) |
| 87 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 88 | + assert bbox.crs == "EPSG:4326" |
| 89 | + |
| 90 | + def test_from_dict_underspecified(self): |
| 91 | + with pytest.raises(KeyError): |
| 92 | + _ = BoundingBox.from_dict({"west": 1, "south": 2, "color": "red"}) |
| 93 | + |
| 94 | + def test_from_dict_overspecified(self): |
| 95 | + bbox = BoundingBox.from_dict({"west": 1, "south": 2, "east": 3, "north": 4, "crs": "EPSG:4326", "color": "red"}) |
| 96 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 97 | + assert bbox.crs == "EPSG:4326" |
| 98 | + |
| 99 | + def test_from_polygon(self): |
| 100 | + polygon = shapely.geometry.box(1, 2, 3, 4) |
| 101 | + bbox = BoundingBox.from_polygon(polygon, projection="EPSG:4326") |
| 102 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 103 | + assert bbox.crs == "EPSG:4326" |
| 104 | + |
| 105 | + def test_as_dict(self): |
| 106 | + bbox = BoundingBox(1, 2, 3, 4) |
| 107 | + assert bbox.as_dict() == {"west": 1, "south": 2, "east": 3, "north": 4, "crs": "EPSG:4326"} |
| 108 | + |
| 109 | + def test_as_polygon(self): |
| 110 | + bbox = BoundingBox(1, 2, 3, 4) |
| 111 | + polygon = bbox.as_polygon() |
| 112 | + assert isinstance(polygon, shapely.geometry.Polygon) |
| 113 | + assert set(polygon.exterior.coords) == {(1, 2), (3, 2), (3, 4), (1, 4)} |
| 114 | + |
| 115 | + |
| 116 | +class TestSizeBasedTileGrid: |
| 117 | + |
| 118 | + def test_from_size_projection(self): |
| 119 | + splitter = SizeBasedTileGrid.from_size_projection(0.1, "EPSG:4326") |
| 120 | + assert splitter.epsg == "epsg:4326" |
| 121 | + assert splitter.size == 0.1 |
| 122 | + |
| 123 | + def test_get_tiles_raises_exception(self): |
| 124 | + """test get_tiles when the input geometry is not a dict or shapely.geometry.Polygon""" |
| 125 | + tile_grid = SizeBasedTileGrid.from_size_projection(0.1, "EPSG:4326") |
| 126 | + with pytest.raises(JobSplittingFailure): |
| 127 | + tile_grid.get_tiles("invalid_geometry") |
| 128 | + |
| 129 | + def test_get_tiles_dict_returns_dict(self, mock_dict_no_crs): |
| 130 | + """test get_tiles when the input geometry dict returns a list of dicts""" |
| 131 | + tile_grid = SizeBasedTileGrid.from_size_projection(0.1, "EPSG:4326") |
| 132 | + tiles = tile_grid.get_tiles(mock_dict_no_crs) |
| 133 | + assert isinstance(tiles, list) |
| 134 | + assert all(isinstance(tile, dict) for tile in tiles) |
| 135 | + |
| 136 | + def test_get_tiles_polygon_returns_polygon(self, mock_polygon_wgs): |
| 137 | + """test get_tiles when the input geometry is a polygon and the tile grid is in wgs""" |
| 138 | + tile_grid = SizeBasedTileGrid.from_size_projection(0.1, "EPSG:4326") |
| 139 | + tiles = tile_grid.get_tiles(mock_polygon_wgs) |
| 140 | + assert isinstance(tiles, list) |
| 141 | + assert all(isinstance(tile, shapely.geometry.Polygon) for tile in tiles) |
| 142 | + |
| 143 | + def test_get_tiles_dict_no_crs_utm(self, mock_dict_no_crs): |
| 144 | + """test get_tiles when the input geometry dict has no crs and the tile grid is in utm""" |
| 145 | + tile_grid = SizeBasedTileGrid.from_size_projection(20.0, "EPSG:3857") |
| 146 | + tiles = tile_grid.get_tiles(mock_dict_no_crs) |
| 147 | + assert tiles[0].get("crs") == "EPSG:4326" |
| 148 | + assert len(tiles) == 36 |
| 149 | + |
| 150 | + def test_get_tiles_dict_no_crs_wgs(self, mock_dict_no_crs): |
| 151 | + """test get_tiles when the input geometry dict has no crs and the tile grid is in wgs""" |
| 152 | + tile_grid = SizeBasedTileGrid.from_size_projection(0.1, "EPSG:4326") |
| 153 | + tiles = tile_grid.get_tiles(mock_dict_no_crs) |
| 154 | + assert tiles[0].get("crs") == "EPSG:4326" |
| 155 | + assert len(tiles) == 100 |
| 156 | + |
| 157 | + def test_get_tiles_dict_with_crs_same(self, mock_dict_with_crs_utm): |
| 158 | + """test get_tiles when the input geometry dict and the tile grid have the same crs""" |
| 159 | + tile_grid = SizeBasedTileGrid.from_size_projection(20.0, "EPSG:3857") |
| 160 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 161 | + assert tiles[0].get("crs") == "EPSG:3857" |
| 162 | + assert len(tiles) == 25 |
| 163 | + |
| 164 | + def test_get_tiles_dict_with_crs_different(self, mock_dict_with_crs_utm): |
| 165 | + """test get_tiles when the input geometry dict and the tile grid have different crs. The original crs from the geometry should be preserved.""" |
| 166 | + tile_grid = SizeBasedTileGrid.from_size_projection(0.1, "EPSG:4326") |
| 167 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 168 | + assert tiles[0].get("crs") == "EPSG:3857" |
| 169 | + assert len(tiles) == 81 |
| 170 | + |
| 171 | + def test_simple_get_tiles_dict(self, mock_dict_with_crs_utm): |
| 172 | + """test get_tiles when the the tile grid size is equal to the size of the input geometry. The original geometry should be returned.""" |
| 173 | + tile_grid = SizeBasedTileGrid.from_size_projection(100, "EPSG:3857") |
| 174 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 175 | + assert len(tiles) == 1 |
| 176 | + assert tiles[0] == mock_dict_with_crs_utm |
| 177 | + |
| 178 | + def test_multiple_get_tile_dict(self, mock_dict_with_crs_utm): |
| 179 | + """test get_tiles when the the tile grid size is smaller than the size of the input geometry. The input geometry should be split into multiple tiles.""" |
| 180 | + tile_grid = SizeBasedTileGrid.from_size_projection(20, "EPSG:3857") |
| 181 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 182 | + assert len(tiles) == 25 |
| 183 | + assert tiles[0].get("crs") == "EPSG:3857" |
| 184 | + assert tiles[0].get("west") == 0 |
| 185 | + assert tiles[0].get("south") == 0 |
| 186 | + assert tiles[0].get("east") == 20_000 |
| 187 | + assert tiles[0].get("north") == 20_000 |
| 188 | + |
| 189 | + def test_larger_get_tile_dict(self, mock_dict_with_crs_utm): |
| 190 | + """test get_tiles when the the tile grid size is larger than the size of the input geometry. The original geometry should be returned.""" |
| 191 | + tile_grid = SizeBasedTileGrid.from_size_projection(200, "EPSG:3857") |
| 192 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 193 | + assert len(tiles) == 1 |
| 194 | + assert tiles[0] == mock_dict_with_crs_utm |
| 195 | + |
| 196 | + def test_get_tiles_polygon_utm(self, mock_polygon_utm): |
| 197 | + """test get_tiles when the input geometry is a polygon in wgs and the tile grid is in utm""" |
| 198 | + tile_grid = SizeBasedTileGrid.from_size_projection(20.0, "EPSG:3857") |
| 199 | + tiles = tile_grid.get_tiles(mock_polygon_utm) |
| 200 | + assert isinstance(tiles, list) |
| 201 | + assert all(isinstance(tile, shapely.geometry.Polygon) for tile in tiles) |
| 202 | + assert len(tiles) == 25 |
| 203 | + assert tiles[0] == shapely.geometry.box(0.0, 0.0, 20_000.0, 20_000.0) |
| 204 | + |
| 205 | + def test_get_tiles_polygon_wgs(self, mock_polygon_wgs): |
| 206 | + """test get_tiles when the input geometry is a polygon in wgs and the tile grid is in wgs""" |
| 207 | + tile_grid = SizeBasedTileGrid.from_size_projection(0.1, "EPSG:4326") |
| 208 | + tiles = tile_grid.get_tiles(mock_polygon_wgs) |
| 209 | + assert isinstance(tiles, list) |
| 210 | + assert all(isinstance(tile, shapely.geometry.Polygon) for tile in tiles) |
| 211 | + assert len(tiles) == 100 |
| 212 | + assert tiles[0] == shapely.geometry.box(0.0, 0.0, 0.1, 0.1) |
| 213 | + |
| 214 | + def test_simple_get_tiles_polygon(self, mock_polygon_utm): |
| 215 | + """test get_tiles when the the tile grid size is equal to the size of the input geometry. The original geometry should be returned.""" |
| 216 | + tile_grid = SizeBasedTileGrid.from_size_projection(100.0, "EPSG:3857") |
| 217 | + tiles = tile_grid.get_tiles(mock_polygon_utm) |
| 218 | + assert len(tiles) == 1 |
| 219 | + assert tiles[0] == mock_polygon_utm |
| 220 | + |
| 221 | + def test_larger_get_tiles_polygon(self, mock_polygon_utm): |
| 222 | + """test get_tiles when the the tile grid size is larger than the size of the input geometry. The original geometry should be returned.""" |
| 223 | + tile_grid = SizeBasedTileGrid.from_size_projection(200.0, "EPSG:3857") |
| 224 | + tiles = tile_grid.get_tiles(mock_polygon_utm) |
| 225 | + assert len(tiles) == 1 |
| 226 | + assert tiles[0] == mock_polygon_utm |
| 227 | + |
| 228 | + |
| 229 | +def test_split_area_default(): |
| 230 | + """test split_area with default parameters""" |
| 231 | + aoi = {"west": 0.0, "south": 0.0, "east": 20_000.0, "north": 20_000.0, "crs": "EPSG:3857"} |
| 232 | + tiles = split_area(aoi) |
| 233 | + assert len(tiles) == 1 |
| 234 | + assert tiles[0] == aoi |
| 235 | + |
| 236 | + |
| 237 | +def test_split_area_custom(): |
| 238 | + """test split_area with wgs projection""" |
| 239 | + aoi = {"west": 0.0, "south": 0.0, "east": 1.0, "north": 1.0, "crs": "EPSG:4326"} |
| 240 | + tiles = split_area(aoi, "EPSG:4326", 1.0) |
| 241 | + assert len(tiles) == 1 |
| 242 | + assert tiles[0] == aoi |
0 commit comments