Skip to content

Commit

Permalink
Update GDALColorInterp and ColorInterp to support new GCI_ constants …
Browse files Browse the repository at this point in the history
…of GDAL 3.10 (#3194)
  • Loading branch information
rouault authored Sep 30, 2024
1 parent c20ed3b commit 7d14093
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
22 changes: 22 additions & 0 deletions rasterio/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ class ColorInterp(IntEnum):
Y = 14
Cb = 15
Cr = 16
# Below values since GDAL 3.10
pan = 17
coastal = 18
rededge = 19
nir = 20
swir = 21
mwir = 22
lwir = 23
tir = 24
other_ir = 25
# GCI_IR_Reserved_1 = 26
# GCI_IR_Reserved_2 = 27
# GCI_IR_Reserved_3 = 28
# GCI_IR_Reserved_4 = 29
sar_ka = 30
sar_k = 31
sar_ku = 32
sar_x = 33
sar_c = 34
sar_s = 35
sar_l = 36
sar_p = 37


class Resampling(IntEnum):
Expand Down
80 changes: 64 additions & 16 deletions rasterio/gdal.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,70 @@ cdef extern from "gdal.h" nogil:
GRIORA_Mode
GRIORA_Gauss

ctypedef enum GDALColorInterp:
GCI_Undefined
GCI_GrayIndex
GCI_PaletteIndex
GCI_RedBand
GCI_GreenBand
GCI_BlueBand
GCI_AlphaBand
GCI_HueBand
GCI_SaturationBand
GCI_LightnessBand
GCI_CyanBand
GCI_YCbCr_YBand
GCI_YCbCr_CbBand
GCI_YCbCr_CrBand
GCI_Max

IF (CTE_GDAL_MAJOR_VERSION, CTE_GDAL_MINOR_VERSION) >= (3, 10):
cdef extern from "gdal.h" nogil:

ctypedef enum GDALColorInterp:
GCI_Undefined
GCI_GrayIndex
GCI_PaletteIndex
GCI_RedBand
GCI_GreenBand
GCI_BlueBand
GCI_AlphaBand
GCI_HueBand
GCI_SaturationBand
GCI_LightnessBand
GCI_CyanBand
GCI_YCbCr_YBand
GCI_YCbCr_CbBand
GCI_YCbCr_CrBand
GCI_PanBand
GCI_CoastalBand
GCI_RedEdgeBand
GCI_NIRBand
GCI_SWIRBand
GCI_MWIRBand
GCI_LWIRBand
GCI_TIRBand
GCI_OtherIRBand
GCI_IR_Reserved_1
GCI_IR_Reserved_2
GCI_IR_Reserved_3
GCI_IR_Reserved_4
GCI_SAR_Ka_Band
GCI_SAR_K_Band
GCI_SAR_Ku_Band
GCI_SAR_X_Band
GCI_SAR_C_Band
GCI_SAR_S_Band
GCI_SAR_L_Band
GCI_SAR_P_Band
GCI_SAR_Reserved_1
GCI_SAR_Reserved_2
GCI_Max
ELSE:
cdef extern from "gdal.h" nogil:

ctypedef enum GDALColorInterp:
GCI_Undefined
GCI_GrayIndex
GCI_PaletteIndex
GCI_RedBand
GCI_GreenBand
GCI_BlueBand
GCI_AlphaBand
GCI_HueBand
GCI_SaturationBand
GCI_LightnessBand
GCI_CyanBand
GCI_YCbCr_YBand
GCI_YCbCr_CbBand
GCI_YCbCr_CrBand


cdef extern from "gdal.h" nogil:

ctypedef struct GDALColorEntry:
short c1
Expand Down
4 changes: 3 additions & 1 deletion tests/test_colorinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import rasterio
from rasterio.enums import ColorInterp
from rasterio.env import GDALVersion

from .conftest import gdal_version

def test_cmyk_interp(tmpdir):
"""A CMYK TIFF has cyan, magenta, yellow, black bands."""
Expand Down Expand Up @@ -80,7 +82,7 @@ def test_set_colorinterp(path_rgba_byte_tif, tmpdir, dtype):
assert src.colorinterp == dst_ci


@pytest.mark.parametrize("ci", ColorInterp.__members__.values())
@pytest.mark.parametrize("ci", ColorInterp.__members__.values() if gdal_version >= GDALVersion(3, 10) else list(filter(lambda x: x <= 16, ColorInterp.__members__.values())))
def test_set_colorinterp_all(path_4band_no_colorinterp, ci):
"""Test setting with all color interpretations."""

Expand Down

0 comments on commit 7d14093

Please sign in to comment.