Skip to content

Commit 4fe7ad2

Browse files
committed
TileGrid.get_tiles: improve rounding when perfectly aligned
1 parent 789a214 commit 4fe7ad2

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/openeo_aggregator/partitionedjobs/splitting.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ def get_tiles(self, bbox: BoundingBox, max_tiles=MAX_TILES) -> List[BoundingBox]
112112
# Bounding box (in tiling CRS) to cover with tiles.
113113
to_cover = BoundingBox.from_dict(reproject_bounding_box(bbox.as_dict(), from_crs=bbox.crs, to_crs=tiling_crs))
114114
# Get ranges of tile indices
115-
xmin, xmax = [int(math.floor((x - x_offset) / tile_size)) for x in [to_cover.west, to_cover.east]]
116-
ymin, ymax = [int(math.floor(y / tile_size)) for y in [to_cover.south, to_cover.north]]
115+
xmin = int(math.floor((to_cover.west - x_offset) / tile_size))
116+
xmax = int(math.ceil((to_cover.east - x_offset) / tile_size)) - 1
117+
ymin = int(math.floor(to_cover.south / tile_size))
118+
ymax = int(math.ceil(to_cover.north / tile_size)) - 1
117119

118120
tile_count = (xmax - xmin + 1) * (ymax - ymin + 1)
119121
if tile_count > max_tiles:

tests/partitionedjobs/test_splitting.py

+62-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,68 @@
1515
from openeo_aggregator.utils import BoundingBox
1616

1717

18-
def test_tile_grid_spec_from_string():
19-
assert TileGrid.from_string("wgs84-1degree") == TileGrid(crs_type="wgs84", size=1, unit="degree")
20-
assert TileGrid.from_string("utm-10km") == TileGrid(crs_type="utm", size=10, unit="km")
18+
class TestTileGrid:
19+
def test_tile_grid_spec_from_string(self):
20+
assert TileGrid.from_string("wgs84-1degree") == TileGrid(crs_type="wgs84", size=1, unit="degree")
21+
assert TileGrid.from_string("utm-10km") == TileGrid(crs_type="utm", size=10, unit="km")
22+
23+
def test_get_tiles_wgs84_simple(self):
24+
tile_grid = TileGrid(crs_type="wgs84", size=1, unit="degree")
25+
bbox = BoundingBox(west=0.2, south=0.3, east=0.6, north=0.8, crs="epsg:4326")
26+
assert tile_grid.get_tiles(bbox=bbox) == [
27+
BoundingBox(west=0, south=0, east=1, north=1, crs="epsg:4326"),
28+
]
29+
30+
def test_get_tiles_wgs84_basic(self):
31+
tile_grid = TileGrid(crs_type="wgs84", size=1, unit="degree")
32+
bbox = BoundingBox(west=1.2, south=2.3, east=2.6, north=4.8, crs="epsg:4326")
33+
assert tile_grid.get_tiles(bbox=bbox) == [
34+
BoundingBox(west=1, south=2, east=2, north=3, crs="epsg:4326"),
35+
BoundingBox(west=1, south=3, east=2, north=4, crs="epsg:4326"),
36+
BoundingBox(west=1, south=4, east=2, north=5, crs="epsg:4326"),
37+
BoundingBox(west=2, south=2, east=3, north=3, crs="epsg:4326"),
38+
BoundingBox(west=2, south=3, east=3, north=4, crs="epsg:4326"),
39+
BoundingBox(west=2, south=4, east=3, north=5, crs="epsg:4326"),
40+
]
41+
42+
def test_get_get_tiles_wgs84_perfect_alignment(self):
43+
tile_grid = TileGrid(crs_type="wgs84", size=1, unit="degree")
44+
bbox = BoundingBox(west=0, south=2, east=1, north=3, crs="epsg:4326")
45+
assert tile_grid.get_tiles(bbox=bbox) == [
46+
BoundingBox(west=0, south=2, east=1, north=3, crs="epsg:4326"),
47+
]
48+
49+
def test_get_tiles_utm_simple(self):
50+
tile_grid = TileGrid.from_string("utm-10km")
51+
bbox = BoundingBox(west=500_100, south=5_672_000, east=503_000, north=5_677_000, crs="epsg:32631")
52+
assert tile_grid.get_tiles(bbox=bbox) == [
53+
BoundingBox(west=500_000, south=5_670_000, east=510_000, north=5_680_000, crs="epsg:32631"),
54+
]
55+
56+
@pytest.mark.parametrize(
57+
["tile_grid", "expected"],
58+
[
59+
(
60+
"utm-10km",
61+
[
62+
BoundingBox(west=640000, south=5660000, east=650000, north=5670000, crs="epsg:32631"),
63+
BoundingBox(west=640000, south=5670000, east=650000, north=5680000, crs="epsg:32631"),
64+
BoundingBox(west=650000, south=5660000, east=660000, north=5670000, crs="epsg:32631"),
65+
BoundingBox(west=650000, south=5670000, east=660000, north=5680000, crs="epsg:32631"),
66+
],
67+
),
68+
(
69+
"utm-100km",
70+
[
71+
BoundingBox(west=600000, south=5600000, east=700000, north=5700000, crs="epsg:32631"),
72+
],
73+
),
74+
],
75+
)
76+
def test_get_tiles_wgs84_to_utm_basic(self, tile_grid, expected):
77+
tile_grid = TileGrid.from_string(tile_grid)
78+
bbox = BoundingBox(west=5.10, south=51.10, east=5.25, north=51.20, crs="epsg:4326")
79+
assert tile_grid.get_tiles(bbox=bbox) == expected
2180

2281

2382
def test_flimsy_splitter(multi_backend_connection, catalog):

0 commit comments

Comments
 (0)