Commit 1624d13
authored
Widen read_vrt buffer to fit all selected band dtypes (#1701)
* Widen read_vrt output buffer to fit all selected band dtypes
read_vrt allocated the multi-band output buffer from selected_bands[0].dtype
only. Each band's source array was then assigned with
result[..., band_idx] = src_arr[...], which silently casts the source to
the narrow buffer dtype. A Float32 band 1 after a Byte band 0 returned
uint8 with the float values truncated; a Byte band with
<ScaleRatio>0.5</ScaleRatio> returned uint8 with the post-scale
fractional part lost.
Compute the effective per-band dtype (declared dtype, or float64 when
any source has scale or offset, matching the existing promotion at
_vrt.py L562-565) and take np.result_type across all selected bands
before allocating the buffer. The single-band branch follows the same
logic so a single-band scaled VRT also widens. All-integer VRTs without
scaling stay integer, so memory is not blown up for the common case.
Fixes #1696
* Address Copilot review on PR #1701: drop line-number cites, guard empty VRT
Three issues raised in review:
* `_vrt.py` allocation comment cited specific line numbers ("see L562-565")
for the ComplexSource scaling block. Line numbers drift; replace with a
named reference to the `# Apply ComplexSource scaling` block.
* The same comment claimed mixes "widen to float64". `np.result_type` may
also produce `float32` (e.g. NumPy 2.x on `uint8 + float32`) or
`complex128` when complex bands are present. Reword to describe the
common-dtype rule and list the typical outcomes.
* `test_vrt_multiband_dtype_1696.py` module docstring and one test
docstring cited `_vrt.py` L327-334 / L562-565. Replace with named
references (`parse_vrt` ComplexSource branch, `# Apply ComplexSource
scaling` block) that survive future edits.
* A VRT with zero `<VRTRasterBand>` elements made `np.result_type(*[])`
raise the generic "at least one array or dtype is required" error. Add
an explicit `if not selected_bands` guard that raises a clear
ValueError, plus a regression test asserting the new message.1 parent de75800 commit 1624d13
2 files changed
Lines changed: 477 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
525 | | - | |
| 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 | + | |
526 | 569 | | |
527 | | - | |
528 | | - | |
529 | | - | |
| 570 | + | |
530 | 571 | | |
531 | | - | |
532 | | - | |
533 | | - | |
| 572 | + | |
534 | 573 | | |
535 | 574 | | |
536 | 575 | | |
| |||
0 commit comments