The following code clips the TIFF images based on the GeoJson file. However, when clipping the tiles that has half image and half nothing (white area in QGIS), the nothing half changes to black, which is what I want but the half that has the real image turns to white. How can I get the half that has the real image to have the same thing (data) while the half that has nothing to turn to the black color?
import os
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from shapely.geometry import mapping
import geopandas as gpd
import rasterio as rio
from rasterio.plot import plotting_extent
from rasterio.mask import mask
import earthpy as et
import earthpy.spatial as es
import earthpy.plot as ep
shape_file = '/.../abc.geojson'
crop_extent = gpd.read_file(shape_file)
lidar_chm_path = '/.../reprojected.tif'
output = '/.../Data/'
if not os.path.exists(output):
os.mkdir(output)
i, j = 0, 0
while i < crop_extent.shape[0]:
with rio.open(lidar_chm_path) as lidar_chm:
try:
lidar_chm_crop, lidar_chm_crop_meta = es.crop_image(lidar_chm,[crop_extent.iloc[i]['geometry']])
lidar_chm_crop_affine = lidar_chm_crop_meta["transform"]
j += 1
lidar_chm_crop_meta.update({'transform': lidar_chm_crop_affine,
'height': lidar_chm_crop.shape[1],
'width': lidar_chm_crop.shape[2],
'nodata': -999.99})
name = output + str(crop_extent.iloc[1][2]) + 'image' + str(j) + '.tif'
with rio.open(name, 'w', **lidar_chm_crop_meta) as ff:
ff.write(lidar_chm_crop[0], 1)
except:
None
i += 1