Skip to content

Commit afb2e24

Browse files
authored
Sync geotiff compression docstrings against codec map (#1562) (#1589)
* Sync compression docstrings against actual codec map (#1562) _writer.write listed three codecs ('none', 'deflate', 'lzw') while _compression_tag accepts nine. to_geotiff listed six and omitted 'lz4', 'jpeg2000'/'j2k', and 'lerc'. Update both docstrings to match _VALID_COMPRESSIONS / _compression_tag. Also fill in the missing parameter docs on _writer.write that grew silently as features landed: compression_level, overview_resampling, crs_wkt, raster_type, x_resolution, y_resolution, resolution_unit, gdal_metadata_xml, extra_tags, bigtiff, max_z_error. Docs-only change. * Document JPEG constraints and overview_levels list-length semantics - JPEG entry in the compression= list now notes the hard uint8 + 1-or-3 bands constraint enforced in write(). - overview_levels docstring no longer implies the values are decimation factors. The implementation only consumes the list length; each overview unconditionally halves the previous resolution.
1 parent 0e60724 commit afb2e24

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

xrspatial/geotiff/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -810,10 +810,14 @@ def to_geotiff(data: xr.DataArray | np.ndarray, path, *,
810810
nodata : float, int, or None
811811
NoData value.
812812
compression : str
813-
'none', 'deflate', 'lzw', 'jpeg', 'packbits', or 'zstd'.
814-
JPEG is lossy and only supports uint8 data (1 or 3 bands).
815-
With ``gpu=True``, JPEG uses nvJPEG for GPU-accelerated
816-
encode/decode when available, falling back to Pillow on CPU.
813+
Codec name. One of ``'none'``, ``'deflate'``, ``'lzw'``,
814+
``'jpeg'``, ``'packbits'``, ``'zstd'``, ``'lz4'``,
815+
``'jpeg2000'`` (alias ``'j2k'``), or ``'lerc'``.
816+
``'jpeg'`` is currently rejected on write because the encoder
817+
omits the JPEGTables tag and produced files do not round-trip
818+
through libtiff / GDAL / rasterio. Use ``'deflate'``, ``'zstd'``,
819+
or ``'lzw'`` instead. ``'lerc'`` accepts ``max_z_error`` for
820+
lossy compression with a bounded per-pixel error.
817821
compression_level : int or None
818822
Compression effort level. None uses each codec's default (6 for
819823
deflate/zstd). Valid ranges: deflate 1-9, zstd 1-22, lz4 0-16.

xrspatial/geotiff/_writer.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,10 +998,21 @@ def write(data: np.ndarray, path: str, *,
998998
Pixel-to-coordinate mapping.
999999
crs_epsg : int or None
10001000
EPSG code.
1001+
crs_wkt : str or None
1002+
WKT string. Used only when ``crs_epsg`` is None.
10011003
nodata : float, int, or None
10021004
NoData value.
10031005
compression : str
1004-
'none', 'deflate', or 'lzw'.
1006+
Codec name. One of ``'none'``, ``'deflate'``, ``'lzw'``,
1007+
``'jpeg'``, ``'packbits'``, ``'zstd'``, ``'lz4'``,
1008+
``'jpeg2000'`` (alias ``'j2k'``), or ``'lerc'``.
1009+
``'jpeg'`` is only valid for ``uint8`` data with 1 or 3 bands;
1010+
any other dtype or band count raises ``ValueError``.
1011+
compression_level : int or None
1012+
Effort level forwarded to the codec. None uses each codec's
1013+
default. Valid ranges: deflate 1-9, zstd 1-22, lz4 0-16.
1014+
Codecs without a level concept (lzw, packbits, jpeg) accept any
1015+
value and ignore it.
10051016
tiled : bool
10061017
Use tiled layout (vs strips).
10071018
tile_size : int
@@ -1013,8 +1024,36 @@ def write(data: np.ndarray, path: str, *,
10131024
cog : bool
10141025
Write as Cloud Optimized GeoTIFF.
10151026
overview_levels : list of int or None
1016-
Overview decimation factors (e.g. [2, 4, 8]).
1017-
Only used if cog=True. If None and cog=True, auto-generate.
1027+
Number of overviews to generate, expressed as a list. Only the
1028+
list *length* is used: each overview halves the previous one,
1029+
regardless of the values supplied (``[2, 4, 8]`` and ``[1, 1, 1]``
1030+
both produce 2x / 4x / 8x decimations). Only used if
1031+
``cog=True``. If None and ``cog=True``, levels auto-generate
1032+
until the next halving would fall below ``tile_size``.
1033+
overview_resampling : str
1034+
Resampling method for overviews: ``'mean'`` (default),
1035+
``'nearest'``, ``'min'``, ``'max'``, ``'median'``, ``'mode'``,
1036+
or ``'cubic'``.
1037+
raster_type : int
1038+
TIFF ``GTRasterTypeGeoKey`` value. ``1`` (default) = PixelIsArea,
1039+
``2`` = PixelIsPoint.
1040+
x_resolution, y_resolution : float or None
1041+
Pixels per ``resolution_unit`` along each axis. Written into the
1042+
TIFF XResolution / YResolution tags.
1043+
resolution_unit : int or None
1044+
TIFF ResolutionUnit tag. ``1`` = none, ``2`` = inch, ``3`` = cm.
1045+
gdal_metadata_xml : str or None
1046+
Raw XML payload written to the ``GDAL_METADATA`` tag. Used to
1047+
round-trip arbitrary GDAL-style metadata.
1048+
extra_tags : list or None
1049+
Additional TIFF tags to emit, as a list of
1050+
``(tag_id, type_id, count, value)`` tuples.
1051+
bigtiff : bool or None
1052+
Force BigTIFF (64-bit offsets). None auto-promotes when the
1053+
estimated file size would exceed the classic-TIFF 4 GB limit.
1054+
max_z_error : float
1055+
Per-pixel error budget for LERC compression. ``0.0`` (default)
1056+
is lossless. Only valid with ``compression='lerc'``.
10181057
"""
10191058
comp_tag = _compression_tag(compression)
10201059
pred_int = normalize_predictor(predictor, data.dtype, comp_tag)

0 commit comments

Comments
 (0)