Abhisek92

Clip_Raster.py

Sep 23rd, 2021
853
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import rasterio as rio
  2. from pathlib import Path
  3. import rasterio.mask as rio_mask
  4. from shapely.geometry import box
  5. from rasterio.vrt import WarpedVRT
  6.  
  7. img_path = Path("")
  8. dem_path = Path("")
  9. dst_path = Path("")
  10.  
  11. with rio.open(src_path, 'r') as img, rio.open(dem_path, 'r') as dem:
  12.     with WarpedVRT(dem, crs=img.crs) as dem_vrt:
  13.         meta = dem_vrt.meta.copy()
  14.         bbox = box(*img.bounds)
  15.         dst_img, dst_transform = rio_mask.mask(
  16.           dataset=dem_vrt,
  17.           shapes=(bbox,),
  18.           invert=False,
  19.           all_touched=False,
  20.           crop=True,
  21.           filled=True
  22.         )
  23.         meta['driver'] = 'GTiff'
  24.         meta['count'], meta['height'], meta['width'] = dst_img.shape
  25.         meta['transform'] = dst_transform
  26.         with rio.open(dst_path, 'w', **meta) as dst:
  27.             dst.write(dst_img)
  28.  
RAW Paste Data