Skip to content

Commit

Permalink
Added Progress Out - Alt Text Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
SellersEvan committed Dec 8, 2023
1 parent 609637a commit 6b48d08
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
36 changes: 27 additions & 9 deletions docs/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,47 @@ with MemoryFile() as mem_dst:
client.upload_fileobj(mem_dst, "my-bucket", "my-key")
```

3. Progress to TextIO
3. Output Progress to Alternative Text Buffer

Use Case: You may want to run your translation tasks in the background and keep
track of progress. To do so you can utilize an alternative text buffer and another
thread. By outputting the progress to a seperate text buffer you can then track
the translation progress without blocking the program.
```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",
)
config = {
"GDAL_NUM_THREADS": "ALL_CPUS",
"GDAL_TIFF_INTERNAL_MASK": True,
"GDAL_TIFF_OVR_BLOCKSIZE": "128",
}


with open("logfile.txt", "w+") as buffer:

# Progress output buffer must be interactive
buffer.isatty = lambda: True

with open("logfile.txt", "w+") as example:
example.isatty = lambda: True # Enable Interactive File like terminal
cog_translate(
"example-input.tif",
"example-output.tif",
cog_profiles.get("deflate"),
config=config,
in_memory=False,
nodata=0,
quiet=example,
quiet=False,
progress_out=buffer,
)
```

Below is a snippet of code that allows you to grab the percentage complete a
translation is using the text buffer.

```python
import re

def getPercentage(buffer:str) -> float:
return int(re.findall("\d*%", buffer)[-1].replace("%", "")) / 100
```

17 changes: 9 additions & 8 deletions rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def cog_translate( # noqa: C901
allow_intermediate_compression: bool = False,
forward_band_tags: bool = False,
forward_ns_tags: bool = False,
quiet: Union[bool, TextIO] = False,
quiet: bool = False,
progress_out: Optional[TextIO] = None,
temporary_compression: str = "DEFLATE",
colormap: Optional[Dict] = None,
additional_cog_metadata: Optional[Dict] = None,
Expand Down Expand Up @@ -153,8 +154,10 @@ def cog_translate( # noqa: C901
Ref: https://github.com/cogeotiff/rio-cogeo/issues/19
forward_ns_tags: bool, optional
Forward namespaces tags to output dataset.
quiet: bool, TextIO, optional (default: False)
Mask processing steps. Define the output buffer for the progress bar.
quiet: bool, optional (default: False)
Mask processing steps.
progress_out: TextIO, optional
Output progress steps to alternative text buffer. Quiet must be False.
temporary_compression: str, optional
Compression used for the intermediate file, default is deflate.
colormap: dict, optional
Expand Down Expand Up @@ -301,11 +304,9 @@ def cog_translate( # noqa: C901
if not quiet:
click.echo("Reading input: {}".format(source), err=True)

fout = sys.stderr
if quiet is True:
fout = ctx.enter_context(open(os.devnull, "w"))
elif quiet is not False:
fout = quiet
fout = ctx.enter_context(open(os.devnull, "w")) if quiet else sys.stderr
if quiet is False and progress_out:
fout = progress_out

with click.progressbar(wind, file=fout, show_percent=True) as windows: # type: ignore
for _, w in windows:
Expand Down

0 comments on commit 6b48d08

Please sign in to comment.