#!/bin/bash set -e DIR=$1 SHP=$2 TMP=$(mktemp -d) export DIR downsample() { gdalwarp -q -r max -tap -tr 0.0001 0.0001 -co NUM_THREADS=2 -of VRT -overwrite "${DIR}/${1}.tif" "${TMP}/${1}.vrt" gdal_calc.py --quiet --format gtiff --overwrite --type=Byte --NoDataValue 0 --co COMPRESS=CCITTFAX4 --co NBITS=1 -A "${TMP}/${1}.vrt" --outfile "${TMP}/${1}.tif" --calc "A == 1" } export -f downsample basename -s.tif ${DIR}/*.tif | parallel -j 15 downsample gdalbuildvrt "$TMP/overview.vrt" $TMP/*.tif gdal_polygonize.py -f 'ESRI Shapefile' $TMP/overview.vrt "$SHP" rm -r $TMP