Skip to content

Commit

Permalink
Refactoring into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaspadilha committed Jul 31, 2024
1 parent ba7cb3c commit 6dfcbb4
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/vibe_lib/vibe_lib/spaceeye/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,37 @@ def get_write_windows(
)


def adjust_dim(
window_dim: float, window_ranges: Tuple[float, float], chip_dim: float, raster_bounds: float
) -> Tuple[float, float]:
"""
Adjust a window's dimension (width or height) to make sure the window reaches the chip size
while still within the raster bounds.
Args:
chip_dim: The chip dimension (width or height).
window_dim: The window dimension (width or height).
window_ranges: The window ranges (start, end).
raster_bounds: The raster dimension (width or height).
Returns:
The adjusted window ranges.
"""
diff = chip_dim - window_dim
offset = diff // 2

offset_low = offset if window_ranges[0] - offset >= 0 else window_ranges[0]
offset_high = diff - offset_low
if offset_high + window_ranges[1] > raster_bounds:
offset_high = raster_bounds - window_ranges[1]
offset_low = diff - offset_high

min_dim = max(window_ranges[0] - offset_low, 0)
max_dim = window_ranges[1] + offset_high

return min_dim, max_dim


class SpaceEyeReader(Dataset[DatasetReturnType]):
"""Dataset that lazily reads chips from sentinel 1 and 2 rasters.
The dataset computes the necessary chips to cover the whole RoI according to
Expand Down Expand Up @@ -266,22 +297,11 @@ def _adjust_roi_window(self, window: Window) -> Window:
f"RoI has dimensions {window.width, window.height} and chip size is {self.chip_size},"
f" adjusting to {width, height}"
)
diff_w = width - window.width
dw = diff_w // 2
diff_h = height - window.height
dh = diff_h // 2

hs, ws = window.toranges()

dw_left = dw if ws[0] - dw >= 0 else ws[0]
dw_right = diff_w - dw_left
min_w = ws[0] - dw_left
max_w = min(ws[1] + dw_right, self.raster_width)

dh_top = dh if hs[0] - dh >= 0 else hs[0]
dh_bottom = diff_h - dh_top
min_h = hs[0] - dh_top
max_h = min(hs[1] + dh_bottom, self.raster_height)
min_h, max_h = adjust_dim(window.height, hs, height, self.raster_height)
min_w, max_w = adjust_dim(window.width, ws, width, self.raster_width)

new_win = Window.from_slices((min_h, max_h), (min_w, max_w))
LOGGER.info(f"Adjusting from {window} to {new_win}")
Expand Down

0 comments on commit 6dfcbb4

Please sign in to comment.