From ab283fec0ae8b276838bacfa7967bc884d083715 Mon Sep 17 00:00:00 2001 From: Mattia Amadio <44863827+matamadio@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:41:55 +0100 Subject: [PATCH] Add utility merge tiles --- docs/_toc.yml | 3 ++ docs/utility.md | 11 +++++ utility/merge_tiles/Merge_tiles.ipynb | 62 +++++++++++++++++++++++++++ utility/merge_tiles/merge_utils.py | 13 ++++++ 4 files changed, 89 insertions(+) create mode 100644 docs/utility.md create mode 100644 utility/merge_tiles/Merge_tiles.ipynb create mode 100644 utility/merge_tiles/merge_utils.py diff --git a/docs/_toc.yml b/docs/_toc.yml index bd1dff7b..aea9f022 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -34,6 +34,9 @@ parts: chapters: - file: docs/tool-setup - file: docs/run-baseline + - file: docs/utility + sections: + - file: utility/merge_tiles/Merge_tiles.ipynb - caption: External resources chapters: - file: docs/external-tools diff --git a/docs/utility.md b/docs/utility.md new file mode 100644 index 00000000..7219991c --- /dev/null +++ b/docs/utility.md @@ -0,0 +1,11 @@ +# Utilities + +Additional tools to help collect, view and process risk data. + +## Merge tiles + +Large raster datasets often comes as a collection of regular tiles. +In order to run the raster analysis offered in our tools, they often need to be merged into one file. +To do that, a simple python script is provided as jupyter notebook. + +[TILE MERGE](utility/merge_tiles/Merge_tiles.ipynb) \ No newline at end of file diff --git a/utility/merge_tiles/Merge_tiles.ipynb b/utility/merge_tiles/Merge_tiles.ipynb new file mode 100644 index 00000000..c72eb902 --- /dev/null +++ b/utility/merge_tiles/Merge_tiles.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b8bf67f8", + "metadata": {}, + "source": [ + "# TILE MERGE SCRIPT\n", + "\n", + "The python script below looks into subfolders of specified directory and merge together all tiff files found, naming the resulting tiff as the folder from which they are generated.\n", + "\n", + "- The function for merging and exporting is found in merge_utils.py\n", + "- GDAL library must be installed in your python environment\n", + "- Output is exported in the same format of the original tiles\n", + "- The scripts makes use of parallel processing for quicker results" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "e3904d3e", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import concurrent.futures\n", + "from merge_utils import merge_tifs\n", + "\n", + "# Specify the directory to search for .tif files\n", + "directory = 'C:/YourWorkDirectory/Fathom3/PL/2020/'\n", + "\n", + "# Iterate through each subdirectory\n", + "subdirs = [os.path.join(directory, subdir) for subdir in os.listdir(directory) if os.path.isdir(os.path.join(directory, subdir))]\n", + "\n", + "# Process subdirectories in parallel\n", + "with concurrent.futures.ProcessPoolExecutor() as executor:\n", + " executor.map(merge_tifs, subdirs)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/utility/merge_tiles/merge_utils.py b/utility/merge_tiles/merge_utils.py new file mode 100644 index 00000000..8d8f3438 --- /dev/null +++ b/utility/merge_tiles/merge_utils.py @@ -0,0 +1,13 @@ +import os +from osgeo import gdal + +def merge_tifs(subdir_path): + # Get a list of all .tif files in the subdirectory + tif_files = [os.path.join(subdir_path, file) for file in os.listdir(subdir_path) if file.endswith('.tif')] + + # If there are .tif files in the subdirectory, merge them + if tif_files: + output_file = os.path.join(subdir_path, f"{os.path.basename(subdir_path)}.tif") + vrt = gdal.BuildVRT('', tif_files) + gdal.Translate(output_file, vrt, options='-co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9') + vrt = None