]> git.openstreetmap.org Git - chef.git/blob - cookbooks/imagery/files/default/osstvw_make_diffs
109545025d359fc0417e757e9bf257ea0f895e16
[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
34         if [ ! -f "${OSSV_DIFF_NAME}/${FILE}" ]; 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       # TIF cleanup loop for images which have no diff
52       while IFS= read -r -d $'\0' FILE; do
53         ( FILE_COLOURS=$(identify -quiet -format '%k' "${OSSV_DIFF_NAME}/${FILE}")
54         if [ "$FILE_COLOURS" -lt "2" ]; then
55           rm -f "${OSSV_DIFF_NAME}/${FILE}"
56         fi ) &
57
58         # Check how many background processes and wait if exceed
59         running=($(jobs -rp))
60         while [ ${#running[@]} -ge 16 ] ; do
61           sleep 1   # this is not optimal, but you can't use wait here
62           running=($(jobs -rp))
63         done
64       done < <(find "${OSSV_DIFF_NAME}" -maxdepth 1 -name '*.tif' -size -180000 -printf '%f\0')
65     fi
66   done
67 done
68 echo Now create the VRT and external overview layers
69 echo 1: gdalbuildvrt -resolution highest -hidenodata diffXYZ.vrt diff/*.tif
70 echo 2: 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