|
| 1 | +import pytest |
| 2 | +import shapely |
| 3 | + |
| 4 | +from openeo.extra.job_management._job_splitting import ( |
| 5 | + JobSplittingFailure, |
| 6 | + _BoundingBox, |
| 7 | + _SizeBasedTileGrid, |
| 8 | + split_area, |
| 9 | +) |
| 10 | + |
| 11 | +# TODO: using fixtures for these simple objects is a bit overkill, makes the test harder to follow, and undermines opportunity to parameterize |
| 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 | +class TestBoundingBox: |
| 45 | + def test_basic(self): |
| 46 | + bbox = _BoundingBox(1, 2, 3, 4) |
| 47 | + assert bbox.west == 1 |
| 48 | + assert bbox.south == 2 |
| 49 | + assert bbox.east == 3 |
| 50 | + assert bbox.north == 4 |
| 51 | + assert bbox.crs == 4326 |
| 52 | + |
| 53 | + def test_from_dict(self): |
| 54 | + bbox = _BoundingBox.from_dict({"west": 1, "south": 2, "east": 3, "north": 4, "crs": "epsg:32633"}) |
| 55 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 56 | + assert bbox.crs == 32633 |
| 57 | + |
| 58 | + def test_from_dict_defaults(self): |
| 59 | + bbox = _BoundingBox.from_dict({"west": 1, "south": 2, "east": 3, "north": 4}) |
| 60 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 61 | + assert bbox.crs == 4326 |
| 62 | + |
| 63 | + def test_from_dict_underspecified(self): |
| 64 | + with pytest.raises(KeyError): |
| 65 | + _ = _BoundingBox.from_dict({"west": 1, "south": 2, "color": "red"}) |
| 66 | + |
| 67 | + def test_from_dict_overspecified(self): |
| 68 | + bbox = _BoundingBox.from_dict( |
| 69 | + {"west": 1, "south": 2, "east": 3, "north": 4, "crs": "EPSG:4326", "color": "red"} |
| 70 | + ) |
| 71 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 72 | + assert bbox.crs == 4326 |
| 73 | + |
| 74 | + def test_from_polygon(self): |
| 75 | + polygon = shapely.geometry.box(1, 2, 3, 4) |
| 76 | + bbox = _BoundingBox.from_polygon(polygon) |
| 77 | + assert (bbox.west, bbox.south, bbox.east, bbox.north) == (1, 2, 3, 4) |
| 78 | + assert bbox.crs == 4326 |
| 79 | + |
| 80 | + def test_as_dict(self): |
| 81 | + bbox = _BoundingBox(1, 2, 3, 4) |
| 82 | + assert bbox.as_dict() == {"west": 1, "south": 2, "east": 3, "north": 4, "crs": 4326} |
| 83 | + |
| 84 | + def test_as_polygon(self): |
| 85 | + bbox = _BoundingBox(1, 2, 3, 4) |
| 86 | + polygon = bbox.as_polygon() |
| 87 | + assert isinstance(polygon, shapely.geometry.Polygon) |
| 88 | + assert set(polygon.exterior.coords) == {(1, 2), (3, 2), (3, 4), (1, 4)} |
| 89 | + |
| 90 | + |
| 91 | +class TestSizeBasedTileGrid: |
| 92 | + def test_from_size_projection(self): |
| 93 | + splitter = _SizeBasedTileGrid.from_size_projection(size=0.1, projection="EPSG:4326") |
| 94 | + assert splitter.epsg == 4326 |
| 95 | + assert splitter.size == 0.1 |
| 96 | + |
| 97 | + def test_get_tiles_raises_exception(self): |
| 98 | + """test get_tiles when the input geometry is not a dict or shapely.geometry.Polygon""" |
| 99 | + tile_grid = _SizeBasedTileGrid.from_size_projection(size=0.1, projection="EPSG:4326") |
| 100 | + with pytest.raises(JobSplittingFailure): |
| 101 | + tile_grid.get_tiles("invalid_geometry") |
| 102 | + |
| 103 | + def test_simple_get_tiles_dict(self, mock_dict_with_crs_utm, mock_polygon_utm): |
| 104 | + """test get_tiles when the the tile grid size is equal to the size of the input geometry. The original geometry should be returned as polygon.""" |
| 105 | + tile_grid = _SizeBasedTileGrid.from_size_projection(size=100_000, projection="EPSG:3857") |
| 106 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 107 | + assert len(tiles) == 1 |
| 108 | + assert tiles[0] == mock_polygon_utm |
| 109 | + |
| 110 | + def test_multiple_get_tile_dict(self, mock_dict_with_crs_utm): |
| 111 | + """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.""" |
| 112 | + tile_grid = _SizeBasedTileGrid.from_size_projection(size=20_000, projection="EPSG:3857") |
| 113 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 114 | + assert len(tiles) == 25 |
| 115 | + assert tiles[0] == shapely.geometry.box(0.0, 0.0, 20_000.0, 20_000.0) |
| 116 | + |
| 117 | + def test_larger_get_tile_dict(self, mock_dict_with_crs_utm, mock_polygon_utm): |
| 118 | + """test get_tiles when the the tile grid size is larger than the size of the input geometry. The original geometry should be returned.""" |
| 119 | + tile_grid = _SizeBasedTileGrid.from_size_projection(size=200_000, projection="EPSG:3857") |
| 120 | + tiles = tile_grid.get_tiles(mock_dict_with_crs_utm) |
| 121 | + assert len(tiles) == 1 |
| 122 | + assert tiles[0] == mock_polygon_utm |
| 123 | + |
| 124 | + def test_get_tiles_polygon_wgs(self, mock_polygon_wgs): |
| 125 | + """test get_tiles when the input geometry is a polygon in wgs and the tile grid is in wgs""" |
| 126 | + tile_grid = _SizeBasedTileGrid.from_size_projection(size=0.1, projection="EPSG:4326") |
| 127 | + tiles = tile_grid.get_tiles(mock_polygon_wgs) |
| 128 | + assert len(tiles) == 100 |
| 129 | + assert tiles[0] == shapely.geometry.box(0.0, 0.0, 0.1, 0.1) |
| 130 | + |
| 131 | + def test_simple_get_tiles_polygon(self, mock_polygon_utm): |
| 132 | + """test get_tiles when the the tile grid size is equal to the size of the input geometry. The original geometry should be returned.""" |
| 133 | + tile_grid = _SizeBasedTileGrid.from_size_projection(size=100_000.0, projection="EPSG:3857") |
| 134 | + tiles = tile_grid.get_tiles(mock_polygon_utm) |
| 135 | + assert len(tiles) == 1 |
| 136 | + assert tiles[0] == mock_polygon_utm |
| 137 | + |
| 138 | + def test_larger_get_tiles_polygon(self, mock_polygon_utm): |
| 139 | + """test get_tiles when the the tile grid size is larger than the size of the input geometry. The original geometry should be returned.""" |
| 140 | + tile_grid = _SizeBasedTileGrid.from_size_projection(size=200_000.0, projection="EPSG:3857") |
| 141 | + tiles = tile_grid.get_tiles(mock_polygon_utm) |
| 142 | + assert len(tiles) == 1 |
| 143 | + assert tiles[0] == mock_polygon_utm |
| 144 | + |
| 145 | + |
| 146 | +def test_split_area_default(): |
| 147 | + """test split_area with default parameters""" |
| 148 | + aoi = {"west": 0.0, "south": 0.0, "east": 20_000.0, "north": 20_000.0, "crs": "EPSG:3857"} |
| 149 | + tiles = split_area(aoi) |
| 150 | + assert len(tiles) == 1 |
| 151 | + assert tiles[0] == shapely.geometry.box(0.0, 0.0, 20_000.0, 20_000.0) |
| 152 | + |
| 153 | + |
| 154 | +def test_split_area_custom(): |
| 155 | + """test split_area with wgs projection""" |
| 156 | + aoi = {"west": 0.0, "south": 0.0, "east": 1.0, "north": 1.0, "crs": "EPSG:4326"} |
| 157 | + tiles = split_area(aoi, "EPSG:4326", 1.0) |
| 158 | + assert len(tiles) == 1 |
| 159 | + assert tiles[0] == shapely.geometry.box(0.0, 0.0, 1.0, 1.0) |
| 160 | + |
| 161 | + |
| 162 | +def test_split_area_custom_no_crs_specified(): |
| 163 | + """test split_area with crs in dict, but not in split_area. The crs in the dict should be used.""" |
| 164 | + aoi = {"west": 0.0, "south": 0.0, "east": 1.0, "north": 1.0, "crs": "EPSG:4326"} |
| 165 | + tiles = split_area(aoi=aoi, tile_size=1.0) |
| 166 | + assert len(tiles) == 1 |
| 167 | + assert tiles[0] == shapely.geometry.box(0.0, 0.0, 1.0, 1.0) |
0 commit comments