Repository: cogeotiff/rio-cogeo

Issue:
Allow compression for intermediate file
In https://github.com/cogeotiff/rio-cogeo/blob/92bba3a2b475451e78afd58e02e25d889c1ab910/rio_cogeo/cogeo.py#L210-L211 we remove the compression to speed up the process. While this is fine in most case, sometime we want to minimize the memory/disk space usage instead of the speed. 

We could add an option to `--allow-intermediate-compression`

Discussion:

Patch:
diff --git a/CHANGES.txt b/CHANGES.txt
index 2a36bf8..ccc5dd0 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,9 @@
+1.1.5 (TBD)
+------------------
+
+ - add `--allow-intermediate-compression` option to reduce the memory/disk footprint (#103)
+
+
 1.1.4 (2019-10-03)
 ------------------
 
diff --git a/rio_cogeo/cogeo.py b/rio_cogeo/cogeo.py
index 346e7dd..99695e3 100644
--- a/rio_cogeo/cogeo.py
+++ b/rio_cogeo/cogeo.py
@@ -65,6 +65,7 @@ def cog_translate(
     resampling="nearest",
     in_memory=None,
     config=None,
+    allow_intermediate_compression=False,
     quiet=False,
 ):
     """
@@ -102,6 +103,10 @@ def cog_translate(
         Force processing raster in memory (default: process in memory if small)
     config : dict
         Rasterio Env options.
+    allow_intermediate_compression: bool, optional (default: False)
+        Allow intermediate file compression to reduce memory/disk footprint.
+        Note: This could reduce the speed of the process.
+        Ref: https://github.com/cogeotiff/rio-cogeo/issues/103
     quiet: bool, optional (default: False)
         Mask processing steps.
 
@@ -207,8 +212,9 @@ def cog_translate(
                     meta.pop("alpha", None)
 
                 meta.update(**dst_kwargs)
-                meta.pop("compress", None)
-                meta.pop("photometric", None)
+                if not allow_intermediate_compression:
+                    meta.pop("compress", None)
+                    meta.pop("photometric", None)
 
                 if in_memory is None:
                     in_memory = vrt_dst.width * vrt_dst.height < IN_MEMORY_THRESHOLD
diff --git a/rio_cogeo/scripts/cli.py b/rio_cogeo/scripts/cli.py
index 9f244fb..aa81bbd 100644
--- a/rio_cogeo/scripts/cli.py
+++ b/rio_cogeo/scripts/cli.py
@@ -147,6 +147,12 @@ def cogeo():
     help="Force processing raster in memory / not in memory (default: process in memory "
     "if smaller than {:.0f} million pixels)".format(IN_MEMORY_THRESHOLD // 1e6),
 )
+@click.option(
+    "--allow-intermediate-compression",
+    default=False,
+    is_flag=True,
+    help="Allow intermediate file compression to reduce memory/disk footprint.",
+)
 @click.option(
     "--threads",
     type=ThreadsParamType(),
@@ -172,6 +178,7 @@ def create(
     latitude_adjustment,
     resampling,
     in_memory,
+    allow_intermediate_compression,
     threads,
     creation_options,
     quiet,
@@ -209,6 +216,7 @@ def create(
         resampling=resampling,
         in_memory=in_memory,
         config=config,
+        allow_intermediate_compression=allow_intermediate_compression,
         quiet=quiet,
     )
 


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index e447982..b086841 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -418,6 +418,22 @@ def test_cogeo_validTempFile(monkeypatch, runner):
         assert result.exit_code == 0
 
 
+def test_cogeo_validCompress(monkeypatch, runner):
+    """Should work as expected."""
+    with runner.isolated_filesystem():
+        result = runner.invoke(
+            cogeo,
+            [
+                "create",
+                raster_path_rgb,
+                "output.tif",
+                "--allow-intermediate-compression",
+            ],
+        )
+        assert not result.exception
+        assert result.exit_code == 0
+
+
 def test_cogeo_validate(runner):
     """Should work as expected."""
     with runner.isolated_filesystem():