def reproject_raster(input_file, output_file, dst_crs, resolution=None, resampling_method=Resampling.nearest, dst_nodata=None):
with rasterio.open(input_file) as src:
src_nodata = src.nodata if src.nodata is not None else dst_nodata
if resolution:
transform, width, height = calculate_default_transform(
src.crs, dst_crs, src.width, src.height, *src.bounds,
resolution=resolution
)
else:
transform, width, height = calculate_default_transform(
src.crs, dst_crs, src.width, src.height, *src.bounds
)
metadata = src.meta.copy()
metadata.update({
'crs': dst_crs,
'transform': transform,
'width': width,
'height': height,
'nodata': dst_nodata if dst_nodata is not None else src_nodata
})
with rasterio.open(output_file, 'w', **metadata) as dst:
for i in range(1, src.count + 1):
src_data = src.read(i)
dst_data = np.zeros((height, width), dtype=src_data.dtype)
dst_data.fill(src_nodata if dst_nodata is None else dst_nodata)
reproject(
source=src_data,
destination=dst_data,
src_transform=src.transform,
src_crs=src.crs,
dst_transform=transform,
dst_crs=dst_crs,
resampling=resampling_method,
src_nodata=src_nodata,
dst_nodata=dst_nodata
)
dst.write(dst_data, i)
input_raster = r'D:\Geographical_data\land_use\CLCD_v01_2023_albert.tif'
reproj_raster = r'D:\Geographical_data\land_use\CLCD_v01_2023_WGS1984_4_128.tif'
dst_crs = 'EPSG:4326'
reproject_raster(input_raster, reproj_raster, dst_crs, Resampling.nearest)
enter image description here
I want to reproject a raster data with rasterio, but it shows MemoryError, I want to ask how to modify the function such as introducing some windowing techniques etc. to handle and speed up the processing of large raster data