-
Notifications
You must be signed in to change notification settings - Fork 0
Remove inline ArcPy compression and add GDAL backfill tool #9
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
Closed
erin-glen
wants to merge
3
commits into
preprocessing/disturbances
from
codex/remove-compression-settings-from-scripts
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #!/usr/bin/env python3 | ||
| """Retroactively compress disturbance outputs using GDAL.""" | ||
|
|
||
| import argparse | ||
| import logging | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| from typing import Iterable, Sequence, List, Optional | ||
|
|
||
| import disturbance_config as cfg | ||
|
|
||
|
|
||
| DEFAULT_CREATION_OPTS = ("COMPRESS=LZW", "TILED=YES") | ||
|
|
||
|
|
||
| def _existing_dirs(dirs: Iterable[str]) -> List[str]: | ||
| out: List[str] = [] | ||
| for d in dirs: | ||
| if os.path.isdir(d): | ||
| out.append(d) | ||
| else: | ||
| logging.warning("Skipping missing directory: %s", d) | ||
| return out | ||
|
|
||
|
|
||
| def _default_dirs() -> List[str]: | ||
| return [ | ||
| cfg.NLCD_FINAL_DIR, | ||
| cfg.NLCD_FINAL_HARVEST_ONLY_DIR, | ||
| cfg.NLCD_FINAL_INSECT_DIR, | ||
| cfg.NLCD_FINAL_FIRE_DIR, | ||
| cfg.NLCD_TCC_CHANGE_DIR, | ||
| cfg.NLCD_TCC_PCT_CHANGE_DIR, | ||
| cfg.NLCD_HARVEST_SEVERITY_DIR, | ||
| cfg.NLCD_HARVEST_PCT_SEV_DIR, | ||
| ] | ||
|
|
||
|
|
||
| def _find_tifs(directories: Sequence[str]) -> List[str]: | ||
| rasters: List[str] = [] | ||
| for d in directories: | ||
| for root, _, files in os.walk(d): | ||
| for name in files: | ||
| if name.lower().endswith(".tif"): | ||
| rasters.append(os.path.join(root, name)) | ||
| return sorted(rasters) | ||
|
|
||
|
|
||
| def _compress_raster(path: str, *, overwrite: bool, dry_run: bool, creation_opts: Sequence[str]) -> bool: | ||
| target_path = path if overwrite else f"{os.path.splitext(path)[0]}_lzw.tif" | ||
|
|
||
| if not overwrite and os.path.exists(target_path): | ||
| logging.info("Skipping (already exists): %s", target_path) | ||
| return True | ||
|
|
||
| logging.info("Compressing: %s", path) | ||
| if dry_run: | ||
| return True | ||
|
|
||
| if shutil.which("gdal_translate") is None: | ||
| raise RuntimeError("gdal_translate not found on PATH; install GDAL or adjust PATH.") | ||
|
|
||
| directory = os.path.dirname(path) | ||
| base = os.path.basename(path) | ||
| with tempfile.NamedTemporaryFile(dir=directory, prefix=f".{base}", suffix=".tmp.tif", delete=False) as tmp: | ||
| tmp_path = tmp.name | ||
|
|
||
| cmd = ["gdal_translate", path, tmp_path] | ||
| for opt in creation_opts: | ||
| cmd.extend(["-co", opt]) | ||
|
|
||
| logging.debug("Running: %s", " ".join(cmd)) | ||
| result = subprocess.run(cmd, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | ||
| if result.returncode != 0: | ||
| logging.error("gdal_translate failed for %s\nSTDOUT: %s\nSTDERR: %s", path, result.stdout, result.stderr) | ||
| os.unlink(tmp_path) | ||
| return False | ||
|
|
||
| if overwrite: | ||
| os.replace(tmp_path, path) | ||
| else: | ||
| os.replace(tmp_path, target_path) | ||
| logging.info("Compressed copy written to %s", target_path) | ||
| return True | ||
|
|
||
|
|
||
| def parse_args(argv: Sequence[str]) -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser(description="Compress disturbance rasters using GDAL.") | ||
| parser.add_argument("dirs", nargs="*", help="Directories to search for GeoTIFF files. Defaults to standard output folders.") | ||
| parser.add_argument("--overwrite", action="store_true", help="Replace originals in-place (default writes *_lzw.tif copies).") | ||
| parser.add_argument("--dry-run", action="store_true", help="List rasters that would be compressed without running GDAL.") | ||
| parser.add_argument( | ||
| "--creation-option", | ||
| "-co", | ||
| action="append", | ||
| dest="creation_options", | ||
| help="Creation options to pass to gdal_translate (may be repeated). Default: COMPRESS=LZW, TILED=YES.", | ||
| ) | ||
| parser.add_argument("--log-level", default="INFO", help="Logging level (default: INFO)") | ||
| return parser.parse_args(argv) | ||
|
|
||
|
|
||
| def main(argv: Optional[Sequence[str]] = None) -> int: | ||
| args = parse_args(argv or sys.argv[1:]) | ||
| logging.basicConfig(level=getattr(logging, args.log_level.upper(), logging.INFO), | ||
| format="%(asctime)s [%(levelname)s] %(message)s") | ||
|
|
||
| dirs = args.dirs or _default_dirs() | ||
| dirs = _existing_dirs(dirs) | ||
| if not dirs: | ||
| logging.error("No valid directories to scan.") | ||
| return 1 | ||
|
|
||
| rasters = _find_tifs(dirs) | ||
| if not rasters: | ||
| logging.warning("No GeoTIFF files found in: %s", ", ".join(dirs)) | ||
| return 0 | ||
|
|
||
| creation_opts = args.creation_options or list(DEFAULT_CREATION_OPTS) | ||
|
|
||
| success = True | ||
| for path in rasters: | ||
| ok = _compress_raster(path, overwrite=args.overwrite, dry_run=args.dry_run, creation_opts=creation_opts) | ||
| success &= ok | ||
|
|
||
| if args.dry_run: | ||
| logging.info("Dry run complete. %d rasters would be processed.", len(rasters)) | ||
| elif success: | ||
| logging.info("Compression complete. Processed %d rasters.", len(rasters)) | ||
| else: | ||
| logging.error("Compression completed with errors.") | ||
| return 2 | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When compressing with
--overwrite, the raster is rewritten viaNamedTemporaryFileandos.replace. Temporary files are created with mode0o600, andos.replacebrings those restrictive permissions along. Any raster that originally had broader read permissions (e.g., shared data at0644) will become owner‑only after compression, breaking downstream access for other users or services. Consider copying the original file’s mode to the temporary output before replacing, or usingshutil.copymodeso that compression doesn’t silently revoke access.Useful? React with 👍 / 👎.