Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
GDAL Merge, clip and translate #satellite #geo #qgis
# why the previous line? I wondered, too; here lies the answer:
# Before you proceed: Make sure you have a
# directory called 'input' containing your tifs.
# You also need a directory named 'aoi' for your areas-of-interest.
# Also important and not optional:
# The .tif files have to have the or a date at the front in the
# form of YYYY-MM-DD or the script won't find them.
# config your script variables
# remove tifs after creating the jpgs?
# do you want to directly resize the images to a certain size?
# the names of your aoi GeoJSONs
# if resize=true, set the size of one side per aoi here:
# aoi_1_width=\480
# aoi_2_height=\900
# you can try and go down to 80 or 75
# if you're looking to reduce file size:
echo "This may take a few minutes, depending on the amount of tifs. For 8 tifs with each at ~60MB it takes ~1 minute."
# Step Zero: Check and create directories
echo "Checking and building output directory:"
if test ! -d output/jpg/ ;
then mkdir output ;
echo "Directory 'output' does not exist; creating..." ;
if test ! -d output/jpg/ ;
then mkdir output/jpg/ ;
echo "Directory 'output/jpg/' does not exist; creating..." ;
if test ! -d output/tif/ ;
then mkdir output/tif/ ;
echo "Directory 'output/tif/' does not exist; creating..." ;
for date in $(ls input | grep -oP "\d{4}-\d{2}-\d{2}" | uniq)
echo "Merging tifs for $date..."
# Step One: Merge all tiffs in the folder -n 0 -a_nodata 0 -ot Byte -of GTiff -o output/tif/$date-merged.tif input/$date*.tif
# Step Two: Cropping to cutline; converting/translating to jpg; resizing jpg
# 1st AOI
gdalwarp -of GTiff -cutline aoi/$aoi_1.geojson -crop_to_cutline -dstnodata 0.0 -t_srs $target_srs -overwrite output/tif/$date-merged.tif output/tif/$date-$aoi_1.tif
gdal_translate -a_srs $target_srs -a_nodata 0.0 -ot Byte -of JPEG output/tif/$date-$aoi_1.tif output/jpg/$date-$aoi_1.jpg
if $resize ; then
convert -quality $jpg_quality -resize $aoi_1_width\x$aoi_1_height output/jpg/$date-$aoi_1.jpg output/jpg/$date-$aoi_1.jpg ;
# 2nd AOI
gdalwarp -of GTiff -cutline aoi/$aoi_2.geojson -crop_to_cutline -dstnodata 0.0 -t_srs $target_srs -overwrite output/tif/$date-merged.tif output/tif/$date-$aoi_2.tif
gdal_translate -a_srs $target_srs -a_nodata 0.0 -ot Byte -of JPEG output/tif/$date-$aoi_2.tif output/jpg/$date-$aoi_2.jpg
if $resize ; then
convert -quality $jpg_quality -resize $aoi_2_width\x$aoi_2_height output/jpg/$date-$aoi_2.jpg output/jpg/$date-$aoi_2.jpg ;
# removes the big merged tif to free space
if $clean_up ;
then rm output/tif/$date-merged.tif ;
# Step Three: Cleaning up the working directory
if $clean_up ;
then echo "Variable 'clean_up' is set to 'true'; removing tifs..." ;
rm output/tif/*-$aoi_1.tif ;
rm output/tif/*-$aoi_2.tif ;
# removes unnecessary files
echo "Removing unnecessary XML files..."
rm output/jpg/*.xml
# if you didn't comment out the output-tif-removal above,
# the following will remove the empty folder
if test ! -d output/tif/*.tif ;
then rm -r output/tif/ ;
echo "The tif directory is empty; removing it for tidyness" ;
echo "The tif directory is not empty; keeping it" ;
echo "Yay, finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.