Skip to content

Commit 21e3ef0

Browse files
added overlapping-tiling option
1 parent 141dc97 commit 21e3ef0

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

detectree2/preprocessing/tiling.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ def _calculate_tile_placements(
439439
tile_height: int,
440440
crowns: gpd.GeoDataFrame = None,
441441
tile_placement: str = "grid",
442+
overlapping_tiles: bool = False,
442443
) -> List[Tuple[int, int]]:
443444
"""Internal method for calculating the placement of tiles"""
444445

@@ -450,6 +451,12 @@ def _calculate_tile_placements(
450451
for miny in np.arange(
451452
math.ceil(data.bounds[1]) + buffer, data.bounds[3] - tile_height - buffer, tile_height, int)
452453
]
454+
if overlapping_tiles:
455+
coordinates.extend([(minx, miny) for minx in np.arange(
456+
math.ceil(data.bounds[0]) + buffer + tile_width // 2, data.bounds[2] - tile_width - buffer -
457+
tile_width // 2, tile_width, int) for miny in np.arange(
458+
math.ceil(data.bounds[1]) + buffer + tile_height // 2, data.bounds[3] - tile_height - buffer -
459+
tile_height // 2, tile_height, int)])
453460
elif tile_placement == "adaptive":
454461

455462
if crowns is None:
@@ -495,6 +502,10 @@ def _calculate_tile_placements(
495502
for col in range(required_intersection_tiles_x):
496503
coordinates.append((int(intersection.total_bounds[0] - x_intersection_offset) + col * tile_width,
497504
int(crowns.total_bounds[1] - y_offset) + row * tile_height))
505+
if overlapping_tiles:
506+
coordinates.append(
507+
(int(intersection.total_bounds[0] - x_intersection_offset) + col * tile_width + tile_width // 2,
508+
int(crowns.total_bounds[1] - y_offset) + row * tile_height + tile_height // 2))
498509
logger.info(f"Finished Tile Placement Generation")
499510
else:
500511
raise ValueError('Unsupported tile_placement method. Must be "grid" or "adaptive"')
@@ -617,6 +628,7 @@ def tile_data(
617628
multithreaded: bool = False,
618629
random_subset: int = -1,
619630
additional_nodata: List[Any] = [],
631+
overlapping_tiles: bool = False,
620632
) -> None:
621633
"""Tiles up orthomosaic and corresponding crowns (if supplied) into training/prediction tiles.
622634
@@ -653,7 +665,8 @@ def tile_data(
653665
with rasterio.open(img_path) as data:
654666
crs = data.crs.to_epsg() # Update CRS handling to avoid deprecated syntax
655667

656-
tile_coordinates = _calculate_tile_placements(img_path, buffer, tile_width, tile_height, crowns, tile_placement)
668+
tile_coordinates = _calculate_tile_placements(img_path, buffer, tile_width, tile_height, crowns, tile_placement,
669+
overlapping_tiles)
657670
image_statistics = calculate_image_statistics(img_path, values_to_ignore=additional_nodata, mode=mode)
658671

659672
tile_args = [

0 commit comments

Comments
 (0)