From b4aa3a9be0b4a99cbb422759a1e87c883f3a37eb Mon Sep 17 00:00:00 2001 From: Adrien-ANTON-LUDWIG Date: Mon, 17 Jul 2023 16:36:56 +0000 Subject: [PATCH] Avoid using rasterio "r+" open mode (ugly patch) When using rasterio "r+" open mode, the file is well updated while opened but completely wrond once saved. --- opendm/dem/commands.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/opendm/dem/commands.py b/opendm/dem/commands.py index e09cf915..cb253c72 100755 --- a/opendm/dem/commands.py +++ b/opendm/dem/commands.py @@ -319,11 +319,9 @@ def median_smoothing(geotiff_path, output_path, smoothing_iterations=1, window_s output_dirty_in = os.path.join(folder_path, "{}.dirty_1{}".format(basename, ext)) output_dirty_out = os.path.join(folder_path, "{}.dirty_2{}".format(basename, ext)) - shutil.copyfile(geotiff_path, output_dirty_in) - log.ODM_INFO('Starting smoothing...') - with rasterio.open(output_dirty_in, "r+", num_threads=num_workers,) as img, rasterio.open(output_dirty_out, "w+", BIGTIFF="IF_SAFER", num_threads=num_workers, **img_in.profile) as imgout: + with rasterio.open(geotiff_path, num_threads=num_workers) as img, rasterio.open(output_dirty_in, "w+", BIGTIFF="IF_SAFER", num_threads=num_workers, **img.profile) as imgout, rasterio.open(output_dirty_out, "w+", BIGTIFF="IF_SAFER", num_threads=num_workers, **img.profile) as imgout2: nodata = img.nodatavals[0] dtype = img.dtypes[0] shape = img.shape @@ -347,10 +345,15 @@ def median_smoothing(geotiff_path, output_path, smoothing_iterations=1, window_s Parallel(n_jobs=num_workers, backend='threading')(delayed(window_filter_2d)(img, imgout, nodata , window, 9, filter, read_lock, write_lock) for window in windows) # Between each iteration we swap the input and output temporary files - img_in, img_out = img_out, img_in + #img_in, img_out = img_out, img_in + if (i == 0): + img = imgout + imgout = imgout2 + else: + img, imgout = imgout, img # If the number of iterations was even, we need to swap temporary files - if (smoothing_iterations % 2 == 0): + if (smoothing_iterations % 2 != 0): output_dirty_in, output_dirty_out = output_dirty_out, output_dirty_in # Cleaning temporary files