Skip to content

Instantly share code, notes, and snippets.

@lpinner
Created February 24, 2024 01:05
Show Gist options
  • Save lpinner/d41d045d6f620882943b4976af8f5fd1 to your computer and use it in GitHub Desktop.
Save lpinner/d41d045d6f620882943b4976af8f5fd1 to your computer and use it in GitHub Desktop.
Rasterio snap window
from rasterio import windows
def snap(window):
""" Handle rasterio's floating point precision (sub pixel) windows """
# Adding the offset differences to the dimensions will handle case where width/heights can 1 pixel too small
# after the offsets are shifted.
# This ensures pixel contains the bounds that were originally passed to windows.from_bounds()
col_off, row_off = math.floor(window.col_off), math.floor(window.row_off)
col_diff, row_diff = window.col_off - col_off, window.row_off - row_off
width, height = math.ceil(window.width + col_diff), math.ceil(window.height + row_diff)
return windows.Window(
col_off=col_off, row_off=row_off, width=width, height=height
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment