kylelinden

Modified transform

Feb 6th, 2019
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)
  5.     tile_transform = src.window_transform(tile_window)
  6.  
  7.     # adjust the translation of the transform if this is a left edge tile
  8.     xoff = tile_transform.xoff
  9.     yoff = tile_transform.yoff
  10.     if xoff < src.bounds.left:
  11.         xoff = src.bounds.left
  12.     if tile_transform.yoff > src.bounds.top:
  13.         yoff = src.bounds.top
  14.  
  15.     tile_transform = Affine(tile_transform.a, tile_transform.b, xoff,
  16.                             tile_transform.d, tile_transform.e, yoff)
  17.  
  18.     # translate the values into the desired range
  19.     if self.scaled_min_max_pixel_values:
  20.         for i, scaled_vals in enumerate(self.scaled_min_max_pixel_values):
  21.             try:
  22.                 tile_data[i] = numpy.interp(tile_data[i],
  23.                                             [tile_data[i].min(), tile_data[i].max()],
  24.                                             scaled_vals)
  25.             except IndexError:
  26.                 logger.warn('Too many scaled_min_max_values passed')
  27.  
  28.     kwds = {
  29.         'transform': rasterio.transform.from_bounds(*tile_bounds, TILE_SIZE, TILE_SIZE),
  30.         'driver': 'PNG',
  31.         'dtype': 'uint8',
  32.         'height': TILE_SIZE,
  33.         'width': TILE_SIZE,
  34.         'crs': TILE_CRS,
  35.         'count': src.count + 1
  36.     }
  37.  
  38.     with MemoryFile() as memfile:
  39.         with memfile.open(**kwds) as dst:
  40.             for i in range(1, src.count + 1):
  41.                 reproject_args = {
  42.                     'source': tile_data[i-1],
  43.                     'src_crs': src.crs,
  44.                     'src_transform': tile_transform,
  45.                     'destination': rasterio.band(dst, i),
  46.                     'resampling': rasterio.enums.Resampling.average,
  47.                     'dst_alpha': src.count + i
  48.                 }
  49.  
  50.                 logger.info(f'reprojecting with kwds {reproject_args}')
  51.                 rasterio.warp.reproject(**reproject_args)
  52.  
  53.         # normal png tiles, read the chunk of memfile as a bytearray
  54.         return memfile.read()
RAW Paste Data