Advertisement
Abhisek92

plot_gridpoints.py

Apr 22nd, 2022 (edited)
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import warnings
  2. import numpy as np
  3. import rasterio as rio
  4. from pathlib import Path
  5. from matplotlib import pyplot as plt
  6.  
  7. def grid_smapling(src_path, n, band=1):
  8.     with warnings.catch_warnings():
  9.         warnings.filterwarnings(
  10.             "ignore", category=rio.errors.NotGeoreferencedWarning
  11.         )
  12.         with rio.open(src_path, 'r') as src:
  13.             h = src.height
  14.             w = src.width
  15.             nh = np.around((((n * h) / w) ** 0.5), decimals=0).astype(int)
  16.             nw = np.around((((n * w) / h) ** 0.5), decimals=0).astype(int)
  17.             h_gap = h / nh
  18.             w_gap = w / nw
  19.             h_marks = h_gap * np.arange(start=0, stop=nh, step=1)
  20.             w_marks = w_gap * np.arange(start=0, stop=nw, step=1)
  21.             h_marks = np.around((h_marks + (h_gap / 2)), decimals=0).astype(int)
  22.             w_marks = np.around((w_marks + (w_gap / 2)), decimals=0).astype(int)
  23.             h_marks, w_marks = np.meshgrid(h_marks, w_marks)
  24.             h_marks = h_marks.ravel()
  25.             w_marks = w_marks.ravel()
  26.             arr = src.read(band)
  27.             z = arr[h_marks, w_marks]
  28.             fig = plt.figure()
  29.             ax = fig.add_subplot(111)
  30.             ax.imshow(arr)
  31.             ax.scatter(w_marks, h_marks, c='r')
  32.             plt.show()
  33.     x, y = rio.transform.xy(src.transform, h_marks, w_marks)
  34.     return x, y, z
  35.  
  36. if __name__ == '__main__':
  37.     _ = grid_smapling('test_vario.tif', 1000, 1)
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement