Last active
September 1, 2021 04:42
-
-
Save prakharcode/b83caaaa2fc6d2d62b7fe558656df0d1 to your computer and use it in GitHub Desktop.
Resample, reprojection and stacking of bands using rasterio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
band_dict = { | |
""" dictonary containing all the bands that are to be stacked | |
in the following format: | |
band_index : "path/to/band" | |
""" | |
} | |
dst_projection = 'EPSG:4326' # the final projection | |
ref_band = "path/to/ref_band/" # the band that you want as a reference | |
with rasterio.open(ref_band) as ref: | |
meta = ref.profile | |
new_transform, width, height = calculate_default_transform(meta.crs, dst_projection, | |
meta.width, meta.height, *meta.bounds) | |
# Update meta to reflect the number of layers | |
meta.update(count = len(band_dict), | |
driver = 'GTiff', | |
crs = {'init': 'epsg:4326'}, | |
transform = new_transform, | |
nodata = 0) | |
for id, layer in enumerate(band_dict.values()): | |
with rasterio.open(layer) as src: | |
with rasterio.open('stack.tif', 'w', **meta) as dst: | |
reproject(source=rasterio.band(src, 1), | |
destination=rasterio.band(dst, id + 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment