Advertisement
Abhisek92

Compress GTiff

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