Skip to content

Commit

Permalink
Add Progress Option
Browse files Browse the repository at this point in the history
  • Loading branch information
SellersEvan committed Nov 14, 2023
1 parent f9ea9b9 commit ac8dc52
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,28 @@ with MemoryFile() as mem_dst:
client = boto3_session.client("s3")
client.upload_fileobj(mem_dst, "my-bucket", "my-key")
```

3. Progress to TextIO

```python
from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles

config = dict(
GDAL_NUM_THREADS="ALL_CPUS",
GDAL_TIFF_INTERNAL_MASK=True,
GDAL_TIFF_OVR_BLOCKSIZE="128",
)

with open("logfile.txt", "w+") as example:
cog_translate(
"example-input.tif",
"example-output.tif",
cog_profiles.get("deflate"),
config=config,
in_memory=False,
nodata=0,
quiet=False,
progress=example
)
```
14 changes: 14 additions & 0 deletions rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from contextlib import ExitStack, contextmanager
from typing import Any, Dict, List, Literal, Optional, Sequence, Tuple, Union

import _io
import click
import morecantile
import rasterio
Expand Down Expand Up @@ -93,6 +94,7 @@ def cog_translate( # noqa: C901
forward_band_tags: bool = False,
forward_ns_tags: bool = False,
quiet: bool = False,
progress: Union[bool, _io.TextIOWrapper] = False,
temporary_compression: str = "DEFLATE",
colormap: Optional[Dict] = None,
additional_cog_metadata: Optional[Dict] = None,
Expand Down Expand Up @@ -155,6 +157,8 @@ def cog_translate( # noqa: C901
Forward namespaces tags to output dataset.
quiet: bool, optional (default: False)
Mask processing steps.
progress: _io.TextIOWrapper, False, options (default: False)
Output progress precentage to TextIO. Quiet must be False.
temporary_compression: str, optional
Compression used for the intermediate file, default is deflate.
colormap: dict, optional
Expand Down Expand Up @@ -302,15 +306,25 @@ def cog_translate( # noqa: C901
click.echo("Reading input: {}".format(source), err=True)

fout = ctx.enter_context(open(os.devnull, "w")) if quiet else sys.stderr

def updateProgress(pct):
if type(progress) == _io.TextIOWrapper and not quiet:
progress.write(str(windows.pct) + "\n")
progress.flush()

with click.progressbar(wind, file=fout, show_percent=True) as windows: # type: ignore
for _, w in windows:
matrix = vrt_dst.read(window=w, indexes=indexes)
updateProgress(windows.pct)
tmp_dst.write(matrix, window=w)
updateProgress(windows.pct)

if add_mask or mask:
# Cast mask to uint8 to fix rasterio 1.1.2 error (ref #115)
mask_value = vrt_dst.dataset_mask(window=w).astype("uint8")
updateProgress(windows.pct)
tmp_dst.write_mask(mask_value, window=w)
updateProgress(windows.pct)

if overview_level is None:
overview_level = get_maximum_overview_level(
Expand Down

0 comments on commit ac8dc52

Please # to comment.