Advertisement
Abhisek92

Reproject_Compress.py

Mar 13th, 2023 (edited)
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import argparse
  2. import numpy as np
  3. import rasterio as rio
  4. from pathlib import Path
  5. from rich.progress import track
  6. from rasterio.vrt import WarpedVRT
  7. from rasterio.windows import Window
  8. from rasterio.enums import Resampling
  9.  
  10.  
  11. # Comment Block- A and uncomment Block -B to run it in Notebook
  12. # Fill the paths in Block - B accordingly
  13.  
  14. # ================================ Block - A ================================ #
  15. parser = argparse.ArgumentParser(
  16.     description=("Compress GTiff Image")
  17. )
  18. parser.add_argument(
  19.     '-i', '--input',
  20.     metavar='Source Image',
  21.     action='store',
  22.     type=str,
  23.     required=True,
  24.     dest='src_path',
  25.     help='Specify the source file path.'
  26. )
  27. parser.add_argument(
  28.     '-o', '--output',
  29.     metavar='Output Image',
  30.     action='store',
  31.     type=str,
  32.     required=True,
  33.     dest='dst_path',
  34.     help='Specify the destination file path.'
  35. )
  36. args = parser.parse_args()
  37. src_path = Path(args.src_path)
  38. dst_path = Path(args.dst_path)
  39. # ================================ Block - A ================================ #
  40.  
  41.  
  42. # ================================ Block - B ================================ #
  43. # src_path = Path("")
  44. # dst_path = Path("")
  45. # ================================ Block - B ================================ #
  46.  
  47.  
  48. with rio.open(src_path, "r") as src:
  49.     with WarpedVRT(src, crs='EPSG:4326', resampling=Resampling.nearest) as vrt:
  50.         meta = vrt.meta.copy()
  51.         big_window = Window(row_off=0, col_off=0, height=meta["height"], width=meta["width"])
  52.         h_marks, w_marks = np.meshgrid(np.arange(0, meta["height"], 512), np.arange(0, meta["width"], 512))
  53.         h_marks, w_marks = h_marks.ravel().tolist(), w_marks.ravel().tolist()
  54.         meta["driver"] = "GTiff"
  55.         meta["TILED"] = True
  56.         meta["BLOCKXSIZE"] = 512
  57.         meta["BLOCKXYSIZE"] = 512
  58.         meta["compress"] = "zstd"
  59.         meta["SPARSE_OK"] = True
  60.         meta["NUM_THREADS"] = "ALL_CPUS"
  61.         meta["ZSTD_LEVEL"] = 22
  62.         img_dtype = np.dtype(meta["dtype"])
  63.         meta["BIGTIFF"] = True if (
  64.             meta["height"] * meta["width"] * img_dtype.itemsize
  65.         ) >= (2 ** 32) else False
  66.         if np.issubdtype(img_dtype, np.integer):
  67.             meta["predictor"] = 2
  68.         elif np.issubdtype(img_dtype, np.inexact):
  69.             meta["predictor"] = 3
  70.         else:
  71.             # This should not happen
  72.             meta["predictor"] = 1
  73.         with rio.open(dst_path, "w", **meta) as dst:
  74.             for hs, ws in track(list(zip(h_marks, w_marks)), description="Writing:"):
  75.                 win = big_window.intersection(Window(row_off=hs, col_off=ws, height=512, width=512))
  76.                 dst.write(vrt.read(window=win), window=win)
  77.  
Tags: Raster
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement