Mosaic & merge¶
Combine several rasters into one:
merge_rasters(src, dst)— mosaic many (overlapping or adjacent) rasters into a single raster covering their union.stack_bands(files, path=...)— stack several single-band rasters into one multi-band raster (e.g. assembling per-band Sentinel files into one image).
Setup¶
In [1]:
Copied!
%matplotlib inline
import tempfile
from pathlib import Path
import numpy as np
DATA = Path('../../../examples/data')
WORK = Path(tempfile.mkdtemp(prefix='pyramids-ops-'))
DATA.is_dir(), WORK.is_dir()
%matplotlib inline
import tempfile
from pathlib import Path
import numpy as np
DATA = Path('../../../examples/data')
WORK = Path(tempfile.mkdtemp(prefix='pyramids-ops-'))
DATA.is_dir(), WORK.is_dir()
Out[1]:
(True, True)
In [2]:
Copied!
from pyramids.dataset import Dataset
from pyramids.dataset.merge import merge_rasters, stack_bands
tiles = [DATA / 'crop_aligned_folder' / f'{i}.tif' for i in range(3)]
[Dataset.read_file(t).shape for t in tiles]
from pyramids.dataset import Dataset
from pyramids.dataset.merge import merge_rasters, stack_bands
tiles = [DATA / 'crop_aligned_folder' / f'{i}.tif' for i in range(3)]
[Dataset.read_file(t).shape for t in tiles]
2026-07-11 14:41:46 | INFO | pyramids.base.config | Logging is configured.
Out[2]:
[(1, 24, 29), (1, 24, 29), (1, 24, 29)]
In [3]:
Copied!
# Each input tile is a single-band 24x29 raster on a shared grid.
# `Dataset.plot(band=0)` renders a band inline (it returns a cleopatra ArrayGlyph).
Dataset.read_file(tiles[0]).plot(band=0)
# Each input tile is a single-band 24x29 raster on a shared grid.
# `Dataset.plot(band=0)` renders a band inline (it returns a cleopatra ArrayGlyph).
Dataset.read_file(tiles[0]).plot(band=0)
Out[3]:
<cleopatra.array_glyph.ArrayGlyph at 0x7ffa55e33cb0>
Mosaic — merge_rasters¶
Writes one raster covering the inputs' combined extent. method controls how overlaps resolve ('last', 'first', …).
In [4]:
Copied!
mosaic = WORK / 'mosaic.tif'
merge_rasters(tiles, mosaic)
Dataset.read_file(mosaic).shape
mosaic = WORK / 'mosaic.tif'
merge_rasters(tiles, mosaic)
Dataset.read_file(mosaic).shape
Out[4]:
(1, 24, 29)
In [5]:
Copied!
# The mosaic covers the inputs' combined extent — plot it to see the merged raster.
Dataset.read_file(mosaic).plot(band=0)
# The mosaic covers the inputs' combined extent — plot it to see the merged raster.
Dataset.read_file(mosaic).plot(band=0)
Out[5]:
<cleopatra.array_glyph.ArrayGlyph at 0x7ffb08b48cd0>
Stack bands — stack_bands¶
Combine single-band rasters into one multi-band raster (order = input order).
In [6]:
Copied!
stacked = WORK / 'stacked.tif'
stack_bands(tiles, path=stacked, band_names=['t0', 't1', 't2'])
out = Dataset.read_file(stacked)
out.band_count, out.band_names
stacked = WORK / 'stacked.tif'
stack_bands(tiles, path=stacked, band_names=['t0', 't1', 't2'])
out = Dataset.read_file(stacked)
out.band_count, out.band_names
Out[6]:
(3, ['t0', 't1', 't2'])
In [7]:
Copied!
# The stacked raster carries the three tiles as bands t0/t1/t2 — plot each band.
for b in range(out.band_count):
out.plot(band=b)
# The stacked raster carries the three tiles as bands t0/t1/t2 — plot each band.
for b in range(out.band_count):
out.plot(band=b)
Notes¶
merge_rasterstakesno_data_value=andmethod=to control fill and overlap handling.stack_bandscanalign=Trueto snap mismatched grids before stacking.- See also: Reproject / resample / align.