EOX GitLab Instance

Commit fa7733c5 authored by Lubomir Doležal's avatar Lubomir Doležal
Browse files

reconstruct hdr file from metadata.xml if missing

parent 766d2274
......@@ -65,6 +65,7 @@ preprocessing:
- "*imagery.blw"
- "*imagery.hdr"
- "*imagery.prj"
- "*metadata.xml"
custom_preprocessor:
path: preprocessor.preprocessor_fix_core_dimap_image_ref.rename_reference_dimap
kwargs:
......
......@@ -2,26 +2,82 @@ import os
import logging
from os.path import join, basename
import shutil
from lxml import etree
import textwrap
from .util import get_all_data_files
logger = logging.getLogger(__name__)
def rename_reference_dimap(source_dir: os.PathLike, target_dir: os.PathLike, preprocess_config: dict, search: str, replace: str, output_file_name: str="imagery.dim"):
def rename_reference_dimap(source_dir: os.PathLike, target_dir: os.PathLike, preprocess_config: dict, search: str, replace: str, output_file_name: str = "imagery.dim"):
filenames = get_all_data_files(source_dir, preprocess_config)
if len(filenames) == 0:
raise Exception('No datafiles were matched by the provided glob')
target_filename = join(target_dir, output_file_name)
# read file
with open(filenames[0]) as f:
# edit content
dimap = f.read()
replaced = dimap.replace(search, replace)
# write resulting file
with open(target_filename, 'w') as w:
w.write(replaced)
os.unlink(filenames[0])
# copy everything else to target_dir
for input_filename in get_all_data_files(source_dir, preprocess_config, '**'):
target_filename = join(target_dir, basename(input_filename))
shutil.move(input_filename, target_filename)
if len(filenames) == 0:
# copy everything else to target_dir
for input_filename in get_all_data_files(source_dir, preprocess_config, ['*metadata.dim']):
target_filename = join(target_dir, basename(input_filename))
shutil.move(input_filename, target_filename)
else:
try:
# read file
with open(filenames[0]) as f:
# edit content
dimap = f.read()
replaced = dimap.replace(search, replace)
# write resulting file
with open(target_filename, 'w') as w:
w.write(replaced)
os.unlink(filenames[0])
except Exception as e:
logger.warn(e)
try:
# check if .hdr file is created
attempt_to_create_hdr(source_dir, target_dir, preprocess_config)
# copy everything else to target_dir
except Exception as e:
logger.warn(e)
for input_filename in get_all_data_files(source_dir, preprocess_config, '**'):
target_filename = join(target_dir, basename(input_filename))
shutil.move(input_filename, target_filename)
def attempt_to_create_hdr(source_dir, target_dir, preprocess_config, output: str = "imagery.hdr"):
hdr = get_all_data_files(source_dir, preprocess_config, ['*hdr'])
if len(hdr) == 0:
# attempt to recreate it from metadata.xml in source_dir
md_files = get_all_data_files(source_dir, preprocess_config, ['*metadata.xml'])
if len(md_files) >= 1:
hdr_data = {}
with open(md_files[0]) as g:
logger.debug('Parsing metadata file %s' % md_files[0])
tree = etree.parse(g)
root = tree.getroot()
for child in root:
if child.tag == 'Image':
for child2 in child:
hdr_data[child2.tag] = child2.text
break
if len(hdr_data) > 0:
logger.debug('Used metadata for hdr creation %s' % hdr_data)
target_filename = join(target_dir, output)
with open(target_filename, 'w') as hdr_output:
hdr_content = textwrap.dedent("""\
BYTEORDER M
LAYOUT BIL
NROWS {NROWS}
NCOLS {NCOLS}
NBANDS 4
NBITS 8
SKIPBYTES 0"""
.format(
NROWS=hdr_data["ROWS"],
NCOLS=hdr_data["COLUMNS"],
))
hdr_output.write(hdr_content)
logger.debug('Successfully written .hdr metadata file to %s' % target_filename)
else:
logger.warn('Failed extraction of IMAGE xml metadata for .hdr creation.')
else:
logger.warn(".Hdr file not present but also metadata.xml not found. No way to fix .bil")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment