Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add transform and crs when file has gcps #293

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 5.3.1 (2024-06-12)

* fix issue when creating COG from file with internal GCPS

## 5.3.0 (2024-03-02)

* add `decimation_base` option in `cogeo.cog_translate` (author @mccarthyryanc, https://github.com/cogeotiff/rio-cogeo/pull/285)
Expand Down
8 changes: 8 additions & 0 deletions rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from rasterio.io import DatasetReader, DatasetWriter, MemoryFile
from rasterio.rio.overview import get_maximum_overview_level
from rasterio.shutil import copy
from rasterio.transform import from_gcps as transform_from_gcps
from rasterio.vrt import WarpedVRT

from rio_cogeo import models, utils
Expand Down Expand Up @@ -249,6 +250,13 @@ def cog_translate( # noqa: C901
"height": src_dst.height,
"resampling": ResamplingEnums[resampling],
}
if src_dst.gcps[1]:
vrt_params.update(
{
"src_crs": src_dst.gcps[1],
"src_transform": transform_from_gcps(src_dst.gcps[0]),
}
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we don't do this, the output file will be only 0, which is quite weird TBH


if nodata is not None:
vrt_params.update(
Expand Down
41 changes: 38 additions & 3 deletions tests/test_cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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")
raster_ns_meta = os.path.join(FIXTURES_DIR, "dataset_namespace_metadata.tif")
raster_path_gcps = os.path.join(FIXTURES_DIR, "slc.tif")

jpeg_profile = cog_profiles.get("jpeg")
jpeg_profile.update({"blockxsize": 64, "blockysize": 64})
Expand Down Expand Up @@ -446,7 +447,9 @@ def test_cog_translate_forward_cmap(runner):
"""Colormap should be passed to the output file."""
with runner.isolated_filesystem():
with rasterio.open(raster_colormap) as dataset:
cog_translate(dataset, "cogeo.tif", deflate_profile, quiet=True)
cog_translate(
dataset, "cogeo.tif", deflate_profile, quiet=True, dtype="uint8"
)

with rasterio.open("cogeo.tif") as cog:
assert cog.colormap(1) == dataset.colormap(1)
Expand All @@ -456,7 +459,12 @@ def test_cog_translate_forward_cmap(runner):
cmap = {0: (0, 0, 0, 0), 1: (1, 2, 3, 255)}
with rasterio.open(raster_nocolormap) as dataset:
cog_translate(
dataset, "cogeo.tif", deflate_profile, quiet=True, colormap=cmap
dataset,
"cogeo.tif",
deflate_profile,
quiet=True,
colormap=cmap,
dtype="uint8",
)
with rasterio.open("cogeo.tif") as cog:
assert cog.colormap(1)[1] == cmap[1]
Expand All @@ -471,6 +479,7 @@ def test_cog_translate_forward_cmap(runner):
quiet=True,
colormap=cmap,
indexes=(1, 1, 1),
dtype="uint8",
)

# add an external colormap (warns of wrong colorinterp)
Expand All @@ -483,6 +492,7 @@ def test_cog_translate_forward_cmap(runner):
quiet=True,
colormap=cmap,
indexes=(1,),
dtype="uint8",
)
with rasterio.open("cogeo.tif") as cog:
assert cog.colormap(1)[1] == cmap[1]
Expand All @@ -492,7 +502,9 @@ def test_cog_translate_forward_cmap(runner):
# Input dataset has colorinterp set to `Palette` but no colormap
with pytest.warns(UserWarning):
with rasterio.open(raster_nocolormap) as dataset:
cog_translate(dataset, "cogeo.tif", deflate_profile, quiet=True)
cog_translate(
dataset, "cogeo.tif", deflate_profile, quiet=True, dtype="uint8"
)
with rasterio.open("cogeo.tif") as cog:
assert cog.colorinterp == dataset.colorinterp

Expand Down Expand Up @@ -744,3 +756,26 @@ def test_cog_translate_decimation_base(runner):

with rasterio.open("cogeo.tif") as src:
assert src.overviews(1)[0] == decimation_base


def test_cog_translate_gcps(runner):
"""Should create proper COG."""
with runner.isolated_filesystem():
cog_translate(
raster_path_gcps,
"cogeo.tif",
cog_profiles.get("deflate"),
quiet=True,
)

with rasterio.open("cogeo.tif") as cog, rasterio.open(
raster_path_gcps
) as source:
assert cog.read(1).max() == source.read(1).max()
assert cog.count == source.count

assert source.gcps[1] is not None
# TODO: when we use rio-cogeo, we're using WarpedVRT for the intermediate
# step. This result on the output COG to be `reprojected` automatically
# ref: https://github.com/cogeotiff/rio-cogeo/issues/292
assert cog.gcps[1] is None
Loading