-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy path_reader.py
More file actions
1744 lines (1501 loc) · 67.5 KB
/
_reader.py
File metadata and controls
1744 lines (1501 loc) · 67.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""TIFF/COG reader: tile/strip assembly, windowed reads, HTTP range requests."""
from __future__ import annotations
import math
import mmap
import os as _os_module
import threading
import urllib.request
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
import numpy as np
from ._compression import (
COMPRESSION_LERC,
COMPRESSION_NONE,
decompress,
fp_predictor_decode,
lerc_decompress_with_mask,
predictor_decode,
unpack_bits,
)
from ._dtypes import SUB_BYTE_BPS, resolve_bits_per_sample, tiff_dtype_to_numpy
from ._geotags import (
GeoInfo,
GeoTransform,
RASTER_PIXEL_IS_POINT,
extract_geo_info,
extract_geo_info_with_overview_inheritance,
)
from ._header import (
IFD,
TIFFHeader,
parse_all_ifds,
parse_header,
select_overview_ifd,
validate_tile_layout,
)
# ---------------------------------------------------------------------------
# Allocation guard: reject TIFF dimensions that would exhaust memory
# ---------------------------------------------------------------------------
#: Default maximum total pixel count (width * height * samples).
#: ~1 billion pixels, which is ~4 GB for float32 single-band.
#: Override per-call via the ``max_pixels`` keyword argument.
MAX_PIXELS_DEFAULT = 1_000_000_000
def _check_dimensions(width, height, samples, max_pixels):
"""Raise ValueError if the requested allocation exceeds *max_pixels*."""
total = width * height * samples
if total > max_pixels:
raise ValueError(
f"TIFF image dimensions ({width} x {height} x {samples} = "
f"{total:,} pixels) exceed the safety limit of "
f"{max_pixels:,} pixels. Pass a larger max_pixels value to "
f"read_to_array() if this file is legitimate."
)
#: Default per-tile compressed-byte cap for HTTP COG reads. A crafted
#: ``TileByteCounts`` entry can declare arbitrarily many bytes, and the
#: HTTP path then tries to fetch and buffer that many bytes from the
#: server before it ever decompresses. 256 MiB tolerates legitimate
#: large tiles (RGB JPEG2000 at very high resolution can land in the
#: tens of MB) while keeping the fetch bounded. Override via the
#: ``XRSPATIAL_COG_MAX_TILE_BYTES`` environment variable.
MAX_TILE_BYTES_DEFAULT = 256 << 20 # 256 MiB
def _max_tile_bytes_from_env() -> int:
"""Read the per-tile byte cap from the environment, or fall back to the default."""
raw = _os_module.environ.get('XRSPATIAL_COG_MAX_TILE_BYTES')
if raw is None:
return MAX_TILE_BYTES_DEFAULT
try:
val = int(raw)
except (TypeError, ValueError):
return MAX_TILE_BYTES_DEFAULT
return max(1, val)
# ---------------------------------------------------------------------------
# Data source abstraction
# ---------------------------------------------------------------------------
#: Soft cap on the number of mmap entries the reader keeps open at once.
#: When the cache size exceeds this, the least-recently-used *idle* entry
#: (refcount 0) is closed. In-use entries are never evicted. Override via
#: the ``XRSPATIAL_GEOTIFF_MMAP_CACHE_SIZE`` environment variable.
_DEFAULT_MMAP_CACHE_SIZE = 32
def _mmap_cache_size_from_env() -> int:
"""Read the cache size cap from the environment, falling back to the default."""
raw = _os_module.environ.get('XRSPATIAL_GEOTIFF_MMAP_CACHE_SIZE')
if raw is None:
return _DEFAULT_MMAP_CACHE_SIZE
try:
val = int(raw)
except (TypeError, ValueError):
return _DEFAULT_MMAP_CACHE_SIZE
return max(1, val)
class _MmapCache:
"""Thread-safe, reference-counted, bounded LRU mmap cache.
Multiple threads reading the same file share a single read-only mmap.
The cache keeps idle (refcount 0) mmaps around so repeated opens of the
same file avoid the cost of re-mapping. When the number of entries
exceeds the cap (default 32, or ``XRSPATIAL_GEOTIFF_MMAP_CACHE_SIZE``),
the least-recently-used *idle* entry is evicted. Entries with active
references are never evicted.
mmap slicing on a read-only mapping is thread-safe (no seek involved).
"""
def __init__(self, max_size: int | None = None):
self._lock = threading.Lock()
# path -> entry list. Each entry is
# [fh, mm, size, refcount, ident, orphaned]
#
# ``ident`` is (st_ino, st_size, st_mtime_ns) used to spot files that
# were replaced (e.g. via ``os.replace`` on an atomic write) at the
# same path. ``orphaned`` is True once the entry has been removed
# from ``self._entries`` (typically because the underlying file was
# replaced). An orphaned entry is no longer the cache slot for the
# path, but live ``_FileSource`` instances still hold the entry list
# by reference and decrement *its* refcount on release. This keeps
# holders of the old mmap unaffected by any new acquires for the
# same path. ``OrderedDict`` gives LRU semantics via move_to_end.
self._entries: OrderedDict[str, list] = OrderedDict()
self._max_size = (max_size if max_size is not None
else _mmap_cache_size_from_env())
@staticmethod
def _file_ident(path: str):
"""Return a (st_ino, st_size, st_mtime_ns) tuple for *path* or None."""
try:
st = _os_module.stat(path)
except OSError:
return None
return (st.st_ino, st.st_size, st.st_mtime_ns)
@staticmethod
def _close_entry_locked(entry):
"""Close the file handle and mmap for *entry* (must be idle)."""
if entry[1] is not None:
entry[1].close()
entry[0].close()
def acquire(self, path: str):
"""Get or create a read-only mmap for *path*.
Returns ``(mm, size, entry)``. The opaque ``entry`` token must be
passed back to :meth:`release` so the matching reference count is
decremented even after the cache slot has been replaced (e.g. by an
atomic file overwrite at the same path).
"""
real = _os_module.path.realpath(path)
with self._lock:
entry = self._entries.get(real)
ident = self._file_ident(real)
if entry is not None:
# If the file at this path has been replaced (different inode,
# size, or mtime) the cached mmap is stale. Drop the entry so
# we re-open below. If the old entry is still in use by other
# callers, leave their mmap valid -- they still hold a
# reference -- but mark it orphaned so a later release of
# *that* entry closes its own resources rather than touching
# the new cache slot.
if ident is not None and entry[4] != ident:
self._entries.pop(real)
entry[5] = True # orphaned
if entry[3] <= 0:
self._close_entry_locked(entry)
entry = None
if entry is not None:
entry[3] += 1
self._entries.move_to_end(real)
return entry[1], entry[2], entry
fh = open(real, 'rb')
fh.seek(0, 2)
size = fh.tell()
fh.seek(0)
if size > 0:
mm = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
else:
mm = None
# Re-stat after opening so size matches the mmap we built.
ident = self._file_ident(real) or (0, size, 0)
new_entry = [fh, mm, size, 1, ident, False]
self._entries[real] = new_entry
self._evict_locked()
return mm, size, new_entry
def release(self, entry):
"""Decrement the reference count for the supplied entry token.
When the count hits zero on a still-cached entry, it stays cached
(keyed by realpath) until LRU eviction or :meth:`clear`. When the
count hits zero on an orphaned entry, its file handle and mmap are
closed immediately because no further callers can reach it.
"""
with self._lock:
entry[3] -= 1
if entry[3] > 0:
return
if entry[5]:
# Orphaned: not in the dict; close now.
self._close_entry_locked(entry)
return
# Find the path so we can move it to the LRU tail. The entry
# identity is unique per realpath while non-orphaned, so a
# linear search over a small dict is fine.
for key, ent in self._entries.items():
if ent is entry:
self._entries.move_to_end(key)
break
self._evict_locked()
def _evict_locked(self):
"""Drop oldest *idle* entries until the cache is at or below the cap."""
if len(self._entries) <= self._max_size:
return
# Walk from the front (oldest); only close idle (refcount 0) entries.
# An in-use entry can still happen to be at the front if the same
# file was acquired long ago and held; skip it.
to_drop = []
for key, entry in list(self._entries.items()):
if len(self._entries) - len(to_drop) <= self._max_size:
break
if entry[3] <= 0:
to_drop.append(key)
for key in to_drop:
entry = self._entries.pop(key)
self._close_entry_locked(entry)
def clear(self):
"""Close and drop all idle entries (used by tests)."""
with self._lock:
for key in [k for k, v in self._entries.items() if v[3] <= 0]:
entry = self._entries.pop(key)
self._close_entry_locked(entry)
# Module-level cache shared across all reads
_mmap_cache = _MmapCache()
class _FileSource:
"""Local file data source using a shared, thread-safe mmap cache."""
def __init__(self, path: str):
self._path = path
self._mm, self._size, self._entry = _mmap_cache.acquire(path)
def read_range(self, start: int, length: int) -> bytes:
if self._mm is not None:
return self._mm[start:start + length]
return b''
def read_all(self):
"""Return mmap object (supports slicing, struct.unpack_from, len)."""
if self._mm is not None:
return self._mm
return b''
@property
def size(self) -> int:
return self._size
def close(self):
if self._entry is not None:
_mmap_cache.release(self._entry)
self._entry = None
def _get_http_pool():
"""Return a module-level urllib3 PoolManager, or None if unavailable."""
global _http_pool
if _http_pool is not None:
return _http_pool
try:
import urllib3
_http_pool = urllib3.PoolManager(
num_pools=10,
maxsize=10,
retries=urllib3.Retry(total=2, backoff_factor=0.1),
)
return _http_pool
except ImportError:
return None
_http_pool = None
# ---------------------------------------------------------------------------
# HTTP range coalescing
# ---------------------------------------------------------------------------
#: Default gap threshold (bytes) for merging adjacent COG tile ranges into a
#: single GET. COG tiles are stored sequentially, so most adjacent ranges
#: differ by zero (back-to-back) or a few bytes; 1 MB tolerates small holes
#: caused by interleaved overview/mask data without ballooning over-fetch.
#: Most tiles are well under 1 MB compressed, so the coalesced GET stays
#: O(num_tiles) bytes plus at most one threshold of slack between tiles.
COALESCE_GAP_THRESHOLD_DEFAULT = 1 << 20 # 1 MB
def coalesce_ranges(
ranges: list[tuple[int, int]],
gap_threshold: int = COALESCE_GAP_THRESHOLD_DEFAULT,
) -> tuple[list[tuple[int, int]], list[tuple[int, int, int]]]:
"""Merge nearby ``(offset, length)`` ranges into fewer larger ones.
Parameters
----------
ranges : list of (offset, length)
Per-tile byte ranges to fetch. Order is preserved in the
``mapping`` output so callers can reassemble per-tile bytes.
gap_threshold : int
Maximum gap, in bytes, between two adjacent ranges before they
are merged. A gap of zero means perfectly back-to-back; larger
gaps trade some over-fetch for fewer round-trips.
Returns
-------
merged : list of (start, length)
Coalesced ranges, sorted by ``start``. Issue one GET per entry.
mapping : list of (merged_idx, rel_offset, length)
For each input range (in input order), the index of the merged
range its bytes live in, the offset within that merged range,
and the original length. Use with :func:`split_coalesced_bytes`.
Notes
-----
Empty input returns ``([], [])``. Negative gap thresholds disable
merging entirely (every input becomes its own merged range).
"""
if not ranges:
return [], []
# Tag each input with its original index so we can rebuild mapping.
indexed = sorted(
((off, length, i) for i, (off, length) in enumerate(ranges)),
key=lambda t: t[0],
)
merged: list[tuple[int, int]] = []
# mapping[input_idx] -> (merged_idx, rel_offset, length)
mapping: list[tuple[int, int, int]] = [(0, 0, 0)] * len(ranges)
cur_start, cur_length, first_idx = indexed[0]
cur_end = cur_start + cur_length
members = [(first_idx, cur_start, cur_length)]
for off, length, orig_idx in indexed[1:]:
gap = off - cur_end
if gap_threshold >= 0 and gap <= gap_threshold:
# Extend current merged range. Gaps may be negative if a
# later-listed range overlaps an earlier one; clamp so the
# merged length covers both.
new_end = max(cur_end, off + length)
cur_length = new_end - cur_start
cur_end = new_end
members.append((orig_idx, off, length))
else:
merged_idx = len(merged)
merged.append((cur_start, cur_length))
for orig, m_off, m_len in members:
mapping[orig] = (merged_idx, m_off - cur_start, m_len)
cur_start, cur_length, cur_end = off, length, off + length
members = [(orig_idx, off, length)]
merged_idx = len(merged)
merged.append((cur_start, cur_length))
for orig, m_off, m_len in members:
mapping[orig] = (merged_idx, m_off - cur_start, m_len)
return merged, mapping
def split_coalesced_bytes(
merged_bytes: list[bytes],
mapping: list[tuple[int, int, int]],
) -> list[bytes]:
"""Slice merged-GET payloads back into per-tile bytes using *mapping*.
Inverse of :func:`coalesce_ranges`. ``merged_bytes[i]`` must be the
bytes returned by the GET for the ``i``th merged range; the output
is one bytes object per original input range, in input order.
"""
out: list[bytes] = [b''] * len(mapping)
for orig_idx, (merged_idx, rel_off, length) in enumerate(mapping):
chunk = merged_bytes[merged_idx]
out[orig_idx] = chunk[rel_off:rel_off + length]
return out
class _HTTPSource:
"""HTTP data source using range requests with connection reuse.
Uses urllib3.PoolManager when available (reuses TCP connections and
TLS sessions across range requests to the same host). Falls back to
stdlib urllib.request if urllib3 is not installed.
"""
def __init__(self, url: str):
self._url = url
self._size = None
self._pool = _get_http_pool()
def read_range(self, start: int, length: int) -> bytes:
end = start + length - 1
if self._pool is not None:
resp = self._pool.request(
'GET', self._url,
headers={'Range': f'bytes={start}-{end}'},
)
return resp.data
# Fallback: stdlib
req = urllib.request.Request(
self._url,
headers={'Range': f'bytes={start}-{end}'},
)
with urllib.request.urlopen(req) as resp:
return resp.read()
def read_ranges(
self,
ranges: list[tuple[int, int]],
max_workers: int = 8,
) -> list[bytes]:
"""Fetch multiple ranges concurrently using a thread pool.
Each ``(start, length)`` pair is fetched with its own range request,
but requests run in parallel so total wall time is bounded by the
slowest worker rather than ``len(ranges) * RTT``.
Returns the bytes for each range in input order.
"""
if not ranges:
return []
if len(ranges) == 1:
start, length = ranges[0]
return [self.read_range(start, length)]
workers = min(max_workers, len(ranges))
results: list[bytes | None] = [None] * len(ranges)
with ThreadPoolExecutor(max_workers=workers) as ex:
future_to_idx = {
ex.submit(self.read_range, start, length): i
for i, (start, length) in enumerate(ranges)
}
for fut in future_to_idx:
idx = future_to_idx[fut]
results[idx] = fut.result()
return results # type: ignore[return-value]
def read_ranges_coalesced(
self,
ranges: list[tuple[int, int]],
max_workers: int = 8,
gap_threshold: int = COALESCE_GAP_THRESHOLD_DEFAULT,
) -> list[bytes]:
"""Fetch *ranges* using merged GETs where adjacent ranges allow it.
Wrapper around :meth:`read_ranges` that first calls
:func:`coalesce_ranges` to group nearby ranges into fewer larger
GETs, then splits the responses back per-input via
:func:`split_coalesced_bytes`. Returns bytes in input order, same
as :meth:`read_ranges`.
Setting *gap_threshold* to a negative number disables merging
and falls back to one GET per input range.
"""
if not ranges:
return []
merged, mapping = coalesce_ranges(ranges, gap_threshold=gap_threshold)
merged_bytes = self.read_ranges(merged, max_workers=max_workers)
return split_coalesced_bytes(merged_bytes, mapping)
def read_all(self) -> bytes:
if self._pool is not None:
resp = self._pool.request('GET', self._url)
return resp.data
with urllib.request.urlopen(self._url) as resp:
return resp.read()
@property
def size(self) -> int | None:
return self._size
def close(self):
pass
_CLOUD_SCHEMES = ('s3://', 'gs://', 'az://', 'abfs://')
def _is_fsspec_uri(path: str) -> bool:
"""Check if a path is a fsspec-compatible URI (not http/https/local)."""
if not isinstance(path, str):
return False
if path.startswith(('http://', 'https://')):
return False
return '://' in path
def _is_file_like(obj) -> bool:
"""Return True if obj exposes a binary file-like interface (read+seek+tell).
``tell`` is required because :class:`_BytesIOSource` uses it to compute
the buffer length via seek-to-end. ``os.PathLike`` instances don't
expose ``read``/``seek``/``tell`` and are excluded here so that
:func:`_coerce_path` can convert them to ``str`` upstream.
"""
return (
not isinstance(obj, str)
and hasattr(obj, 'read')
and hasattr(obj, 'seek')
and hasattr(obj, 'tell')
)
def _coerce_path(source):
"""Normalize ``os.PathLike`` (e.g. ``pathlib.Path``) to ``str``.
Strings and binary file-likes pass through unchanged. Used at the top
of every public reader/writer entry so that ``Path('mosaic.vrt')``
dispatches to the VRT path, ``Path('x.tif')`` derives a ``name``, etc.
"""
if isinstance(source, _os_module.PathLike):
return _os_module.fspath(source)
return source
class _BytesIOSource:
"""Data source backed by an in-memory or any seekable binary file-like.
Wraps a `BytesIO` or any object exposing ``read``/``seek`` so the reader
can issue windowed byte reads without touching the filesystem. Concurrent
callers (e.g. parallel tile decode) are serialized through a lock around
the seek+read pair so they don't race on the underlying buffer's cursor.
"""
def __init__(self, fileobj):
# _is_file_like (the gate that lets us reach this constructor)
# already requires read/seek/tell, so we can call tell() directly
# rather than guarding it. We do still defend against tell raising
# on a closed/detached buffer with an informative error.
self._fh = fileobj
self._lock = threading.Lock()
try:
cur = fileobj.tell()
fileobj.seek(0, 2)
self._size = fileobj.tell()
fileobj.seek(cur)
except (OSError, ValueError) as e:
raise ValueError(
f"file-like source is not usable for size measurement: "
f"{type(e).__name__}: {e}"
) from e
def read_range(self, start: int, length: int) -> bytes:
if length <= 0:
return b''
with self._lock:
self._fh.seek(start)
return self._fh.read(length)
def read_all(self):
with self._lock:
self._fh.seek(0)
return self._fh.read()
@property
def size(self) -> int:
return self._size
def close(self):
# Don't close the caller's buffer -- they own it.
self._fh = None
class _CloudSource:
"""Cloud storage data source using fsspec.
Supports S3, GCS, Azure Blob Storage, and any other fsspec backend.
Requires the appropriate library (s3fs, gcsfs, adlfs) to be installed.
"""
def __init__(self, url: str, **storage_options):
try:
import fsspec
except ImportError:
raise ImportError(
"fsspec is required to read from cloud storage. "
"Install it with: pip install fsspec")
self._url = url
self._fs, self._path = fsspec.core.url_to_fs(url, **storage_options)
self._size = self._fs.size(self._path)
def read_range(self, start: int, length: int) -> bytes:
with self._fs.open(self._path, 'rb') as f:
f.seek(start)
return f.read(length)
def read_all(self) -> bytes:
with self._fs.open(self._path, 'rb') as f:
return f.read()
@property
def size(self) -> int:
return self._size
def close(self):
pass
def _open_source(source):
"""Open a data source (local file, URL, cloud path, or file-like)."""
source = _coerce_path(source)
if _is_file_like(source):
return _BytesIOSource(source)
if not isinstance(source, str):
raise TypeError(
f"source must be a str path/URL or a binary file-like object "
f"with read+seek methods, got {type(source).__name__}")
if source.startswith(('http://', 'https://')):
return _HTTPSource(source)
if _is_fsspec_uri(source):
return _CloudSource(source)
return _FileSource(source)
def _apply_predictor(chunk: np.ndarray, pred: int, width: int,
height: int, bytes_per_sample: int,
samples: int = 1,
byte_order: str = '<') -> np.ndarray:
"""Apply the appropriate predictor decode to decompressed data.
``width``, ``height``, ``bytes_per_sample``, and ``samples`` describe
the raw pixel layout before predictor inversion: ``width * samples``
samples per row, each ``bytes_per_sample`` bytes wide.
Predictor=2 (horizontal differencing) operates at the *sample* level
per TIFF Technical Note (libtiff/GDAL convention): the difference is
taken between adjacent same-component samples in the sample's
natural bit width, with stride equal to ``samples`` samples. A
byte-wise implementation drops the inter-byte carry for multi-byte
samples and produces wrong values.
Predictor=3 (floating-point) byte-swizzles each row into
``bytes_per_sample`` interleaved lanes of length ``width * samples``,
per TIFF Technical Note 3. The un-transpose stage has to put the
MSB lane at the file's high-order byte position, which differs for
big- vs little-endian files; ``byte_order`` carries that.
"""
if pred == 2:
return predictor_decode(chunk, width, height,
bytes_per_sample, samples=samples,
byte_order=byte_order)
elif pred == 3:
return fp_predictor_decode(chunk, width * samples, height,
bytes_per_sample,
big_endian=(byte_order == '>'))
return chunk
def _packed_byte_count(pixel_count: int, bps: int) -> int:
"""Compute the number of packed bytes for sub-byte bit depths."""
return (pixel_count * bps + 7) // 8
def _int_nodata_in_range(nodata_int: int, dtype: np.dtype) -> bool:
"""Return True iff *nodata_int* is representable as *dtype*.
Used to gate ``dtype.type(int(...))`` casts that would otherwise raise
``OverflowError`` on real-world files that pair an unsigned dtype with
a negative GDAL_NODATA sentinel (e.g. uint16 + ``-9999``). When the
sentinel cannot be represented, the file's pixels can never match it,
so the caller should treat the sentinel as a no-op for value matching
(still surfacing it via ``attrs['nodata']`` so write round-trips
preserve the original tag).
"""
if dtype.kind not in ('u', 'i'):
return False
info = np.iinfo(dtype)
return info.min <= nodata_int <= info.max
def _resolve_masked_fill(nodata_str: str | None, dtype: np.dtype):
"""Resolve the value to use when restoring LERC-masked pixels.
Mirrors :func:`_sparse_fill_value` but defaults to NaN for floating
dtypes when the file does not declare a nodata sentinel. Float
rasters with no GDAL_NODATA tag still benefit from NaN propagation
because LERC's zero fill would silently masquerade as a real
measurement at z == 0.
Note: integer dtypes with no GDAL_NODATA tag fall back to ``0``,
which is the same value LERC zero-fills masked pixels with -- in
that case the mask application is intentionally a no-op. We avoid
inventing an integer sentinel (e.g. iinfo.max) because doing so
would silently change pixel values for files that never declared
one, breaking downstream consumers that key off the original data.
Out-of-range integer sentinels (e.g. ``uint16`` paired with
``GDAL_NODATA="-9999"``, common on legacy GDAL files) cannot be
represented in the file dtype and so cannot match any decoded
pixel; we fall back to ``0`` rather than raising ``OverflowError``
on the dtype cast.
"""
if nodata_str is not None:
try:
v = float(nodata_str)
if dtype.kind == 'f':
return dtype.type(v)
if not math.isnan(v) and not math.isinf(v):
nodata_int = int(v)
if _int_nodata_in_range(nodata_int, dtype):
return dtype.type(nodata_int)
except (TypeError, ValueError):
pass
if dtype.kind == 'f':
return dtype.type(np.nan)
return dtype.type(0)
def _decode_strip_or_tile(data_slice, compression, width, height, samples,
bps, bytes_per_sample, is_sub_byte, dtype, pred,
byte_order='<', jpeg_tables=None,
masked_fill=None):
"""Decompress, apply predictor, unpack sub-byte, and reshape a strip/tile.
Parameters
----------
byte_order : str
'<' for little-endian, '>' for big-endian. When the file byte
order differs from the system's native order, pixel data is
byte-swapped after decompression.
jpeg_tables : bytes or None
Raw bytes of the file's JPEGTables tag (347), or None if the file
doesn't have one. GDAL-style tiled JPEG TIFFs store DQT/DHT tables
once in this tag and each tile is a JPEG fragment that depends on
them; the JPEG decoder splices the tables in before handing the
tile to libjpeg. Ignored for non-JPEG compressions.
masked_fill : scalar or None
Fill value written into pixels that the LERC valid-mask flags as
invalid. Only consulted for ``compression == COMPRESSION_LERC``
when the decoder returns a non-trivial mask; ignored for every
other codec. Callers should compute it once per IFD via
:func:`_resolve_masked_fill` (typically NaN for float dtypes or
the parsed ``GDAL_NODATA`` sentinel). When ``None``, masked
pixels are left at LERC's zero fill.
Returns an array shaped (height, width) or (height, width, samples).
"""
pixel_count = width * height * samples
if is_sub_byte:
expected = _packed_byte_count(pixel_count, bps)
else:
expected = pixel_count * bytes_per_sample
lerc_mask = None
if compression == COMPRESSION_LERC:
# LERC needs special handling: lerc.decode also returns a
# valid-mask which the generic decompress() dispatcher discards.
# We capture it here so masked pixels can be restored to nodata
# below, instead of leaking LERC's zero fill into the output.
# Forward ``expected`` so the wrapper rejects bombs at the
# blob-header level rather than after the full buffer is
# materialised (issue #1625).
decoded_bytes, lerc_mask = lerc_decompress_with_mask(
data_slice, expected_size=expected)
chunk = np.frombuffer(decoded_bytes, dtype=np.uint8)
else:
chunk = decompress(data_slice, compression, expected,
width=width, height=height, samples=samples,
jpeg_tables=jpeg_tables)
# Validate the decompressed byte count. A truncated deflate stream or a
# buggy compressor can produce fewer or more bytes than expected. Without
# this check the downstream reshape raises an opaque "cannot reshape array
# of size N into shape (h, w)" that hides which tile/strip broke. Edge
# tiles in a valid TIFF still decompress to the full tile_height x
# tile_width (the caller slices the top-left region), so this only fires
# on genuine corruption.
if chunk.size != expected:
raise ValueError(
f"Decompressed tile/strip size mismatch: expected {expected} "
f"bytes for a {width} x {height} x {samples} block "
f"(bps={bps}, compression={compression}), got {chunk.size}. "
f"The TIFF data is likely truncated or corrupt."
)
if pred in (2, 3) and not is_sub_byte:
if not chunk.flags.writeable:
chunk = chunk.copy()
chunk = _apply_predictor(chunk, pred, width, height,
bytes_per_sample, samples=samples,
byte_order=byte_order)
if is_sub_byte:
pixels = unpack_bits(chunk, bps, pixel_count)
else:
# Use the file's byte order for the view, then convert to native
file_dtype = dtype.newbyteorder(byte_order)
pixels = chunk.view(file_dtype)
if file_dtype.byteorder not in ('=', '|', _NATIVE_ORDER):
pixels = pixels.astype(dtype)
if samples > 1:
out = pixels.reshape(height, width, samples)
else:
out = pixels.reshape(height, width)
# Restore nodata in positions LERC flagged as invalid. LERC
# zero-fills masked pixels in the data array, which would otherwise
# be indistinguishable from real zero readings downstream.
if lerc_mask is not None and masked_fill is not None:
mask_arr = np.asarray(lerc_mask)
if mask_arr.ndim == 2 and out.ndim == 3:
mask_arr = mask_arr[..., None]
invalid = np.broadcast_to(mask_arr == 0, out.shape)
if invalid.any():
if not out.flags.writeable:
out = out.copy()
np.putmask(out, invalid, masked_fill)
return out
import sys as _sys
_NATIVE_ORDER = '<' if _sys.byteorder == 'little' else '>'
def _sparse_fill_value(ifd: IFD, dtype: np.dtype):
"""Resolve the fill value for sparse tiles/strips.
A sparse TIFF entry has TileByteCounts/StripByteCounts == 0 (and
typically the matching Offset == 0). GDAL emits these for SPARSE_OK
files where blocks containing only the nodata value are omitted.
The reader is expected to materialise such blocks as nodata, or
zero when nodata is unset (the default per the GDAL convention).
"""
nodata_str = ifd.nodata_str
if nodata_str is not None:
try:
v = float(nodata_str)
if dtype.kind == 'f':
return dtype.type(v)
if not math.isnan(v) and not math.isinf(v):
nodata_int = int(v)
if _int_nodata_in_range(nodata_int, dtype):
return dtype.type(nodata_int)
except (TypeError, ValueError):
pass
return dtype.type(0)
def _has_sparse(byte_counts) -> bool:
"""Return True if any tile/strip is empty (byte_count == 0)."""
if byte_counts is None:
return False
for bc in byte_counts:
if bc == 0:
return True
return False
# ---------------------------------------------------------------------------
# Strip reader
# ---------------------------------------------------------------------------
def _read_strips(data: bytes, ifd: IFD, header: TIFFHeader,
dtype: np.dtype, window=None,
max_pixels: int = MAX_PIXELS_DEFAULT) -> np.ndarray:
"""Read a strip-organized TIFF image.
Parameters
----------
data : bytes
Full file data.
ifd : IFD
Parsed IFD for this image.
header : TIFFHeader
File header.
dtype : np.dtype
Output pixel dtype.
window : tuple or None
(row_start, col_start, row_stop, col_stop) or None for full image.
max_pixels : int
Maximum allowed pixel count (width * height * samples).
Returns
-------
np.ndarray with shape (height, width) or windowed subset.
"""
width = ifd.width
height = ifd.height
samples = ifd.samples_per_pixel
compression = ifd.compression
rps = ifd.rows_per_strip
offsets = ifd.strip_offsets
byte_counts = ifd.strip_byte_counts
pred = ifd.predictor
bps = resolve_bits_per_sample(ifd.bits_per_sample)
bytes_per_sample = bps // 8
is_sub_byte = bps in SUB_BYTE_BPS
jpeg_tables = ifd.jpeg_tables
masked_fill = (_resolve_masked_fill(ifd.nodata_str, dtype)
if compression == COMPRESSION_LERC else None)
if offsets is None or byte_counts is None:
raise ValueError("Missing strip offsets or byte counts")
planar = ifd.planar_config # 1=chunky (interleaved), 2=planar (separate)
# Determine output region
if window is not None:
r0, c0, r1, c1 = window
r0 = max(0, r0)
c0 = max(0, c0)
r1 = min(height, r1)
c1 = min(width, c1)
else:
r0, c0, r1, c1 = 0, 0, height, width
out_h = r1 - r0
out_w = c1 - c0
_check_dimensions(out_w, out_h, samples, max_pixels)
# Sparse strips (StripByteCounts == 0) must materialise as nodata or 0
# rather than be decoded. Pre-fill the result so any skipped strips
# land on a known fill value.
sparse = _has_sparse(byte_counts)
if sparse:
fill = _sparse_fill_value(ifd, dtype)
if samples > 1:
result = np.full((out_h, out_w, samples), fill, dtype=dtype)
else:
result = np.full((out_h, out_w), fill, dtype=dtype)
elif samples > 1:
result = np.empty((out_h, out_w, samples), dtype=dtype)
else:
result = np.empty((out_h, out_w), dtype=dtype)
if planar == 2 and samples > 1:
strips_per_band = math.ceil(height / rps)
first_strip = r0 // rps
last_strip = min((r1 - 1) // rps, strips_per_band - 1)
for band_idx in range(samples):
band_offset = band_idx * strips_per_band
for strip_idx in range(first_strip, last_strip + 1):
global_idx = band_offset + strip_idx
if global_idx >= len(offsets):
continue
if byte_counts[global_idx] == 0:
# Sparse strip: result is already pre-filled.
continue
strip_row = strip_idx * rps
strip_rows = min(rps, height - strip_row)
if strip_rows <= 0:
continue
strip_data = data[offsets[global_idx]:offsets[global_idx] + byte_counts[global_idx]]
strip_pixels = _decode_strip_or_tile(
strip_data, compression, width, strip_rows, 1,
bps, bytes_per_sample, is_sub_byte, dtype, pred,
byte_order=header.byte_order,
jpeg_tables=jpeg_tables,
masked_fill=masked_fill)
src_r0 = max(r0 - strip_row, 0)
src_r1 = min(r1 - strip_row, strip_rows)
dst_r0 = max(strip_row - r0, 0)
dst_r1 = dst_r0 + (src_r1 - src_r0)
if dst_r1 > dst_r0:
result[dst_r0:dst_r1, :, band_idx] = strip_pixels[src_r0:src_r1, c0:c1]
else:
first_strip = r0 // rps
last_strip = min((r1 - 1) // rps, len(offsets) - 1)
for strip_idx in range(first_strip, last_strip + 1):
strip_row = strip_idx * rps
strip_rows = min(rps, height - strip_row)
if strip_rows <= 0:
continue
if byte_counts[strip_idx] == 0: