kylelinden

Rasterio Tiling (working, but no scaling)

Feb 4th, 2019
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import rasterio
  2. from rasterio.transform import from_bounds
  3.  
  4. with rasterio.open(source_path) as src:
  5.     kwds = {
  6.         'transform': from_bounds(*tile_bounds, tile_size, tile_size),
  7.         'driver': 'PNG',
  8.         'dtype': 'uint8',
  9.         'height': tile_size,
  10.         'width': tile_size,
  11.         'crs': dst_srs,
  12.         'count': src.count + 1
  13.     }
  14.  
  15.     # create the output directory
  16.     os.makedirs(os.path.dirname(output_path), exist_ok=True)
  17.  
  18.     with rasterio.open(output_path, 'w', **kwds) as dst:
  19.         for i in range(1, src.count + 1):
  20.             reproject_args = {
  21.                 'source': rasterio.band(src, i),
  22.                 'destination': rasterio.band(dst, i),
  23.                 'resampling': self._get_resample_alg_value(resample_alg),
  24.                 'dst_alpha': src.count + i
  25.             }
  26.  
  27.             # can only specify nan nodata value on float32 and float64 input bands
  28.             if src.dtypes[i-1].startswith('float'):
  29.                 reproject_args['src_nodata'] = numpy.nan
  30.  
  31.             reproject(**reproject_args)
RAW Paste Data