Advertisement
kylelinden

Masked array

Feb 6th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. with rasterio.open(self.source.path) as src:
  2.     tile_bounds_src_crs = rasterio.warp.transform_bounds(TILE_CRS, src.crs, *tile_bounds)
  3.     tile_window = src.window(*tile_bounds_src_crs)
  4.     tile_data = src.read(window=tile_window, boundless=True, masked=True)
  5.     tile_transform = src.window_transform(tile_window)
  6.  
  7.     # translate the values into the desired range
  8.     if self.scaled_min_max_pixel_values:
  9.         for i, scaled_vals in enumerate(self.scaled_min_max_pixel_values):
  10.             try:
  11.                 tile_data[i] = numpy.interp(tile_data[i],
  12.                                             [tile_data[i].min(), tile_data[i].max()],
  13.                                             scaled_vals)
  14.             except IndexError:
  15.                 logger.warn('Too many scaled_min_max_values passed')
  16.  
  17.     kwds = {
  18.         'transform': rasterio.transform.from_bounds(*tile_bounds, TILE_SIZE, TILE_SIZE),
  19.         'driver': 'PNG',
  20.         'dtype': 'uint8',
  21.         'height': TILE_SIZE,
  22.         'width': TILE_SIZE,
  23.         'crs': TILE_CRS,
  24.         'count': src.count + 1
  25.     }
  26.  
  27.     with MemoryFile() as memfile:
  28.         with memfile.open(**kwds) as dst:
  29.             for i in range(1, src.count + 1):
  30.                 reproject_args = {
  31.                     'source': tile_data[i-1],
  32.                     'src_crs': src.crs,
  33.                     'src_transform': tile_transform,
  34.                     'destination': rasterio.band(dst, i),
  35.                     'resampling': rasterio.enums.Resampling.average,
  36.                     'dst_alpha': src.count + i
  37.                 }
  38.  
  39.                 logger.info(f'reprojecting with kwds {reproject_args}')
  40.                 rasterio.warp.reproject(**reproject_args)
  41.  
  42.         # normal png tiles, read the chunk of memfile as a bytearray
  43.         return memfile.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement