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=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"— 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. Its extension alone selects the output
driver ( |
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 |
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 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. |
DriverNotExistError
|
|
FileFormatNotSupportedError
|
|
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
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
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 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
|
|