1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import numpy as np import rasterio from rasterio.warp import calculate_default_transform, reproject, Resampling from rasterio import crs
src_img = 'example.tif' dst_img = 'reproject.tif'
dst_crs = crs.CRS.from_epsg('4326')
with rasterio.open(src_img) as src_ds: profile = src_ds.profile
dst_transform, dst_width, dst_height = calculate_default_transform( src_ds.crs, dst_crs, src_ds.width, src_ds.height, *src_ds.bounds)
profile.update({ 'crs': dst_crs, 'transform': dst_transform, 'width': dst_width, 'height': dst_height, 'nodata': 0 })
with rasterio.open(dst_img, 'w', **profile) as dst_ds: for i in range(1, src_ds.count + 1): src_array = src_ds.read(i) dst_array = np.empty((dst_height, dst_width), dtype=profile['dtype'])
reproject( source=src_array, src_crs=src_ds.crs, src_transform=src_ds.transform, destination=dst_array, dst_transform=dst_transform, dst_crs=dst_crs, resampling=Resampling.cubic, num_threads=2)
dst_ds.write(dst_array, i)
|