SHARE
TWEET

Fix Nodata

Abhisek92 May 29th, 2020 (edited) 965 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top