I was wondering how to merge multiple TIFF files together and clip the mosaic by the bounds of a intersecting shapefile. I've been able to merge them and output as a .tiff, but am not sure how to to mask the mosaic before outputting.
import os
import rasterio
from rasterio.merge import merge
tiffs_path = ['my_file1','my_file2' ]
shape_path = 'my_shape'
raster_to_mosaic = []
for x in tiffs_path:
raster = rasterio.open(x)
raster_to_mosaic.append(raster)
mosaic, output = merge(raster_to_mosaic)
output_meta = raster.meta.copy()
output_meta.update({
'driver' :'GTiff',
'height': mosaic.shape[1],
'width': mosaic.shape[2],
'crs':'EPSG:4326',
'compress':'lzw',
'transform': output
})
with rasterio.open('merged_raster.tiff', 'w+', **output_meta) as m:
m.write(mosaic)