Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with rasterio.open(self.source.path) as src:
- tile_bounds_src_crs = rasterio.warp.transform_bounds(TILE_CRS, src.crs, *tile_bounds)
- tile_window = src.window(*tile_bounds_src_crs)
- tile_data = src.read(window=tile_window)
- tile_transform = src.window_transform(tile_window)
- # adjust the translation of the transform if this is a left edge tile
- xoff = tile_transform.xoff
- yoff = tile_transform.yoff
- if xoff < src.bounds.left:
- xoff = src.bounds.left
- if tile_transform.yoff > src.bounds.top:
- yoff = src.bounds.top
- tile_transform = Affine(tile_transform.a, tile_transform.b, xoff,
- tile_transform.d, tile_transform.e, yoff)
- # translate the values into the desired range
- if self.scaled_min_max_pixel_values:
- for i, scaled_vals in enumerate(self.scaled_min_max_pixel_values):
- try:
- tile_data[i] = numpy.interp(tile_data[i],
- [tile_data[i].min(), tile_data[i].max()],
- scaled_vals)
- except IndexError:
- logger.warn('Too many scaled_min_max_values passed')
- kwds = {
- 'transform': rasterio.transform.from_bounds(*tile_bounds, TILE_SIZE, TILE_SIZE),
- 'driver': 'PNG',
- 'dtype': 'uint8',
- 'height': TILE_SIZE,
- 'width': TILE_SIZE,
- 'crs': TILE_CRS,
- 'count': src.count + 1
- }
- with MemoryFile() as memfile:
- with memfile.open(**kwds) as dst:
- for i in range(1, src.count + 1):
- reproject_args = {
- 'source': tile_data[i-1],
- 'src_crs': src.crs,
- 'src_transform': tile_transform,
- 'destination': rasterio.band(dst, i),
- 'resampling': rasterio.enums.Resampling.average,
- 'dst_alpha': src.count + i
- }
- logger.info(f'reprojecting with kwds {reproject_args}')
- rasterio.warp.reproject(**reproject_args)
- # normal png tiles, read the chunk of memfile as a bytearray
- return memfile.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement