Skip to content

Commit

Permalink
add transform and crs when file has gcps
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Jun 12, 2024
1 parent c9ffcae commit c987244
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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]),
}
)

if nodata is not None:
vrt_params.update(
Expand Down
23 changes: 23 additions & 0 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 @@ -744,3 +745,25 @@ 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 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

0 comments on commit c987244

Please sign in to comment.