Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Example licensed under cc by-sa 3.0 with attribution required
- from contextlib import contextmanager
- import rasterio
- from rasterio import Affine, MemoryFile
- from rasterio.enums import Resampling
- # use context manager so DatasetReader and MemoryFile get cleaned up automatically
- @contextmanager
- def resample_raster(raster, scale=2):
- t = raster.transform
- # rescale the metadata
- transform = Affine(t.a / scale, t.b, t.c, t.d, t.e / scale, t.f)
- height = raster.height * scale
- width = raster.width * scale
- profile = src.profile
- profile.update(transform=transform, driver='GTiff', height=height, width=width)
- data = raster.read( # Note changed order of indexes, arrays are band, row, col order not row, col, band
- out_shape=(raster.count, height, width),
- resampling=Resampling.bilinear,
- )
- with MemoryFile() as memfile:
- with memfile.open(**profile) as dataset: # Open as DatasetWriter
- dataset.write(data)
- del data
- with memfile.open() as dataset: # Reopen as DatasetReader
- yield dataset # Note yield not return
- with rasterio.open('path/to/raster') as src:
- with resample_raster(src) as resampled:
- print('Orig dims: {}, New dims: {}'.format(src.shape, resampled.shape))
- print(repr(resampled))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement