Skip to content

Commit 687779f

Browse files
SizeBasedTileGrid: omit non-intersecting tiles from final result #883
1 parent 07af1d2 commit 687779f

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

openeo/extra/job_management/_job_splitting.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@ def get_tiles(self, geometry: Union[Dict, Polygon, MultiPolygon]) -> gpd.GeoData
179179

180180
bbox = BBoxDict.from_any(geom, crs=self._epsg)
181181
polygons = self._split_bounding_box(to_cover=bbox, tile_size=self.size)
182-
return gpd.GeoDataFrame(geometry=polygons, crs=f"EPSG:{self._epsg}")
182+
gdf = gpd.GeoDataFrame(geometry=polygons, crs=f"EPSG:{self._epsg}")
183+
184+
# Drop tiles that don't actually intersect the original geometry.
185+
# This matters for concave or complex shapes whose bounding box is
186+
# significantly larger than the shape itself.
187+
mask = gdf.intersects(geom)
188+
return gdf.loc[mask].reset_index(drop=True)
183189

184190

185191
class _PredefinedTileGrid(_TileGridInterface):

tests/extra/job_management/test_job_splitting.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ def test_edge_tiles_clipped_to_aoi(self):
109109
assert 150_000.0 in east_edges
110110
assert 150_000.0 in north_edges
111111

112+
def test_concave_polygon_skips_non_intersecting_tiles(self):
113+
"""Tiles from the bounding box that don't intersect the actual geometry are dropped."""
114+
# L-shaped polygon: bottom strip spans full width up to y=0.9, left column extends to y=2.
115+
# The horizontal edge at y=0.9 avoids the y=1 tile boundary.
116+
l_shape = shapely.geometry.Polygon([(0, 0), (2, 0), (2, 0.9), (0.5, 0.9), (0.5, 2), (0, 2), (0, 0)])
117+
grid = _SizeBasedTileGrid(epsg=4326, size=1.0)
118+
result = grid.get_tiles(l_shape)
119+
# Bounding box is 2x2 -> 4 candidate tiles, but upper-right (1,1)->(2,2) doesn't intersect
120+
assert len(result) == 3
121+
112122
def test_reprojects_bbox_when_crs_differs(self):
113123
"""AOI in EPSG:4326 should be reprojected to the tile grid's CRS (EPSG:3857) before splitting."""
114124
# A small bbox around lon=0, lat=0 in WGS84

0 commit comments

Comments
 (0)