Skip to content

Commit

Permalink
Add utility merge tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
matamadio committed Feb 8, 2024
1 parent f9debf1 commit ab283fe
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions docs/utility.md
Original file line number Diff line number Diff line change
@@ -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)
62 changes: 62 additions & 0 deletions utility/merge_tiles/Merge_tiles.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions utility/merge_tiles/merge_utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ab283fe

Please sign in to comment.