Advertisement
kylelinden

Rasterio Tiling (broken)

Feb 4th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. import rasterio
  2. from rasterio.transform import from_bounds as transform_from_bounds
  3. from rasterio.warp import reproject, transform_bounds
  4. from rasterio.windows import transform as window_transform
  5.  
  6. with rasterio.open(source_path) as src:
  7.     tile_bounds_src_crs = transform_bounds(dst_srs, src.crs, *tile_bounds)
  8.     tile_window = src.window(*tile_bounds_src_crs)
  9.     tile_data = src.read(window=tile_window)
  10.  
  11.     # translate the values into the desired range
  12.     if scaled_min_max_pixel_values:
  13.         for i, scaled_vals in enumerate(scaled_min_max_pixel_values):
  14.             try:
  15.                 tile_data[i] = numpy.interp(tile_data[i],
  16.                                             [tile_data[i].min(), tile_data[i].max()],
  17.                                             scaled_vals)
  18.             except IndexError:
  19.                 self.logger.warn('Too many scaled_min_max_values passed')
  20.  
  21.     kwds = {
  22.         'transform': transform_from_bounds(*tile_bounds, tile_size, tile_size),
  23.         'driver': 'PNG',
  24.         'dtype': 'uint8',
  25.         'height': tile_size,
  26.         'width': tile_size,
  27.         'crs': dst_srs,
  28.         'count': src.count + 1
  29.     }
  30.  
  31.     # create the output directory
  32.     os.makedirs(os.path.dirname(output_path), exist_ok=True)
  33.  
  34.     with rasterio.open(output_path, 'w', **kwds) as dst:
  35.         for i in range(1, src.count + 1):
  36.             reproject_args = {
  37.                 'source': tile_data[i-1],
  38.                 'src_crs': src.crs,
  39.                 'src_transform': window_transform(tile_window, src.transform),
  40.                 'destination': rasterio.band(dst, i),
  41.                 'resampling': self._get_resample_alg_value(resample_alg),
  42.                 'dst_alpha': src.count + i
  43.             }
  44.  
  45.             # can only specify nan nodata value on float32 and float64 input bands
  46.             if src.dtypes[i-1].startswith('float'):
  47.                 reproject_args['src_nodata'] = numpy.nan
  48.  
  49.             reproject(**reproject_args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement