EOX GitLab Instance

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

[preprocessor] override projection from mapping against metadata.xml entry...

[preprocessor] override projection from mapping against metadata.xml entry when no .dim, .prj or .tif files
parent fa7733c5
......@@ -86,9 +86,26 @@ preprocessing:
data_file_globs:
- "*_[0-9]_*.tif"
- "*.vrt"
- "*metadata.dim"
- "*imagery.dim"
- "*imagery.tif"
additional_file_globs:
- "*rpc.txt"
- "*imagery.bil"
- "*imagery.blw"
- "*imagery.hdr"
- "*imagery.prj"
- "*metadata.xml"
custom_preprocessor:
path: preprocessor.preprocessor_fix_core_dimap_image_ref.rename_reference_dimap
kwargs:
search: IMAGERY.TIF
replace: imagery.bil
georeference:
data_file_globs:
- "*imagery.dim"
- "*_[0-9]_*.tif"
- "*imagery.tif"
geotransforms:
- type: rpc
rpc_file_template: "{fileroot}_rpc.txt"
......
......@@ -5,20 +5,24 @@ import shutil
from lxml import etree
import textwrap
from .util import get_all_data_files
from .util import get_all_data_files, gdal, replace_ext
logger = logging.getLogger(__name__)
PROJECTIONS = {
"EUROPEAN": "EPSG:3035",
"CZ": "EPSG:32633",
"SK": "EPSG:32633",
"ES_29": "EPSG:32629",
"ES_30": "EPSG:32630",
}
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)
target_filename = join(target_dir, output_file_name)
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:
filenames = get_all_data_files(source_dir, preprocess_config, ["*metadata.dim"])
# working in source_dir to be picked up later by move operation
target_filename = join(source_dir, output_file_name)
if len(filenames) != 0:
try:
# read file
with open(filenames[0]) as f:
......@@ -31,16 +35,20 @@ def rename_reference_dimap(source_dir: os.PathLike, target_dir: os.PathLike, pre
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)
try:
# check if .hdr file is created
# if not create it to source_dir to be picked up later by move operation
attempt_to_create_hdr(source_dir, source_dir, preprocess_config)
except Exception as e:
logger.warn(e)
try:
attempt_to_set_projection(source_dir, source_dir, preprocess_config)
except Exception as e:
logger.warn(e)
# 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)
def attempt_to_create_hdr(source_dir, target_dir, preprocess_config, output: str = "imagery.hdr"):
......@@ -81,3 +89,46 @@ def attempt_to_create_hdr(source_dir, target_dir, preprocess_config, output: str
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")
def attempt_to_set_projection(source_dir, target_dir, preprocess_config):
# check if there are no tifs and only bil + metadata.xml without dimap file
# search for GeoInformation><PROJECTION>European</PROJECTION></GeoInformation> to set crs for image from mapping by force
needed_files = get_all_data_files(source_dir, preprocess_config, ['*.tif', '*.dim', '*.prj'])
if len(needed_files) > 0:
# expected files are there, return
return
# this means we have no dimap and a bil with possible georeferencing in metadata.xml (which we need to extract out)
md_files = get_all_data_files(source_dir, preprocess_config, ['*metadata.xml'])
if len(md_files) >= 1:
projection = None
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 == 'GeoInformation':
for child2 in child:
if child2.tag == 'PROJECTION':
projection = child2.text.upper()
break
break
if projection is not None:
logger.info('Found projection %s' % projection)
# get epsg code of projection by name
epsg_code = PROJECTIONS.get(projection, None)
input_filename = get_all_data_files(source_dir, preprocess_config, ['*.bil'])[0]
target_filename = replace_ext(join(target_dir, basename(input_filename)), '.tif')
# create a tif from bil and set its projection
ds = gdal.Open(input_filename)
ds = gdal.GetDriverByName("GTiff").CreateCopy(target_filename, ds, strict=0)
if not epsg_code:
logger.warning("Projection %s not in the mapping of projections" % projection)
# use GSC bbox as a fallback if everything failed, risking bad orientation
pass
else:
ds.SetProjection(epsg_code)
del ds
del preprocess_config['stack_bands']
else:
logger.warn("metadata.xml not found. No way to fix .bil without geotransform")
\ No newline at end of file
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