Skip to content

Instantly share code, notes, and snippets.

@sgillies
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgillies/14a325d7d78878c45bfc to your computer and use it in GitHub Desktop.
Save sgillies/14a325d7d78878c45bfc to your computer and use it in GitHub Desktop.
reprojection
import numpy
import rasterio
from rasterio import Affine as A
from rasterio.warp import reproject, RESAMPLING
with rasterio.open('rasterio/tests/data/RGB.byte.tif') as src:
src_transform = src.affine
# Zoom out by a factor of 2 from the center of the source
# dataset. The destination transform is the product of the
# source transform, a translation down and to the right, and
# a scaling.
dst_transform = src_transform*A.translation(
-src.width/2.0, -src.height/2.0)*A.scale(2.0)
data = src.read()
kwargs = src.meta
kwargs['transform'] = dst_transform
with rasterio.open('/tmp/zoomed-out.tif', 'w', **kwargs) as dst:
for i, band in enumerate(data, 1):
dest = numpy.zeros_like(band)
reproject(
band,
dest,
src_transform=src_transform,
src_crs=src.crs,
dst_transform=dst_transform,
dst_crs=src.crs,
resampling=RESAMPLING.nearest)
dst.write_band(i, dest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment