Ryan Rueger

ryan@rueg.re / picture / key / home
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrrueger <rrueger.github@velleto.com>2021-08-19 19:55:57 +0200
committerrrueger <rrueger.github@velleto.com>2021-08-19 19:55:57 +0200
commit501e376b507f9dfbe6ffc5b314eaff17a2958eb7 (patch)
treec14e2db26fa8025694d8bdff959c9cee2e08698b
downloadcontours-501e376b507f9dfbe6ffc5b314eaff17a2958eb7.tar.gz
contours-501e376b507f9dfbe6ffc5b314eaff17a2958eb7.tar.bz2
contours-501e376b507f9dfbe6ffc5b314eaff17a2958eb7.zip
Initial commit
-rw-r--r--contour-line-example.pngbin0 -> 6458224 bytes
-rw-r--r--contour.py42
-rw-r--r--contour.sh85
3 files changed, 127 insertions, 0 deletions
diff --git a/contour-line-example.png b/contour-line-example.png
new file mode 100644
index 0000000..62cca89
--- /dev/null
+++ b/contour-line-example.png
Binary files differ
diff --git a/contour.py b/contour.py
new file mode 100644
index 0000000..48fd2a5
--- /dev/null
+++ b/contour.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+from sys import argv
+
+if not len(argv) == 6:
+ print("contour.py: Error: contour.py <GPKG Input File> <PNG Output File> <FG colour> <BG colour> <Line Thickness>")
+ print("Colors are given in hex RGB or RGBA values")
+ exit(1)
+
+from qgis.core import (
+ QgsApplication,
+ QgsMapRendererParallelJob,
+ QgsMapSettings,
+ QgsProject,
+ QgsVectorLayer,
+)
+
+from qgis.PyQt.QtGui import QColor
+from qgis.PyQt.QtCore import QSize
+
+QgsApplication.setPrefixPath('/usr', True)
+
+path_to_gpkg = argv[1]
+path_to_png = argv[2]
+
+gpkg_layer = path_to_gpkg + "|layername=Contour"
+vlayer = QgsVectorLayer(gpkg_layer, "Contour", "ogr")
+QgsProject.instance().addMapLayer(vlayer)
+vlayer.renderer().symbol().setColor(QColor(argv[3]))
+vlayer.renderer().symbol().setWidth(float(argv[5]))
+
+settings = QgsMapSettings()
+settings.setLayers([vlayer])
+settings.setBackgroundColor(QColor(argv[4]))
+settings.setOutputSize(QSize(1000, 1000))
+settings.setExtent(vlayer.extent())
+settings.setOuputDpi = 100
+
+render = QgsMapRendererParallelJob(settings)
+render.start()
+render.waitForFinished()
+render.renderedImage().save(path_to_png, "png")
diff --git a/contour.sh b/contour.sh
new file mode 100644
index 0000000..e178183
--- /dev/null
+++ b/contour.sh
@@ -0,0 +1,85 @@
+#!/bin/bash
+
+set -e
+
+################################################################################
+# Configuration
+################################################################################
+
+PROJECT_ROOT=
+
+# Location to create contour of, e.g. N47E008
+LOCATION=
+# (Linear contour lines) Difference in elevation between contour lines, e.g. 100
+ELEV=
+# (Exponential contour lines) The exponential base to use for drawing contour
+# lines at expoential intervals, e.g. 1.1
+BASE=
+# Offset from sea-level to use when drawing contour lines, e.g. 400
+OFFSET=
+
+# Colours given in RGB or RGBA hex values, e.g. red, #f00, or #00000000
+FG_COLOUR=
+BG_COLOUR=
+# Given in float, e.g. 0.12
+LINE_THICKNESS=
+
+# Used for downloading the data from NASA
+EARTHDATA_USERNAME=
+EARTHDATA_PASS=
+################################################################################
+
+cd "$PROJECT_ROOT"
+
+if [[ ! -r "data/$LOCATION.SRTMGL1.hgt.zip" ]]
+then
+ cd data
+
+ wget \
+ --quiet \
+ --show-progress \
+ --continue \
+ --user="$EARTHDATA_USERNAME" \
+ --password="$EARTHDATA_PASS" \
+ "https://e4ftl01.cr.usgs.gov//DP133/SRTM/SRTMGL1.003/2000.02.11/$LOCATION.SRTMGL1.hgt.zip"
+
+ cd ..
+fi
+
+if [[ ! -r "data/$LOCATION.hgt" ]]
+then
+ cd data
+ unzip "$LOCATION.SRTMGL1.hgt.zip"
+ cd ..
+fi
+
+SIGMA=50
+KERNEL=100
+
+ORIGINAL=$PROJECT_ROOT/data/$LOCATION.hgt
+SMOOTHED=$PROJECT_ROOT/data/$LOCATION-s$SIGMA-r$KERNEL.sdat
+CONTOUR_LIN=$PROJECT_ROOT/gdal_contours/linear-${ELEV}m-$LOCATION-s$SIGMA-r$KERNEL.gpkg
+CONTOUR_EXP=$PROJECT_ROOT/gdal_contours/exp-$BASE-offset-$OFFSET-$LOCATION-s$SIGMA-r$KERNEL.gpkg
+
+if ! [[ -r "$SMOOTHED" ]]
+then
+ saga_cmd grid_filter "Gaussian Filter" \
+ -INPUT "$ORIGINAL" \
+ -SIGMA "$SIGMA" \
+ -KERNEL_TYPE 0 \
+ -KERNEL_RADIUS "$KERNEL" \
+ -RESULT "$SMOOTHED"
+fi
+
+if ! [[ -r "$CONTOUR_LIN" ]]
+then
+ gdal_contour -a ELEV -i "$ELEV" -f "GPKG" "$SMOOTHED" "$CONTOUR_LIN"
+fi
+
+if ! [[ -r "$CONTOUR_EXP" ]]
+then
+ gdal_contour -e ELEV -off "$OFFSET" -e "$BASE" -f "GPKG" "$SMOOTHED" "$CONTOUR_EXP"
+fi
+
+python3 contour.py "$CONTOUR_EXP" "${CONTOUR_EXP/%gpkg/png}" "$FG_COLOUR" "$BG_COLOUR" "$LINE_THICKNESS"
+python3 contour.py "$CONTOUR_LIN" "${CONTOUR_LIN/%gpkg/png}" "$FG_COLOUR" "$BG_COLOUR" "$LINE_THICKNESS"