From 27367e89c60f53e902b6ed0a45322960910edf21 Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Wed, 20 Sep 2023 17:19:27 -0500 Subject: [PATCH 1/3] Remove gdalinfo reference --- macrostrat/raster_cli/process.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/macrostrat/raster_cli/process.py b/macrostrat/raster_cli/process.py index 7fd611f..e9620a6 100644 --- a/macrostrat/raster_cli/process.py +++ b/macrostrat/raster_cli/process.py @@ -116,7 +116,9 @@ def process_image( except RasterioIOError: pass - run(["gdalinfo", src_path]) + # This is not always available without a full GDAL install + # RasterIO tools should be used instead. + #run(["gdalinfo", src_path]) if should_copy: dst_path = src_path From 27f4dfde7c4618614479d391a642e8402b5e07ed Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Wed, 20 Sep 2023 17:22:08 -0500 Subject: [PATCH 2/3] Support opening local files --- macrostrat/raster_cli/process.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/macrostrat/raster_cli/process.py b/macrostrat/raster_cli/process.py index e9620a6..d326e67 100644 --- a/macrostrat/raster_cli/process.py +++ b/macrostrat/raster_cli/process.py @@ -98,6 +98,8 @@ def process_image( wget.download(url, src_path) elif url_info.scheme == "s3": _s3_download(url, src_path) + elif filepath.exists(): + src_path = url else: raise Exception(f"Unsuported scheme {url_info.scheme}") From e4a67ec0c659b9a3100e7e2cad16577544adea83 Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Wed, 20 Sep 2023 17:31:51 -0500 Subject: [PATCH 3/3] Add support for cases where raster is not the only file in a Zip --- macrostrat/raster_cli/process.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/macrostrat/raster_cli/process.py b/macrostrat/raster_cli/process.py index d326e67..9b2c4c7 100644 --- a/macrostrat/raster_cli/process.py +++ b/macrostrat/raster_cli/process.py @@ -8,8 +8,9 @@ from rio_cogeo.cogeo import cog_translate, cog_validate from rio_cogeo.profiles import cog_profiles +import rasterio from rasterio.errors import RasterioIOError -from zipfile import is_zipfile +from zipfile import is_zipfile, ZipFile from os import path from subprocess import run @@ -104,17 +105,31 @@ def process_image( raise Exception(f"Unsuported scheme {url_info.scheme}") should_copy = copy_valid_cog + rio_openable = None # Use the appropriate GDAL VSI driver for zip files if url.endswith(".zip") or is_zipfile(src_path): - src_path = "/vsizip/" + path.abspath(src_path) + zip_path = "/vsizip/" + path.abspath(src_path) + # Check if RasterIO can read the zip file directly + try: + rasterio.open(zip_path) + rio_openable = zip_path + except RasterioIOError: + # If not, find the largest file in the zip and use that + with ZipFile(src_path) as zf: + largest = sorted(zf.filelist, key=lambda x: x.file_size)[-1] + rio_openable = f"/vsizip/{path.abspath(src_path)}/{largest.filename}" + should_copy = False elif url.endswith(".tar.gz"): - src_path = "/vsitar/" + path.abspath(src_path) + rio_openable = "/vsitar/" + path.abspath(src_path) should_copy = False + if rio_openable is None: + rio_openable = src_path + if should_copy: try: - should_copy = cog_validate(src_path) + should_copy = cog_validate(rio_openable) except RasterioIOError: pass @@ -127,7 +142,7 @@ def process_image( else: print("Converting to COG") _translate( - src_path, + rio_openable, dst_path, profile=profile, profile_options=profile_options,