From cdcab96370e128a17b1a7ad883642eb2779cebb3 Mon Sep 17 00:00:00 2001 From: heyarne Date: Fri, 24 Jul 2020 11:28:36 +0000 Subject: [PATCH] Make band_paths work with unzipped folders --- True-Color Image/sentinel_helpers.py | 40 +++++++++++++--------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/True-Color Image/sentinel_helpers.py b/True-Color Image/sentinel_helpers.py index 6d483f2..ab0362c 100644 --- a/True-Color Image/sentinel_helpers.py +++ b/True-Color Image/sentinel_helpers.py @@ -1,35 +1,31 @@ import zipfile - +import glob +from Pathlib import Path def band_paths(p, bands, resolution=None): ''' - Given a zip file at `p`, returns the paths inside p to the raster files containing - + Given a zip file or folder at `p`, returns the paths inside p to the raster files containing information for the given bands. Because some bands are available in more than one - resolution, this can be filtered by prodiding a third parameter (e.g. resolution='10m'). - - - - The returned paths are formatted in the zip scheme as per Apache Commons VFS and can - - thus be directly opened by rasterio. - + + The returned paths are formatted in the zip scheme as per Apache Commons VFS if necessary + and can be directly opened by rasterio. ''' + if p.endswith('.zip'): + with zipfile.ZipFile(p) as f: + files = f.namelist() + rasters = [f for f in files if f.endswith('.jp2')] + else: + rasters = glob.glob(Path(p) / '**/*.jp2' + + rasters = [r for r in rasters for b in bands if b in r] + if resolution: + rasters = [r for r in rasters if resolution in r] - with zipfile.ZipFile(p) as f: - - files = f.namelist() - - rasters = [f for f in files if f.endswith('.jp2')] - rasters = [r for r in rasters for b in bands if b in r] - if resolution: - rasters = [r for r in rasters if resolution in r] - - rasters = ['zip+file://' + p + '!/' + r for r in rasters] - return rasters + rasters = ['zip+file://' + p + '!/' + r for r in rasters] + return rasters def rgb_paths(zip_file, resolution='10m'):