EOX GitLab Instance

Skip to content
Snippets Groups Projects
Commit 37eb8d37 authored by Fabian Schindler's avatar Fabian Schindler
Browse files

Fixing subdataset step

parent 8207db8f
No related branches found
No related tags found
No related merge requests found
import os import os
from os.path import join, splitext, basename from os.path import join, splitext, basename, dirname
from glob import glob from glob import glob
from typing import Set from typing import Dict
from osgeo import gdal from osgeo import gdal
from ..util import replace_ext from ..util import replace_ext
def extract_subdataset_step(source_dir: os.PathLike, target_dir: os.PathLike, data_file_glob: str, subdataset_types: Set[str]=None): gdal.UseExceptions()
for filename in glob(join(source_dir, data_file_glob)):
def extract_subdataset_step(source_dir: os.PathLike, target_dir: os.PathLike, data_file_glob: str, subdataset_types: Dict[str, str]=None):
datafiles = glob(join(source_dir, data_file_glob))
if not datafiles:
raise Exception('No datafiles were matched by the provided glob')
for filename in datafiles:
extract_subdatasets( extract_subdatasets(
filename, filename,
join(target_dir, basename(replace_ext(filename, '.tif'))), target_dir,
subdataset_types subdataset_types
) )
def extract_subdatasets(source_filename: os.PathLike, target_filename: os.PathLike, subdataset_types: Set[str]=None): def extract_subdatasets(source_filename: os.PathLike, target_dir: os.PathLike, subdataset_types: Dict[str, str]=None):
ds = gdal.Open(source_filename) ds = gdal.Open(source_filename)
sub_datasets = [] sub_datasets = []
for locator, _ in ds.GetSubDatasets(): for locator, _ in ds.GetSubDatasets():
_, _, sd_type = locator.split(':') _, _, sd_type = locator.split(':')
if subdataset_types is None or sd_type in subdataset_types: if subdataset_types is None or sd_type in subdataset_types:
sub_datasets.append(locator) sub_datasets.append((locator, subdataset_types[sd_type]))
# combine as VRT if not sub_datasets:
out_vrt_name = join(source_dir, replace_ext(filename, '.vrt')) raise Exception('No subdatasets were matched by the provided types')
gdal.BuildVRT(out_vrt_name, sub_datasets, # TODO: options
) for locator, suffix in sub_datasets:
target_filename = join(target_dir, basename(replace_ext(source_filename, '%s.tif' % suffix)))
# TODO: maybe just translate here? gdal.Translate(target_filename, locator, format='GTiff')
gdal.Warp(
target_filename,
out_vrt_name
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment