Last active
August 29, 2015 14:14
-
-
Save sgillies/14a325d7d78878c45bfc to your computer and use it in GitHub Desktop.
reprojection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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