#!/bin/bash set -e echo '=== TRI MAX-AGGREGATION REGEN (GDAL 3.4 compatible) ===' echo "Start: $(date)" TMP='/tmp/tri_gen' TERRAIN_DIR='/home/slin/enduro-trails/data/terrain' TRI_DIR="${TERRAIN_DIR}/tri" mkdir -p $TMP echo '' echo '=== Step 1: Verify tri_raw_full.tif ===' if [ ! -f "$TMP/tri_raw_full.tif" ]; then echo "FATAL: tri_raw_full.tif not found" exit 1 fi echo "OK: $(gdalinfo $TMP/tri_raw_full.tif 2>&1 | grep 'Size is')" echo '' echo '=== Step 2: 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: $(du -sh $TMP/tri_colored_max.tif | cut -f1)" echo '' echo '=== Step 3: Create low-res version with MAX resampling ===' # Target resolution ~0.005 deg (~500m) for zooms 5-9 # gdalwarp -r max works on GDAL 3.4 (unlike gdaladdo -r max) gdalwarp -r max \ -tr 0.005 0.005 \ -srcnodata "0 0 0 0" \ $TMP/tri_colored_max.tif \ $TMP/tri_colored_lowres.tif \ -co COMPRESS=LZW \ -co BIGTIFF=YES \ -overwrite echo "Low-res colored TRI: $(du -sh $TMP/tri_colored_lowres.tif | cut -f1)" echo "Size: $(gdalinfo $TMP/tri_colored_lowres.tif 2>&1 | grep 'Size is')" echo '' echo '=== Step 4: Clean output directory ===' rm -rf ${TRI_DIR} mkdir -p ${TRI_DIR} echo '' echo '=== Step 5: Generate tiles z5-9 from LOW-RES (max-aggregated) ===' gdal2tiles.py \ --zoom=5-9 \ --processes=4 \ --tilesize=256 \ --resampling=average \ --webviewer=none \ $TMP/tri_colored_lowres.tif \ ${TRI_DIR}/ echo "Low zoom tiles done" echo '' echo '=== Step 6: Generate tiles z10-12 from FULL-RES ===' gdal2tiles.py \ --zoom=10-12 \ --processes=4 \ --tilesize=256 \ --resampling=average \ --webviewer=none \ $TMP/tri_colored_max.tif \ ${TRI_DIR}/ echo "High zoom tiles done" echo '' echo '=== Step 7: 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 8: Sample tile check (zoom 7) ===' TILE=$(find ${TRI_DIR}/7/ -name '*.png' -size +1000c 2>/dev/null | head -1) if [ -n "$TILE" ]; then echo "Sample: $TILE" python3 -c "from PIL import Image; im=Image.open('$TILE'); print('Size:', im.size, 'Mode:', im.mode); bands=im.split(); print('R range:', bands[0].getextrema(), 'A range:', bands[3].getextrema())" 2>/dev/null || file "$TILE" else echo "No large tiles at zoom 7" fi echo '' echo '=== Step 9: Avg 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)"