Reading, inspecting & validating#
The read side of the COG surface: inspect structure without touching pixels, validate that a file is a valid COG, and read only the pixels you need via overview-decimated partial reads.
- Inspect —
cog_inforeads only headers/metadata (compression, predictor, blocksize, dtype, CRS/bounds/resolution, the overview pyramid, per-band tags, colour table) and returns a frozenCOGInfo. Cheap even for a large remote COG. - Validate —
validatereturns aValidationReport(usable as a bool);Dataset.is_cogis a fast metadata-only probe andDataset.validate_cogis the authoritative check. - Partial reads —
read_part/preview/point/read_tilerequest a smaller output size so GDAL serves from the nearest overview, fetching only the relevant byte ranges over/vsicurl/.
Structured inspection#
pyramids.dataset.cog.inspect
#
Structured Cloud Optimized GeoTIFF inspection.
Provides :func:cog_info — a GDAL-only, metadata-only inspection of a raster
that answers "what compression / predictor / blocksize / overview pyramid does
this COG use?" without reading any pixels. It depends only on the GDAL Python
bindings pyramids already uses.
The result is a frozen :class:COGInfo dataclass carrying the band/geo profile
plus a per-level :class:OverviewLevel list. Validity is delegated to
:func:pyramids.dataset.cog.validate.validate so :attr:COGInfo.is_cog agrees
with :meth:pyramids.dataset.engines.cog.COG.validate_cog.
OverviewLevel
dataclass
#
One level of a COG's internal overview pyramid.
Attributes:
| Name | Type | Description |
|---|---|---|
index |
int
|
Zero-based overview index (0 is the coarsest-to-finest order GDAL reports, i.e. index 0 is the first/largest overview). |
width |
int
|
Overview width in pixels. |
height |
int
|
Overview height in pixels. |
blocksize |
tuple[int, int]
|
|
decimation |
int
|
Integer shrink factor relative to full resolution,
|
Source code in src/pyramids/dataset/cog/inspect.py
COGInfo
dataclass
#
Structured metadata describing a (Cloud Optimized) GeoTIFF.
Attributes:
| Name | Type | Description |
|---|---|---|
is_cog |
bool
|
|
driver |
str
|
GDAL driver short name (e.g. |
width |
int
|
Full-resolution width in pixels. |
height |
int
|
Full-resolution height in pixels. |
band_count |
int
|
Number of raster bands. |
dtype |
str
|
GDAL data-type name of band 1 (e.g. |
crs_epsg |
int | None
|
EPSG code of the CRS, or |
bounds |
tuple[float, float, float, float]
|
|
resolution |
tuple[float, float]
|
|
compression |
str | None
|
|
predictor |
str | None
|
|
interleave |
str | None
|
|
blocksize |
tuple[int, int]
|
|
overviews |
list[OverviewLevel]
|
Per-level overview metadata, finest index first. |
band_tags |
dict[int, dict[str, Any]]
|
Per-band metadata dict keyed by 1-based band index. |
colormap |
bool
|
|
Source code in src/pyramids/dataset/cog/inspect.py
cog_info(path, config=None)
#
Inspect a raster and return its structured COG metadata.
Reads only headers/metadata (no pixels), so it is cheap even for very large
or remote (/vsicurl/) COGs. Validity is determined by the same validator
that backs :meth:pyramids.dataset.engines.cog.COG.validate_cog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Local path or |
required |
config
|
dict[str, str] | None
|
GDAL config options applied (via |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
COGInfo |
COGInfo
|
The structured metadata, including the overview pyramid. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
When |
Examples:
- Inspect a COG and read its compression and overview pyramid:
- A plain (non-COG) GeoTIFF reports
is_cog=Falsewith no overviews:
Source code in src/pyramids/dataset/cog/inspect.py
_cog_info_impl(p)
#
Build the :class:COGInfo for p (config context already applied).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
str
|
Local path or |
required |
Returns:
| Name | Type | Description |
|---|---|---|
COGInfo |
COGInfo
|
The structured metadata. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
When |
Source code in src/pyramids/dataset/cog/inspect.py
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 | |
Overview-decimated reads#
pyramids.dataset.engines.cog.COG
#
Bases: _Engine
Cloud Optimized GeoTIFF read/write/validate operations for Dataset.
Owns the real implementations of to_cog, is_cog (property),
and validate_cog. Dataset exposes a same-named facade for each
so ds.to_cog(...) and ds.cog.to_cog(...) are equivalent.
to_cog is the single owner of COG write policy: it applies the
house defaults, resolves the dtype-aware predictor and overview
resampling, and runs the STATISTICS retry. The
:func:pyramids.dataset.cog.write_cog facade is a thin delegator that
only normalises its input and forwards overrides here, so both entry
points produce identical output for identical input. The
categorical-raster resampling guardrail
(_warn_if_categorical_with_averaging) lives here too.
Source code in src/pyramids/dataset/engines/cog.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 | |
is_cog
property
#
True iff the backing file on disk is a valid COG.
False for MEM datasets, /vsimem/ paths, and unsaved
datasets (empty :attr:file_name).
Examples:
- Check the backing file of a newly-opened COG:
- Plain GeoTIFFs and MEM datasets return False:
- Use in a conditional pipeline:
_is_cog_cheap(path)
staticmethod
#
Fast, metadata-only heuristic for "is this file a COG?" (ARC-7).
Avoids the full COG validator on every is_cog access (which reads the
whole IFD/offset table — costly over /vsicurl). Checks: GTiff driver,
no external .ovr sidecar, internally tiled (square blocks or a single
tile), and internal overviews present when the image is larger than one
tile. This can FALSE-POSITIVE on a tiled GeoTIFF that is not laid out in
strict COG order — use :meth:validate_cog for the authoritative check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
On-disk or remote |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in src/pyramids/dataset/engines/cog.py
validate_cog(strict=False, config=None)
#
Validate the backing file as a COG.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict
|
bool
|
If |
False
|
config
|
dict[str, str] | None
|
GDAL config options for the read; defaults to the remote
read tuning for |
None
|
Returns:
| Type | Description |
|---|---|
ValidationReport
|
ValidationReport with errors, warnings, and structural details. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
Dataset has no on-disk backing file
(MEM-only or |
Examples:
- Validate and branch on the result:
- Strict mode promotes warnings to errors:
- Inspect structural details from the report:
Source code in src/pyramids/dataset/engines/cog.py
info(config=None)
#
Return structured COG metadata for the backing file.
Reads only headers/metadata (no pixels) and reports compression,
predictor, blocksize, dtype, CRS/bounds/resolution, the overview
pyramid, per-band tags, and colour-table presence. See
:class:pyramids.dataset.cog.inspect.COGInfo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, str] | None
|
GDAL config options for the read; defaults to the remote
read tuning for |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
COGInfo |
COGInfo
|
The structured metadata for the on-disk file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
Dataset has no on-disk backing file
(MEM-only or |
Examples:
- Inspect a COG's compression and overview pyramid:
- Read the tile size and band count:
Source code in src/pyramids/dataset/engines/cog.py
read_part(bbox, *, dst_width=None, dst_height=None, bbox_crs=4326, resampling='bilinear', band=None)
#
Read a geographic window, decimated from the nearest overview.
Requesting a dst_width/dst_height smaller than the source window
makes GDAL serve the data from the nearest overview level, so for a COG
over /vsicurl/ only the relevant byte ranges are fetched — the
cloud-native partial-read pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple[float, float, float, float]
|
|
required |
dst_width
|
int | None
|
Output width in pixels. Defaults to the source window width (no decimation). |
None
|
dst_height
|
int | None
|
Output height in pixels. Defaults to the source window height. |
None
|
bbox_crs
|
int
|
EPSG code of |
4326
|
resampling
|
str
|
One of |
'bilinear'
|
band
|
int | None
|
0-based band index. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: |
ndarray
|
|
ndarray
|
|
ndarray
|
only — no transform, bounds, or CRS is attached. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Unknown |
OutOfBoundsError
|
The window does not intersect the raster at all. |
Note
A window that only partially overlaps the raster is not
stretched to fill the output: the intersection is read and placed
at its correct offset inside a dst_height x dst_width buffer
whose out-of-raster remainder is filled with NoData (the band's
NoData value, else NaN for float / 0 for integer — see
:meth:_nodata_fill). A fully-inside window is returned without
padding. This keeps the result aligned to the requested window,
which matters for edge tiles served by :meth:read_tile.
Examples:
- Read a 256x256 decimated thumbnail of a bbox:
Source code in src/pyramids/dataset/engines/cog.py
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 | |
preview(*, max_size=1024, resampling='bilinear', band=None)
#
Read a whole-image thumbnail downsampled to max_size on the long edge.
Pulls from a coarse overview when one exists, so previewing a huge COG is cheap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
Maximum pixels on the longer edge. Defaults to 1024. |
1024
|
resampling
|
str
|
Resampling method (see :meth: |
'bilinear'
|
band
|
int | None
|
0-based band index. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: The downsampled array, |
ndarray
|
|
ndarray
|
or CRS is attached to the returned array. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Unknown |
Examples:
- Build a 128px thumbnail of a single band:
Source code in src/pyramids/dataset/engines/cog.py
point(x, y, *, point_crs=4326, band=None)
#
Sample band value(s) at a single coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
X / longitude / easting in |
required |
y
|
float
|
Y / latitude / northing in |
required |
point_crs
|
int
|
EPSG code of |
4326
|
band
|
int | None
|
0-based band index. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: A scalar 0-d array for a single band, or a |
ndarray
|
|
ndarray
|
coordinate metadata is attached. |
Raises:
| Type | Description |
|---|---|
OutOfBoundsError
|
The point falls outside the raster extent. |
Examples:
- Sample all bands at a lon/lat coordinate:
Source code in src/pyramids/dataset/engines/cog.py
read_tile(z, x, y, *, tilesize=256, resampling='bilinear', band=None)
#
Read a Web-Mercator XYZ/slippy-map tile.
Computes the EPSG:3857 bounds of tile (z, x, y) from the closed-form
Web-Mercator formula and delegates to :meth:read_part at tilesize
resolution — no extra tiling dependency needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
int
|
Zoom level. |
required |
x
|
int
|
Tile column index. |
required |
y
|
int
|
Tile row index (origin top-left / north-west). |
required |
tilesize
|
int
|
Output tile size in pixels (square). Defaults to 256. |
256
|
resampling
|
str
|
Resampling method (see :meth: |
'bilinear'
|
band
|
int | None
|
0-based band index. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: A |
ndarray
|
|
ndarray
|
georeferencing is defined by its |
ndarray
|
array; edge tiles are NoData-padded (see :meth: |
Raises:
| Type | Description |
|---|---|
OutOfBoundsError
|
The tile does not intersect the raster. |
Examples:
- Read the zoom-0 world tile of a global COG:
Source code in src/pyramids/dataset/engines/cog.py
Validation#
pyramids.dataset.cog.validate
#
COG validation wrapping osgeo_utils sample validator.
Provides :func:validate — a thin wrapper over
osgeo_utils.samples.validate_cloud_optimized_geotiff.validate (GDAL
ships it as a "sample"; the signature has drifted between GDAL 3.4 /
3.6 / 3.8 / 3.12, so we defensively probe the return shape). If the
import fails entirely, a minimal in-house fallback checks that the file
is tiled and has overviews.
Returns a :class:ValidationReport — a frozen dataclass usable as a
:class:bool (is_valid) with errors, warnings, and
details fields for richer reporting.
ValidationReport
dataclass
#
Outcome of validating whether a file is a Cloud Optimized GeoTIFF.
Attributes:
| Name | Type | Description |
|---|---|---|
is_valid |
bool
|
|
errors |
list[str]
|
Error messages (empty when valid). |
warnings |
list[str]
|
Non-fatal warnings (e.g., "no overviews"). |
details |
dict[str, Any]
|
Structural metadata from the validator — typically
|
Source code in src/pyramids/dataset/cog/validate.py
_osgeo_validate(path)
#
Invoke the osgeo_utils sample validator; return (errors, warnings, details).
The sample validator's signature has drifted across GDAL versions.
We probe defensively: GDAL 3.6+ returns
(warnings, errors, details) while older builds may return just
(warnings, errors).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path or |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Tuple of |
list[str]
|
to match this module's public convention. |
Raises:
| Type | Description |
|---|---|
ImportError
|
The |
FileNotFoundError
|
The underlying file cannot be opened
(raised via |
Source code in src/pyramids/dataset/cog/validate.py
_fallback_validate(path)
#
Minimal in-house validator used when the sample module is unavailable.
Checks: file opens; image is tiled (block dimensions smaller than full extent); at least one overview present. Does NOT check the IFD-before-data layout; recommends upgrading GDAL if used.
Heuristic limitations
The "is stripped" check compares the block shape reported by
:func:GetBlockSize — stripped TIFFs typically return
(width, small_N) (e.g. (512, 4)) while tiled files
return (tile, tile). The rule used is by!= bx and
by * 4 < bx, which:
- Correctly flags standard stripped layouts (
(W, 1),(W, 4),(W, 8)). - Correctly passes square-tiled COGs (
(256, 256),(512, 512)). - Can FALSE-NEGATIVE on pathological cases such as
near-square strips (
by == bx) — extremely rare in practice. - Can FALSE-POSITIVE on legitimately non-square TIFF tiles
(e.g.
(512, 128)used for tall elongated rasters) — also rare; the GTiff driver requires square tiles for COG.
The authoritative check is the TIFF TILEWIDTH /
STRIPBYTECOUNTS tag, but reading it requires either
:mod:tifffile or a direct libtiff binding. We accept
the heuristic because this fallback runs only when
:mod:osgeo_utils.samples.validate_cloud_optimized_geotiff
is unavailable — which, in practice, is never on GDAL >= 3.4.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path or |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
|
list[str]
|
func: |
Source code in src/pyramids/dataset/cog/validate.py
validate(path, strict=False, config=None)
#
Validate that the file at path is a valid Cloud Optimized GeoTIFF.
Delegates to osgeo_utils.samples.validate_cloud_optimized_geotiff
when available (GDAL ≥ 3.4). Falls back to a minimal in-house check
(tiled + overviews) when the import fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Local path or |
required |
strict
|
bool
|
If |
False
|
config
|
dict[str, str] | None
|
GDAL config options applied (via |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ValidationReport |
ValidationReport
|
Includes |
ValidationReport
|
and a |
|
ValidationReport
|
boolean ( |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
When a local |
Examples:
- Validate a local COG and inspect the report:
- Strict mode promotes warnings (e.g. "no overviews") to errors:
- Validate a cloud-hosted COG via VSI path:
Source code in src/pyramids/dataset/cog/validate.py
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 | |