-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |