Advertisement
Abhisek92

Fix Nodata

May 29th, 2020
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import numpy as np
  2. import rasterio as rio
  3. from rasterio.features import sieve
  4.  
  5. def generate_mask(imgfile, out_img=None, current_nd=0, target_nd=-99999, size=500):
  6.     with rio.open(imgfile, 'r') as src:
  7.         meta = src.meta.copy()
  8.         img_array = src.read(masked=False)
  9.         mask = img_array == current_nd
  10.         mask =  mask.any(axis=0)
  11.         #mask = np.stack((mask for i in range(img_array.shape[0])), axis=0)
  12.         mask =  np.iinfo(np.uint8).max * ((np.logical_not(mask)).astype(np.uint8))
  13.         mask = sieve(mask, size=size)
  14.         mask = np.logical_not(mask.astype(bool))
  15.         mask = np.stack([mask for i in range(img_array.shape[0])], axis=0)
  16.         img_array[mask] = target_nd
  17.         meta['nodata'] = target_nd
  18.     if out_img is None:
  19.         out_img = imgfile
  20.     with rio.open(out_img, 'w', **meta) as dst:
  21.         dst.write(img_array)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement