#!/bin/bash set -e echo '=== TRI REGENERATION WITH MAX-AGGREGATION FOR LOW ZOOMS ===' echo "Start: $(date)" TMP='/tmp/tri_gen' SRTM_DIR='/home/slin/enduro-trails/data/srtm' TERRAIN_DIR='/home/slin/enduro-trails/data/terrain' TRI_DIR="${TERRAIN_DIR}/tri" mkdir -p $TMP echo '' echo '=== Step 1: Check tri_raw_full.tif ===' if [ -f "$TMP/tri_raw_full.tif" ]; then echo "tri_raw_full.tif exists:" gdalinfo -stats $TMP/tri_raw_full.tif 2>&1 | grep -E "Size|STATISTICS" || true else echo "tri_raw_full.tif NOT FOUND — regenerating from SRTM..." find $SRTM_DIR -name '*.hgt' > $TMP/hgt_list.txt echo "HGT files: $(wc -l < $TMP/hgt_list.txt)" gdalbuildvrt -vrtnodata -32768 -overwrite -input_file_list $TMP/hgt_list.txt $TMP/srtm.vrt gdaldem TRI $TMP/srtm.vrt $TMP/tri_raw_full.tif -of GTiff -co COMPRESS=LZW -co BIGTIFF=YES -compute_edges echo "TRI computed:" gdalinfo -stats $TMP/tri_raw_full.tif 2>&1 | grep -E "Size|STATISTICS" || true fi echo '' echo '=== Step 2: Add overviews with MAX resampling ===' # Check GDAL version for -r max support GDAL_VER=$(gdal-config --version 2>/dev/null || gdalinfo --version | grep -oP '\d+\.\d+') echo "GDAL version: $GDAL_VER" # Try gdaladdo -r max if gdaladdo -r max --config COMPRESS_OVERVIEW LZW $TMP/tri_raw_full.tif 2 4 8 16 32 64 2>&1; then echo "Overviews with MAX added successfully" gdalinfo $TMP/tri_raw_full.tif 2>&1 | grep -i "overview" || true else echo "gdaladdo -r max FAILED — using gdalwarp fallback" # Fallback: create a low-res version with max resampling gdalwarp -r max \ -tr 0.01 0.01 \ $TMP/tri_raw_full.tif \ $TMP/tri_raw_lowres.tif \ -co COMPRESS=LZW -overwrite echo "Low-res TRI created:" gdalinfo -stats $TMP/tri_raw_lowres.tif 2>&1 | grep -E "Size|STATISTICS" || true fi echo '' echo '=== Step 3: Color relief (v5 ramp, threshold 15) ===' cat > $TMP/tri_color_max.txt << 'RAMP' nv 0 0 0 0 0 0 0 0 0 14 0 0 0 0 15 255 255 100 60 20 255 230 50 100 25 255 200 0 140 30 255 150 0 180 40 255 100 0 200 50 230 50 0 220 65 200 0 0 235 80 180 0 50 245 100 150 0 100 255 RAMP gdaldem color-relief \ $TMP/tri_raw_full.tif \ $TMP/tri_color_max.txt \ $TMP/tri_colored_max.tif \ -alpha \ -of GTiff \ -co COMPRESS=LZW \ -co BIGTIFF=YES echo "Colored TRI created:" gdalinfo -stats $TMP/tri_colored_max.tif 2>&1 | grep STATISTICS_MAX || true echo '' echo '=== Step 3-alt: Add overviews to colored file (max on RGBA) ===' gdaladdo -r max --config COMPRESS_OVERVIEW LZW $TMP/tri_colored_max.tif 2 4 8 16 32 64 2>&1 || true echo "Overviews on colored file:" gdalinfo $TMP/tri_colored_max.tif 2>&1 | grep -i "overview" || true echo '' echo '=== Step 4: Generate tiles ===' rm -rf ${TRI_DIR} mkdir -p ${TRI_DIR} gdal2tiles.py \ --zoom=5-12 \ --processes=4 \ --tilesize=256 \ --resampling=average \ --webviewer=none \ $TMP/tri_colored_max.tif \ ${TRI_DIR}/ echo '' echo '=== Step 5: Stats ===' for z in 5 6 7 8 9 10 11 12; do total=$(find ${TRI_DIR}/$z/ -name '*.png' 2>/dev/null | wc -l) nonempty=$(find ${TRI_DIR}/$z/ -name '*.png' -size +400c 2>/dev/null | wc -l) echo " zoom $z: $total total, $nonempty non-empty" done echo '' echo '=== Step 6: Sample tile check ===' TILE=$(find ${TRI_DIR}/7/ -name '*.png' -size +1000c 2>/dev/null | head -1) if [ -n "$TILE" ]; then echo "Sample tile: $TILE" python3 -c "from PIL import Image; im=Image.open('$TILE'); print('Size:', im.size, 'Mode:', im.mode, 'Extrema:', im.getextrema())" 2>/dev/null || file "$TILE" else echo "No non-empty tiles found at zoom 7!" fi echo '' echo '=== Step 7: Average tile size at zoom 7 ===' find ${TRI_DIR}/7/ -name '*.png' -size +400c -exec ls -la {} \; 2>/dev/null | awk '{sum+=$5; n++} END {if(n>0) print "avg:", sum/n, "bytes, count:", n; else print "no tiles"}' echo '' du -sh ${TRI_DIR} echo '=== DONE ===' echo "End: $(date)"