]> git.openstreetmap.org Git - chef.git/blob - cookbooks/imagery/files/default/osstvw_make_diffs
imagery: let osstvw_make_diffs make vrt too
[chef.git] / cookbooks / imagery / files / default / osstvw_make_diffs
1 #!/bin/bash
2 set -e
3 GDALCOPYPROJ="/usr/share/doc/python-gdal/examples/gdalcopyproj.py"
4
5 # Expected directory names: 2010-04, 2010-11, ... 2016-04
6 OSSV_EDITIONS=($(ls -d 201*|sort -n))
7 OSSV_EDITIONS_LENGTH=${#OSSV_EDITIONS[@]}
8
9 OSSV_EDITIONS_TMP=$(mktemp -d --suffix=osstvw_edit)
10 function cleanup {
11   rm -rf "${OSSV_EDITIONS_TMP}"
12 }
13 trap cleanup EXIT
14
15 # Create OSSTVW ocean blue to compare new files against
16 convert -size 5000x5000 xc:'#e6f6ff' -depth 8 ${OSSV_EDITIONS_TMP}/blue.png
17
18 # Outer loop (compare from)
19 for (( i=0; i<${OSSV_EDITIONS_LENGTH}; i++ )); do
20   # Inner loop (compare to)
21   for (( o=${i}; o<${OSSV_EDITIONS_LENGTH}; o++ )); do
22     # Skip when outer == inner edition
23     if [ "$i" != "$o" ]; then
24       OSSV_DIFF_NAME="diff-${OSSV_EDITIONS[$i]}-${OSSV_EDITIONS[$o]}"
25       mkdir -p "${OSSV_DIFF_NAME}"
26       # TIF file loop for edition - see end of loop for find
27       while IFS= read -r -d $'\0' FILE; do
28         FILE_1="${OSSV_EDITIONS[$i]}/${FILE}"
29         FILE_2="${OSSV_EDITIONS[$o]}/${FILE}"
30         if [ ! -f "${FILE_1}" ]; then
31           FILE_1="${OSSV_EDITIONS_TMP}/blue.png"
32         fi
33         # Skip existing files or excluded files
34         if [ ! -f "${OSSV_DIFF_NAME}/${FILE}" ] && [ ! -f "${OSSV_DIFF_NAME}/${FILE}.skip" ]; then
35           echo Creating diff ${OSSV_EDITIONS[$i]} ${OSSV_EDITIONS[$o]} "${FILE_1}" "${OSSV_DIFF_NAME}/${FILE}"
36           # Background compare + gdalcopyproj
37           ((compare -quiet "${FILE_1}" "${FILE_2}" -compose Src -highlight-color Red -lowlight-color 'rgba(255,255,255,0)' -define tiff:tile-geometry=512x512 "${OSSV_DIFF_NAME}/${FILE}" || true) && python ${GDALCOPYPROJ} "${FILE_2}" "${OSSV_DIFF_NAME}/${FILE}" ) &
38
39           # Check how many background processes and wait if exceed
40           running=($(jobs -rp))
41           while [ ${#running[@]} -ge 16 ] ; do
42             sleep 1   # this is not optimal, but you can't use wait here
43             running=($(jobs -rp))
44           done
45         fi
46       done < <(find "${OSSV_EDITIONS[$o]}" -maxdepth 1 -name '*.tif' -printf '%f\0')
47
48       # Wait for background jobs to finish
49       wait
50
51       echo "Removing empty diffs"
52       # TIF cleanup loop for images which have no diff
53       while IFS= read -r -d $'\0' FILE; do
54         ( FILE_COLOURS=$(identify -quiet -format '%k' "${OSSV_DIFF_NAME}/${FILE}")
55         if [ "$FILE_COLOURS" -lt "2" ]; then
56           rm -f "${OSSV_DIFF_NAME}/${FILE}"
57           touch "${OSSV_DIFF_NAME}/${FILE}.skip"
58         fi ) &
59
60         # Check how many background processes and wait if exceed
61         running=($(jobs -rp))
62         while [ ${#running[@]} -ge 16 ] ; do
63           sleep 1   # this is not optimal, but you can't use wait here
64           running=($(jobs -rp))
65         done
66       done < <(find "${OSSV_DIFF_NAME}" -maxdepth 1 -name '*.tif' -size -180000c -printf '%f\0')
67       if [ ! -f "ossv-${OSSV_DIFF_NAME}-combined.vrt" ]; then
68         gdalbuildvrt -resolution highest -hidenodata "ossv-${OSSV_DIFF_NAME}-combined.vrt" ${OSSV_DIFF_NAME}/*.tif
69       fi
70     fi
71   done
72 done
73 echo Now create external overview layers
74 echo eg: gdaladdo --config GDAL_CACHEMAX=16000 -ro --config COMPRESS DEFLATE --config COMPRESS_OVERVIEW DEFLATE --config ZLEVEL 9 --config BIGTIFF_OVERVIEW IF_SAFER --config GDAL_TIFF_OVR_BLOCKSIZE 512 -r average ossv-2016-04-combined.vrt 4 16 64 256 1024 4096