Skip to content

Commit

Permalink
add more metadata info (#159)
Browse files Browse the repository at this point in the history
* add more metadata info

* add missing test

* update changes [skip-ci]
  • Loading branch information
vincentsarago authored Mar 13, 2020
1 parent acce94d commit f3f5843
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- added `rio_tiler.utils.has_mask_band` function
- added `rio_tiler.utils.get_overview_level` to calculate the overview level needed for partial reading.
- added type hints
- added scale, offsets, colormap, datatype and colorinterp in reader.metadata output (#158)

Breaking Changes:
- removed python 2 support
Expand Down
18 changes: 17 additions & 1 deletion rio_tiler/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,24 @@ def _get_descr(ix):
for b in range(data.shape[0])
}

other_meta = dict()
if src_dst.scales[0] and src_dst.offsets[0]:
other_meta.update(dict(scale=src_dst.scales[0]))
other_meta.update(dict(offset=src_dst.offsets[0]))

try:
cmap = src_dst.colormap(1)
other_meta.update(dict(colormap=cmap))
except ValueError:
pass

return dict(
bounds=bounds, statistics=statistics, band_descriptions=band_descriptions
bounds=bounds,
statistics=statistics,
band_descriptions=band_descriptions,
dtype=src_dst.meta["dtype"],
colorinterp=[c.name for c in src_dst.colorinterp],
**other_meta,
)


Expand Down
Binary file added tests/fixtures/cog_cmap.tif
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

COG_WEB_TILED = os.path.join(os.path.dirname(__file__), "fixtures", "web.tif")
COG_SCALE = os.path.join(os.path.dirname(__file__), "fixtures", "cog_scale.tif")
COG_CMAP = os.path.join(os.path.dirname(__file__), "fixtures", "cog_cmap.tif")


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -450,3 +451,23 @@ def test_read_unscale():
src_dst, [310000, 4100000], coord_crs=src_dst.crs, unscale=True
)
assert round(p[0], 3) == 1000.892


def test_metadata():
"""Should return correct metadata."""
with rasterio.open(COG_CMAP) as src_dst:
meta = reader.metadata(src_dst)
assert meta["dtype"] == "int8"
assert meta["colorinterp"] == ["palette"]
assert not meta.get("scale")
assert not meta.get("ofsset")
assert meta.get("colormap")

with rasterio.open(COG_SCALE) as src_dst:
meta = reader.metadata(src_dst)
assert meta["dtype"] == "int16"
assert meta["colorinterp"] == ["gray"]
assert meta["scale"] == 0.0001
assert meta["offset"] == 1000.0
assert meta["band_descriptions"] == [(1, "Green")]
assert not meta.get("colormap")

0 comments on commit f3f5843

Please sign in to comment.