#! /bin/sh

# Skript zum Erstellen des Topo-Hintergrunds für europarc Webkarten
# Daten: GMTED2010 von http://earthexplorer.usgs.gov/
# (Daniel Kerkow, geolicious GbR)
# All rights reserved.


# The script should be run from within its folder, and it expects a folder 
# ./raw, containing the DEM data files in .tif format with EPSG:4326.

mkdir tmp
mkdir output

echo '### merging the single datasets to VRT-Format'
gdalbuildvrt \
    -overwrite \
    -resolution highest \
    tmp/dem-merged.vrt \
    raw/*.tif

echo '### reprojecting from WGS84/latlong (EPSG:4326) to Google Mercartor (EPSG:3785) with bilinear resampling and cropping'
gdalwarp \
    -multi \
    -wo NUM_THREADS=ALL_CPUS \
    -s_srs EPSG:4326 \
    -t_srs EPSG:900913 \
    -r bilinear \
    -tr 100 100 \
    -te -20000.0 4700000.0 2800000.0 8000000.0 \
    -of GTiff \
    tmp/dem-merged.vrt \
    tmp/dem-merged-reproj.tif

### Generating colored output files:

echo '### generating shaded relief map'
gdaldem hillshade \
    -co compress=lzw \
    -of GTiff \
    tmp/dem-merged-reproj.tif \
    tmp/dem-hillshade.tif

echo '### generating color relief map'
gdaldem color-relief \
    -co compress=lzw \
    -co BIGTIFF=YES \
    -of GTiff \
    tmp/dem-merged-reproj.tif \
    color-ramp.txt \
    tmp/color-relief.tif

#echo '### gererating slope shading'
#gdaldem slope \
#    -co compress=lzw \
#    -of GTiff \
#    tmp/dem-merged-reproj.tif \
#    tmp/slope.tif

#echo '### assigning slope-ramp.txt to slope.tif'
#gdaldem color-relief \
#    -co compress=lzw \
#    -of GTiff \
#    tmp/slope.tif \
#    slope-ramp.txt \
#    tmp/slope-shaded.tif
    
echo '### merging color-relief and hillshade'
./hsv_merge.py \
    -q \
    tmp/color-relief.tif \
    tmp/dem-hillshade.tif \
    output/color-relief-merged.tif

echo '### cleaning up ...'
 
rm -r tmp/

echo ''
echo '### Operation finished ! ###'
echo ''
