Skip to content

Instantly share code, notes, and snippets.

@underchemist
Last active March 14, 2020 07:17
Show Gist options
  • Save underchemist/6c5f521d155d3dc1460201feba8251fd to your computer and use it in GitHub Desktop.
Save underchemist/6c5f521d155d3dc1460201feba8251fd to your computer and use it in GitHub Desktop.
rasterio inspired block_windows implementation using itertools
from itertools import product, chain
def block_windows(src, xsize, ysize):
height, width = src.shape
nrows, rmod = divmod(height, ysize)
ncols, cmod = divmod(width, xsize)
offsets = product(range(0, width, xsize), range(0, height, ysize))
sizes = product(chain([xsize]*ncols, [cmod]), chain([ysize]*nrows, [rmod]))
for (xoff, yoff), (xsize, ysize) in zip(offsets, sizes):
yield xoff, yoff, xsize, ysize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment