# dem_file: GeoTIFF DEM containing the surface to calculate contours
# interval: Contours interval
# format: OGR output format
# simplify: Simplify value
# epsg: target EPSG code
#
# ------
# output: If successful, prints the full path to the contours file. Otherwise it prints "error"

ext=""
if [ "${format}" = "GeoJSON" ]; then
    ext="json"
elif [ "${format}" = "GPKG" ]; then
    ext="gpkg"
elif [ "${format}" = "DXF" ]; then
    ext="dxf"
elif [ "${format}" = "ESRI Shapefile" ]; then
    ext="shp"
fi

#gdal_contour -a elevation -i ${interval} -f GPKG "${dem_file}" contours.gpkg > /dev/null
#ogr2ogr -dialect SQLite -where "ST_Length(geom) > 4" -simplify ${simplify} -t_srs EPSG:${epsg} -overwrite -f "${format}" output.$$ext contours.gpkg > /dev/null

MIN_CONTOUR_LENGTH=5

r.external input="${dem_file}" output=dem --overwrite
r.contour input=dem output=contours step=${interval} --overwrite
v.generalize --overwrite input=contours output=contours_smooth method=douglas threshold=${simplify}
v.generalize --overwrite input=contours_smooth output=contours_simplified method=chaiken threshold=1
v.generalize --overwrite input=contours_simplified output=contours_final method=douglas threshold=${simplify}
v.edit map=contours_final tool=delete threshold=-1,0,-$$MIN_CONTOUR_LENGTH query=length
v.out.ogr input=contours_final output=temp.gpkg format="GPKG"

ogr2ogr -t_srs EPSG:${epsg} -overwrite -f "${format}" output.$$ext temp.gpkg > /dev/null

if [ -e "output.$$ext" ]; then
    # ESRI ShapeFile extra steps to compress into a zip archive
    # we leverage Python's shutil in this case
    if [ "${format}" = "ESRI Shapefile" ]; then
        ext="zip"
        mkdir contours/
        mv output* contours/
        echo "import shutil;shutil.make_archive('output', 'zip', 'contours/')" | python
    fi

    echo "$$(pwd)/output.$$ext"
else
    echo "error"
fi
