Skip to content

Commit

Permalink
Merge pull request #223 from cogeotiff/issue217
Browse files Browse the repository at this point in the history
surface band metadata in cog_info output
  • Loading branch information
vincentsarago authored Dec 16, 2021
2 parents 9c0e994 + 171cb8d commit ad913b5
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# 3.0.2 (2021-12-16)

* remove usage of (soon to be deprecated) `rasterio.path` (https://github.com/cogeotiff/rio-cogeo/pull/222)
* add band metadata in `cog_info` output and update `rio_cogeo.models.Info` (https://github.com/cogeotiff/rio-cogeo/pull/223)

# 3.0.1 (2021-10-27)

Expand Down
14 changes: 14 additions & 0 deletions rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ def cog_info(src_path: Union[str, pathlib.PurePath], **kwargs: Any) -> models.In
continue
tags.update({str.title(ns).replace("_", " "): src_dst.tags(ns=ns)})

band_metadata = {
f"Band {ix}": models.BandMetadata(
**{
"Description": src_dst.descriptions[ix - 1],
"ColorInterp": src_dst.colorinterp[ix - 1].name,
"Offset": src_dst.offsets[ix - 1],
"Scale": src_dst.scales[ix - 1],
"Metadata": src_dst.tags(ix),
}
)
for ix in src_dst.indexes
}

try:
colormap = src_dst.colormap(1)
except ValueError:
Expand Down Expand Up @@ -666,5 +679,6 @@ def cog_info(src_path: Union[str, pathlib.PurePath], **kwargs: Any) -> models.In
Profile=profile,
GEO=geo,
Tags=tags,
Band_Metadata=band_metadata,
IFD=ifds,
)
18 changes: 17 additions & 1 deletion rio_cogeo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Any, Dict, Optional, Sequence, Tuple

from pydantic import BaseModel
from pydantic import BaseModel, Field

BBox = Tuple[float, float, float, float]

Expand Down Expand Up @@ -59,6 +59,16 @@ class Config:
extra = "ignore"


class BandMetadata(DictLike, BaseModel):
"""band metadata"""

Description: Optional[str]
ColorInterp: str
Offset: float
Scale: float
Metadata: Optional[Dict[str, Any]]


class Info(DictLike, BaseModel):
"""rio-cogeo Info."""

Expand All @@ -72,4 +82,10 @@ class Info(DictLike, BaseModel):
Profile: Profile
GEO: Geo
Tags: Dict[str, Dict]
Band_Metadata: Dict[str, BandMetadata] = Field(None, alias="Band Metadata")
IFD: Sequence[IFD]

class Config:
"""Config for model."""

allow_population_by_field_name = True
33 changes: 32 additions & 1 deletion rio_cogeo/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ def validate(input, strict, config):
def info(input, to_json):
"""Dataset info."""
metadata = cog_info(input)

if to_json:
click.echo(metadata.json(exclude_none=True))
click.echo(metadata.json(exclude_none=True, by_alias=True))
else:

sep = 25
Expand Down Expand Up @@ -347,6 +348,36 @@ def info(input, to_json):
f""" {click.style(key, underline=True, bold=True)}: {val}"""
)

for ns, meta in metadata.Band_Metadata.items():
click.echo(
f"""
{click.style(ns, bold=True)}"""
)

if meta.Description:
click.echo(
f""" {click.style("Description", underline=True, bold=True)}: {meta.Description}"""
)
click.echo(
f""" {click.style("ColorInterp", underline=True, bold=True)}: {meta.ColorInterp}"""
)
if meta.Offset != 0.0 and meta.Scale != 1.0:
click.echo(
f""" {click.style("Offset", underline=True, bold=True)}: {meta.Offset}"""
)
click.echo(
f""" {click.style("Scale", underline=True, bold=True)}: {meta.Scale}"""
)

if meta.Metadata:
click.echo(
f""" {click.style("Metadata", underline=True, bold=True)}:"""
)
for key, val in meta.Metadata.items():
click.echo(
f""" {click.style(key, underline=True, bold=True)}: {val}"""
)

click.echo(
f"""
{click.style('IFD', bold=True)}
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ default_section = THIRDPARTY
[flake8]
ignore = E501,W503,E203
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist
max-complexity = 12
max-complexity = 14
max-line-length = 90

[mypy]
Expand Down
Binary file added tests/fixtures/cog_band_tags.tif
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
os.path.dirname(__file__), "fixtures", "validate", "image_dec.tif"
)
raster_path_gcps = os.path.join(os.path.dirname(__file__), "fixtures", "slc.tif")
raster_band_tags = os.path.join(
os.path.dirname(__file__), "fixtures", "cog_band_tags.tif"
)


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -528,3 +531,11 @@ def test_cogeo_info(runner):
result = runner.invoke(cogeo, ["info", raster_path_gcps])
assert not result.exception
assert result.exit_code == 0

result = runner.invoke(cogeo, ["info", raster_band_tags, "--json"])
assert not result.exception
assert result.exit_code == 0

result = runner.invoke(cogeo, ["info", raster_band_tags])
assert not result.exception
assert result.exit_code == 0
10 changes: 10 additions & 0 deletions tests/test_cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
raster_nocolormap = os.path.join(FIXTURES_DIR, "image_nocolormap.tif")
raster_badoutputsize = os.path.join(FIXTURES_DIR, "bad_output_vrt.tif")
raster_web_z5_z11 = os.path.join(FIXTURES_DIR, "image_web_z5_z11.tif")
raster_band_tags = os.path.join(FIXTURES_DIR, "cog_band_tags.tif")

jpeg_profile = cog_profiles.get("jpeg")
jpeg_profile.update({"blockxsize": 64, "blockysize": 64})
Expand Down Expand Up @@ -658,3 +659,12 @@ def test_gdal_cog_web_mask(runner):
quiet=True,
)
assert cog_validate("cogeo.tif")


def test_info_with_metadata():
"""Make sure info returns band metadata."""
info = cog_info(raster_band_tags)
assert info.Band_Metadata
assert info.dict(by_alias=True)["Band Metadata"]
assert info.Band_Metadata["Band 1"].Description == "Green"
assert info.Band_Metadata["Band 1"].Metadata

0 comments on commit ad913b5

Please sign in to comment.