Untitled
unknown
python
3 years ago
1.5 kB
9
Indexable
import os
import rasterio
from rasterio.plot import show
from utils import planet_utils
img_path = "data/01d9458e-afc8-4524-beaf-466a801f4ca7-20220529_094114_40_247a-raster.tif"
overlay_path = "data/01d9458e-afc8-4524-beaf-466a801f4ca7-20220529_094114_40_247a-map.tif"
evi_path = "data/01d9458e-afc8-4524-beaf-466a801f4ca7-20220529_094114_40_247a-evi.tif"
# load mask
with rasterio.open(overlay_path) as src:
snow_mask = src.read(2).astype(bool)
shadow_mask = src.read(3).astype(bool)
cloud_mask = src.read(6).astype(bool)
mask = snow_mask + shadow_mask + cloud_mask
show(snow_mask, title="snow_mask", cmap="binary")
show(shadow_mask, title="shadow", cmap="binary")
show(cloud_mask, title="cloud", cmap="binary")
show(mask, title="mask", cmap="binary")
# load raster image
with rasterio.open(overlay_path) as src:
out_meta = src.meta
profile = src.profile
img_data = src.read([7,6,5,4,3,2,1,], masked=True) / 10000.0 # apply RGB ordering and scale down
show(img_data, title="original image")
# invalidate masked out pixels
img_data.mask = mask
img_data = img_data.filled(fill_value=float(os.getenv("MASKED_OUT_PIXELS_VALUE",0)))
show(img_data, title="masked image")
# Set spatial characteristics of the output object to mirror the input
kwargs = src.meta
kwargs.update(
dtype=rasterio.float32,
count = 7)
# Write img_data to a new raster file
with rasterio.open('data/test.tif', 'w', **kwargs) as dst:
dst.write(img_data.astype(rasterio.float32))
Editor is loading...