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=INHERIT_NO_DATA, init='nan', n='nan', method='last', dst_crs=None, resampling=DEFAULT_RESAMPLING, signer=None, *, bbox=None, bbox_crs=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"/"count"/"mean"— per-pixel reduction across every source overlapping that pixel, ignoring no-data. Each source is aligned onto the union grid and folded into a NaN-aware accumulator one strip at a time, so peak memory isO(strip)whichever reduction is asked for."count"reports how many sources contributed a valid value to each cell, and"mean"divides the total by that same count -- never by the number of sources merely overlapping the cell.
"median" is recognised but not implemented: it would have to hold every
overlapping source's value for a cell at once, which the one-accumulator strip
reduction cannot do, and asking for it raises :class:NotImplementedError
rather than returning an approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Sequence[str | Path]
|
Paths to all input rasters. |
required |
dst
|
str | Path
|
Path to the output raster. Its extension alone selects the output
driver ( |
required |
no_data_value
|
float | int | str | None
|
Stamped on the output bands as the nodata marker. Omitted means
inherit from the sources: the first value they declare wins, and
a disagreement warns. Passing a value explicitly overrides that --
but note that any real cell holding it becomes unreadable, which is
why 0 is a poor choice for an elevation, bathymetry, anomaly or
difference raster (#1086). Passing Whichever marker is settled on fills the pixels no source covers
too, on both write paths and however it was settled, so the gaps
really hold what the band declares. The value is read from each source's band 1, and one marker is
stamped on every band of the output. It is read from every source,
including one When no source declares one, a marker is chosen wherever there
is something for it to mark: a mosaic generally has pixels no source
covers, and leaving those undeclared makes them read as real data --
a literal An inherited value the output band cannot store -- a |
INHERIT_NO_DATA
|
init
|
float | int | str
|
Reported value for pixels with no source coverage in the VRT (z-order
methods only — the reduction methods never read it). Maps to
:func: It is the fallback, not the last word: whatever marker gets settled
on is filled into the gaps instead, so A numeric 1086 through a different argument -- and that warns. It must name a#number, or be a spelling of |
'nan'
|
n
|
float | int | str
|
Source pixels matching this value are ignored — both when building
the VRT mosaic (z-order, as |
'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 |
DEFAULT_RESAMPLING
|
signer
|
Any
|
Optional signer exposing |
None
|
bbox
|
Sequence[float] | None
|
Optional This is not a convenience for cropping afterwards: without it GDAL is
given no reason to read less, so a mosaic of remote sources pulls the
entire source extent through The window is resolved once, onto the mosaic's own pixel grid, and
used by both methods: z-order passes it to :func: A bbox must be ordered For a lon/lat mosaic the window is rewritten into the mosaic's own
longitude convention first, so a |
None
|
bbox_crs
|
int | str | None
|
CRS that |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Note
The z-order methods ("first"/"last") preserve the data type via
BuildVRT + Translate — specifically that of the first source in
z-order, which for method="first" is the last one you passed,
since the list is reversed so that it wins. That is the source
BuildVRT takes its band type from; one whose band type disagrees is
skipped entirely, with a GDAL warning naming it, so the output then
covers the remaining sources' extent rather than the union. The
reduction methods ("min"/"max"/"sum"/"count"/"mean")
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. "count" is Float64 too,
rather than an integer band of its own: one reduction path writes one
dtype, and a tally is exact in Float64 far past any plausible source
count. The warp itself is asked for
Float64 as well, so that the NaN marking a strip's uncovered pixels
survives it: rounded into an integer destination it would come back as a
real 0 (or the type's minimum for Int32) and be folded into the
result as data.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
|
TypeError
|
|
ValueError
|
|
RuntimeError
|
GDAL failed to open a source, reproject it, or build the
source mosaic. When a source is at fault the message names it and
its position in |
DriverNotExistError
|
|
FileFormatNotSupportedError
|
|
Warns:
| Type | Description |
|---|---|
UserWarning
|
The sources declare more than one distinct no-data value
(the first wins, and all of them are named); or the value they
declare is one the output's data type cannot store, so a storable
one is chosen in its place; or a numeric |
Examples:
- Mosaic two tiles, keeping the larger value wherever they overlap:
- Composite the average of every source covering a pixel, and write the
matching per-pixel tally of how many of them contributed it:
>>> from pyramids.dataset.merge import merge_rasters >>> merge_rasters( # doctest: +SKIP ... ["scene_a.tif", "scene_b.tif", "scene_c.tif"], ... "mosaic_mean.tif", ... method="mean", ... ) >>> merge_rasters( # doctest: +SKIP ... ["scene_a.tif", "scene_b.tif", "scene_c.tif"], ... "mosaic_count.tif", ... method="count", ... ) - Default last-wins compositing. The overlap rule is unchanged, but the mosaic's no-data marker is now inherited from the sources rather than set to 0 (#1086):
- Override the inherited marker on a z-order merge of integer tiles.
The pixels no tile covers are filled with
-1as well, so the marker the band declares is the one they hold: - Keep a different value in the uncovered pixels by naming
init. The band still declares-1, but the gaps hold0and stay readable: - Ask for no marker at all, so every value in the mosaic stays readable — including a real 0 m in an elevation model:
- 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
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 | |
pyramids.dataset.merge.stack_bands(files, *, band_names=None, align=False, resampling=None, 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
|
resampling
|
str | dict[str, str] | None
|
Which algorithm |
None
|
no_data_value
|
Any
|
No-data value stamped on the output bands. Omitted means
inherit from the source rasters: the first file that declares
one wins, a disagreement warns, and if none declares one the output
declares none either. Pass a value to override that, or |
INHERIT_NO_DATA
|
path
|
str | Path | None
|
Output path, whose extension selects the driver ( |
None
|
signer
|
Any
|
Optional signer exposing |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A multi-band dataset, one band per input file. |
Raises:
| Type | Description |
|---|---|
DriverNotExistError
|
|
FileFormatNotSupportedError
|
|
Warns:
| Type | Description |
|---|---|
UserWarning
|
The inputs declare more than one distinct no-data value,
while |
Examples:
- Stack a Landsat scene's per-band GeoTIFFs; the band names come from the file names:
- Name the bands and write the stack straight to disk:
- Resample mismatched inputs onto the first file's grid, and declare no sentinel on the result:
See Also
- :meth:
pyramids.dataset.Dataset.from_band_files: The method this delegates to, with the full contract and runnable examples. - :func:
merge_rasters: Mosaics rasters that tile the same area, rather than stacking rasters that share a grid.
Source code in src/pyramids/dataset/merge.py
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 | |