Skip to content

Instantly share code, notes, and snippets.

@grovduck
Last active November 15, 2017 21:32
Show Gist options
  • Save grovduck/5550c1df0c66e2f69f0689e2d525e98b to your computer and use it in GitHub Desktop.
Save grovduck/5550c1df0c66e2f69f0689e2d525e98b to your computer and use it in GitHub Desktop.
[Expand rasterio windows] Get rasterio windows that expand the shape of raster blocks #rasterio
import sys
import click
import rasterio
def expand(window, size=1):
r, c = window
return ((r[0] - size, r[1] + size), (c[0] - size, c[1] + size))
def check_window(window, w, h):
r, c = window
return ((max(0, r[0]), min(h, r[1])), (max(0, c[0]), min(w, c[1])))
@click.command()
@click.argument(
'raster-fn',
type=click.Path(exists=True),
required=True
)
@click.option(
'--expand-size',
type=click.INT,
default=1,
help='Number of pixels to expand window'
)
def main(raster_fn, expand_size):
src = rasterio.open(raster_fn)
for _, w in src.block_windows():
new_w = check_window(expand(w, expand_size), src.width, src.height)
print 'Original: ', w
print 'Expanded: ', new_w
print
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment