Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GDAL Merge, clip and translate #satellite #geo #qgis
#!/bin/bash
# why the previous line? I wondered, too; here lies the answer: https://stackoverflow.com/questions/8967902/why-do-you-need-to-put-bin-bash-at-the-beginning-of-a-script-file#8967916
# 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?
clean_up=true
# do you want to directly resize the images to a certain size?
resize=true
target_srs=EPSG:3857
# the names of your aoi GeoJSONs
aoi_1=mobile
aoi_2=desktop
# if resize=true, set the size of one side per aoi here:
aoi_1_height=640
# aoi_1_width=\480
# aoi_2_height=\900
aoi_2_width=1440
# you can try and go down to 80 or 75
# if you're looking to reduce file size:
jpg_quality=92
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..." ;
fi
if test ! -d output/jpg/ ;
then mkdir output/jpg/ ;
echo "Directory 'output/jpg/' does not exist; creating..." ;
fi
if test ! -d output/tif/ ;
then mkdir output/tif/ ;
echo "Directory 'output/tif/' does not exist; creating..." ;
fi
for date in $(ls input | grep -oP "\d{4}-\d{2}-\d{2}" | uniq)
do
echo "Merging tifs for $date..."
# Step One: Merge all tiffs in the folder
gdal_merge.py -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 ;
fi
# 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 ;
fi
# removes the big merged tif to free space
if $clean_up ;
then rm output/tif/$date-merged.tif ;
fi
done
# 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 ;
fi
# 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" ;
else
echo "The tif directory is not empty; keeping it" ;
fi
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.