Skip to content

Instantly share code, notes, and snippets.

@prakharcode
Last active September 1, 2021 04:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakharcode/b83caaaa2fc6d2d62b7fe558656df0d1 to your computer and use it in GitHub Desktop.
Save prakharcode/b83caaaa2fc6d2d62b7fe558656df0d1 to your computer and use it in GitHub Desktop.
Resample, reprojection and stacking of bands using rasterio
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