Advertisement
Abhisek92

modified_smapling.py

May 2nd, 2022
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. def grid_smapling(src_path, n=None, band=1):
  2.     with warnings.catch_warnings():
  3.         warnings.filterwarnings(
  4.             "ignore", category=rio.errors.NotGeoreferencedWarning
  5.         )
  6.         with rio.open(src_path, 'r') as src:
  7.             arr = src.read(band)
  8.             if isinstance(n, int) and n > 0:
  9.                 h = src.height
  10.                 w = src.width
  11.                 nh = np.around((((n * h) / w) ** 0.5), decimals=0).astype(int)
  12.                 nw = np.around((((n * w) / h) ** 0.5), decimals=0).astype(int)
  13.                 h_gap = h / (nh + 2)
  14.                 w_gap = w / (nw + 2)
  15.                 h_marks = np.around(
  16.                     (h_gap * np.arange(start=1, stop=(nh+2), step=1)), decimals=0
  17.                 ).astype(int)
  18.                 w_marks = np.around(
  19.                     (w_gap * np.arange(start=1, stop=(nw+2), step=1)), decimals=0
  20.                 ).astype(int)
  21.                 h_marks, w_marks = np.meshgrid(h_marks, w_marks)
  22.                 h_marks = h_marks.ravel()
  23.                 w_marks = w_marks.ravel()
  24.                 z = arr[h_marks, w_marks]
  25.             else:
  26.                 h_len, w_len = arr.shape
  27.                 z = arr.ravel()
  28.                 h_marks, w_marks = np.meshgrid(
  29.                     np.arange(h_len), np.arange(w_len)
  30.                 )
  31.                 h_marks = h_marks.ravel()
  32.                 w_marks = w_marks.ravel()
  33.     x, y = rio.transform.xy(src.transform, h_marks, w_marks)
  34.     return x, y, z
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement