Merge & stack#
Free functions for combining multiple rasters into one.
flowchart LR
M["<b>pyramids.dataset.merge</b>"]
M --> MR["<b>merge_rasters</b><br/>mosaic overlapping rasters<br/>into one (reproject on the fly)"]
M --> SB["<b>stack_bands</b><br/>stack single-band rasters<br/>into one multi-band Dataset"]
merge_rasters— mosaic several (overlapping or adjacent) rasters into a single raster covering their union.stack_bands— stack several single-band rasters into one multi-band raster.
See the Mosaic & merge notebook for runnable examples.
pyramids.dataset.merge.merge_rasters(src, dst, no_data_value='0', init='nan', n='nan', method='last', dst_crs=None, resampling='nearest neighbor', signer=None)
#
Merge a group of rasters into one raster, resolving overlaps by method.
The overlap-resolution method selects how overlapping pixels are
combined:
"last"(default) /"first"— z-order compositing: the last (or first) source covering a pixel wins. Implemented cheaply with :func:gdal.BuildVRT+ :func:gdal.Translate."min"/"max"/"sum"— per-pixel reduction across every source overlapping that pixel, ignoring no-data. Each source is aligned onto the union grid and the bands are stacked and reduced with NaN-aware numpy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Sequence[str | Path]
|
Paths to all input rasters. |
required |
dst
|
str | Path
|
Path to the output raster. |
required |
no_data_value
|
float | int | str
|
Stamped on the output bands as the nodata marker. For the reduction methods it also fills pixels with no source coverage. |
'0'
|
init
|
float | int | str
|
Reported value for pixels with no source coverage in the VRT (z-order
methods only). Maps to :func: |
'nan'
|
n
|
float | int | str
|
Source pixels matching this value are ignored — both when building the VRT mosaic (z-order) and when reducing (the value is treated as source no-data). |
'nan'
|
method
|
str
|
Overlap-resolution rule: one of |
'last'
|
dst_crs
|
int | str | None
|
Target CRS for the mosaic, as an EPSG code ( |
None
|
resampling
|
str
|
Resampling method used when a source is reprojected to |
'nearest neighbor'
|
signer
|
Any
|
Optional signer exposing |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Note
The z-order methods ("first"/"last") preserve each source's data
type via BuildVRT + Translate. The reduction methods
("min"/"max"/"sum") align every source onto the union grid
with gdal.Warp (nearest resampling — exact for already-aligned tiles)
and write a single-precision-safe Float64 output regardless of the
source dtype, so they may differ in dtype from a z-order merge of the
same integer inputs.
Raises:
| Type | Description |
|---|---|
TypeError
|
|
ValueError
|
|
RuntimeError
|
GDAL failed to open a source, reproject it, or build the source mosaic. |
Examples:
- Mosaic two tiles, keeping the larger value wherever they overlap:
- Default last-wins compositing (unchanged from the previous behaviour):
- Mosaic tiles from two UTM zones into a single CRS:
- Mosaic Requester-Pays S3 tiles by passing a signer (no
withblock):
Source code in src/pyramids/dataset/merge.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
pyramids.dataset.merge.stack_bands(files, *, band_names=None, align=False, no_data_value=_INHERIT_NO_DATA, path=None, signer=None)
#
Stack N single-band rasters into one multi-band :class:Dataset.
Free-function alias for :meth:pyramids.dataset.Dataset.from_band_files
— see that method for the full contract, edge cases, and examples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[str | Path]
|
Single-band raster paths/URLs to stack (order = band order). |
required |
band_names
|
list[str] | None
|
Explicit per-band names; |
None
|
align
|
bool
|
When |
False
|
no_data_value
|
Any
|
No-data value for the output bands; omitted means "inherit from the source rasters". |
_INHERIT_NO_DATA
|
path
|
str | Path | None
|
Output |
None
|
signer
|
Any
|
Optional signer exposing |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A multi-band dataset, one band per input file. |