EOX GitLab Instance

Skip to content
Snippets Groups Projects
Commit 461a9f1f authored by Lubomir Dolezal's avatar Lubomir Dolezal
Browse files

add fix geotransform function from old preprocessor and adapt it for jp2 driver open & close

parent 81a1d2a7
No related branches found
No related tags found
2 merge requests!49Production release 1.1.1,!46Emg prod types
......@@ -67,7 +67,7 @@ class SwiftSource(Source):
def get_container_and_path(self, path: str):
container = self.container
if container is None:
if container is None or container == '':
parts = (path[1:] if path.startswith('/') else path).split('/')
container, path = parts[0], '/'.join(parts[1:])
......
......@@ -125,7 +125,7 @@ definitions:
type:
description: The type of georeferencing to apply.
type: string
enum: [gcp, rpc, corner, world, no_op] # TODO: more
enum: [gcp, rpc, corner, world, no_op, fix_geotrans] # TODO: more
options:
description: Additional options for the georeferencing. Depends on the type of georeferencing.
type: object
......
......@@ -26,6 +26,8 @@ def georeference_step(source_dir: os.PathLike, target_dir: os.PathLike, preproce
georef_func = corner_georef
elif type_name == 'no_op':
georef_func = no_op
elif type_name == 'fix_geotrans':
georef_func = fix_geotrans
else:
raise Exception('Invalid georeference type %s' % type_name)
try:
......@@ -166,6 +168,57 @@ def no_op(input_filename: os.PathLike, target_filename: os.PathLike):
shutil.move(input_filename, target_filename)
def fix_geotrans(input_filename: os.PathLike, target_filename: os.PathLike, warp_options: dict=None):
# assumes already existing geotransform and if xres=0,yres=0, fixes it
try:
ds = gdal.Open(input_filename, gdal.GA_Update)
except RuntimeError:
logger.warn('Can not open file by GDAL %s' % (input_filename))
return
ds = correct_geo_transform(ds)
if ds.GetDriver().ShortName == 'JP2OpenJPEG':
# workaround for rewriting jp2 files and exception thrown by "USE_SRC_CODESTREAM=YES specified, but no codestream found"
# then needs .vrt configured in data_file_globs too
target_filename = replace_ext(target_filename, '.vrt'),
gdal.Warp(
target_filename,
ds,
**(warp_options or {}),
)
def correct_geo_transform(src_ds):
# input - gdal dataset
# sets new geotransform if necessary by creating control points of a raster with switched height and width
# returns - gdal dataset
ulx, xres, xskew, uly, yskew, yres = src_ds.GetGeoTransform()
logger.debug("Testing for malformed geotransform")
# test geotransform if necessary to shift
if xres == 0.0 and yres == 0.0:
logger.info("Malformed geotransform xres,yres=0 detected, correcting.")
# malformed image, compute xres and yres switched in geotransform
lrx = ulx + (src_ds.RasterXSize * xskew)
lry = uly + (src_ds.RasterYSize * yskew)
# [ulx, lrx, lry, uly] - bounds = lon_min, lon_max, lat_min, lat_max
fp = [[0, src_ds.RasterXSize, src_ds.RasterXSize, 0], [0, 0, src_ds.RasterYSize, src_ds.RasterYSize]]
tp = [[ulx, lrx, lrx, ulx], [lry, lry, uly, uly]]
pix = list(zip(fp[0], fp[1]))
coor = list(zip(tp[0], tp[1]))
# compute the gdal.GCP parameters
gcps = []
for index, txt in enumerate(pix):
gcps.append(gdal.GCP())
gcps[index].GCPPixel = pix[index][0]
gcps[index].GCPLine = src_ds.RasterYSize - int(pix[index][1])
gcps[index].GCPX = coor[index][0]
gcps[index].GCPY = coor[index][1]
# get correct geotransform from gcps
geotransform_new = gdal.GCPsToGeoTransform(gcps)
# overwrite geotransform with new
src_ds.SetGeoTransform(geotransform_new)
return src_ds
def gcps_from_borders(size: Tuple[float, float], coords: List[Tuple[float, float]], orbit_direction: str, force_north_up: bool=False):
x_size, y_size = size
# expects coordinates in dict(.*border_left.*:[lat,lon],...)
......
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