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

optimize_rasters: skip_existing #224

Merged
merged 9 commits into from
Jul 12, 2021
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
15 changes: 11 additions & 4 deletions terracotta/scripts/optimize_rasters.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def _named_tempfile(basedir: Union[str, Path]) -> Iterator[str]:
type=PathlibPath(file_okay=False, writable=True),
help='Output folder for cloud-optimized rasters. Subdirectories will be flattened.'
)
@click.option(
'--skip-existing', is_flag=True, default=False, help='Skip existing files'
)
@click.option(
'--overwrite', is_flag=True, default=False, help='Force overwrite of existing files'
)
Expand Down Expand Up @@ -145,6 +148,7 @@ def _named_tempfile(basedir: Union[str, Path]) -> Iterator[str]:
def optimize_rasters(raster_files: Sequence[Sequence[Path]],
output_folder: Path,
overwrite: bool = False,
skip_existing: bool = False,
resampling_method: str = 'average',
reproject: bool = False,
in_memory: bool = None,
Expand Down Expand Up @@ -214,10 +218,13 @@ def optimize_rasters(raster_files: Sequence[Sequence[Path]],

output_file = output_folder / input_file.with_suffix('.tif').name

if not overwrite and output_file.is_file():
raise click.BadParameter(
f'Output file {output_file!s} exists (use --overwrite to ignore)'
)
if output_file.is_file():
if skip_existing:
continue
if not overwrite:
raise click.BadParameter(
f'Output file {output_file!s} exists (use --overwrite or --skip-existing)'
)

with contextlib.ExitStack() as es, warnings.catch_warnings():
warnings.filterwarnings('ignore', message='invalid value encountered.*')
Expand Down
32 changes: 32 additions & 0 deletions tests/scripts/test_optimize_rasters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import warnings
import traceback

Expand Down Expand Up @@ -149,3 +150,34 @@ def test_optimize_rasters_multiband(tmpdir, unoptimized_raster_file):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'invalid value encountered.*')
np.testing.assert_array_equal(src1.read(), src2.read())


@pytest.mark.parametrize('extra_flag', ['skip-existing', 'overwrite', None])
def test_reoptimize(tmpdir, unoptimized_raster_file, extra_flag):
from terracotta.scripts import cli

infile = str(unoptimized_raster_file.dirpath('*.tif'))
outfile = tmpdir / 'out.tif'

# first time
runner = CliRunner()
args = ['optimize-rasters', infile, '-o', str(outfile)]
result = runner.invoke(cli.cli, args)
assert result.exit_code == 0
ctime = os.stat(outfile).st_ctime

# second time
args = ['optimize-rasters', infile, '-o', str(outfile)]
if extra_flag:
args.append(f'--{extra_flag}')

result = runner.invoke(cli.cli, args)

if extra_flag == 'skip-existing':
assert result.exit_code == 0
assert os.stat(outfile).st_ctime == ctime
elif extra_flag == 'overwrite':
assert result.exit_code == 0
assert os.stat(outfile).st_ctime != ctime
else:
assert result.exit_code == 2