Skip to content

Instantly share code, notes, and snippets.

@sgillies
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgillies/9282152 to your computer and use it in GitHub Desktop.
Save sgillies/9282152 to your computer and use it in GitHub Desktop.
import rasterio
with rasterio.open('input.tif') as src:
source = src.read_band(1)
def window_transform(transform, offset):
result = transform[:]
result[3] = transform[3] + offset[0]*transform[5]
result[0] = transform[0] + offset[1]*transform[1]
return result
# This is the idiom for copying parameters from a file to use
# when writing a new file.
kwargs = src.meta
kwargs.update(
width=512,
height=512,
# calculates a new affine transformation matrix, offset 256 rows and 0 columns
# from the one in src.
transform=window_transform(src.transform, (256, 0))
with rasterio.open('output.tif', 'w', **kwargs) as dst:
dst.write_band(1, source[:512,:512])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment