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
from os.path import join, splitext, basename
from os.path import join, splitext, basename, dirname
from glob import glob
from typing import Set
from typing import Dict
from osgeo import gdal
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):
for filename in glob(join(source_dir, data_file_glob)):
gdal.UseExceptions()
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(
filename,
join(target_dir, basename(replace_ext(filename, '.tif'))),
target_dir,
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)
sub_datasets = []
for locator, _ in ds.GetSubDatasets():
_, _, sd_type = locator.split(':')
if subdataset_types is None or sd_type in subdataset_types:
sub_datasets.append(locator)
# combine as VRT
out_vrt_name = join(source_dir, replace_ext(filename, '.vrt'))
gdal.BuildVRT(out_vrt_name, sub_datasets, # TODO: options
)
# TODO: maybe just translate here?
gdal.Warp(
target_filename,
out_vrt_name
)
sub_datasets.append((locator, subdataset_types[sd_type]))
if not sub_datasets:
raise Exception('No subdatasets were matched by the provided types')
for locator, suffix in sub_datasets:
target_filename = join(target_dir, basename(replace_ext(source_filename, '%s.tif' % suffix)))
gdal.Translate(target_filename, locator, format='GTiff')
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