From e52a69735c03d01e6bb128c0b04c3c6ee28802ce Mon Sep 17 00:00:00 2001 From: Ducky Date: Tue, 26 Sep 2023 02:13:31 +0100 Subject: [PATCH] osm-garmin: add Containerfile --- osm-garmin/Containerfile | 12 +++ osm-garmin/app/build-gmap.sh | 146 +++++++++++++++++++++++++++++++ osm-garmin/app/daemon.sh | 45 ++++++++++ osm-garmin/app/install-mkgmap.sh | 30 +++++++ osm-garmin/app/utils.sh | 61 +++++++++++++ 5 files changed, 294 insertions(+) create mode 100644 osm-garmin/Containerfile create mode 100755 osm-garmin/app/build-gmap.sh create mode 100644 osm-garmin/app/daemon.sh create mode 100644 osm-garmin/app/install-mkgmap.sh create mode 100644 osm-garmin/app/utils.sh diff --git a/osm-garmin/Containerfile b/osm-garmin/Containerfile new file mode 100644 index 0000000..31a73b9 --- /dev/null +++ b/osm-garmin/Containerfile @@ -0,0 +1,12 @@ +FROM registry.fedoraproject.org/fedora:38 + +WORKDIR /app +VOLUME /data +COPY app/ /app + +RUN chmod +x /app/*.sh +RUN dnf install --assumeyes --setopt=install_weak_deps=False java-1.8.0-openjdk nano unzip +RUN /app/install-mkgmap.sh +RUN dnf clean --assumeyes all + +CMD ["/app/bin/daemon.sh"] \ No newline at end of file diff --git a/osm-garmin/app/build-gmap.sh b/osm-garmin/app/build-gmap.sh new file mode 100755 index 0000000..899f408 --- /dev/null +++ b/osm-garmin/app/build-gmap.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash + +. /app/utils.sh + +region="$1" +style="$2" + +cwd="$(pwd)" + +function build_gmap() { + region="$1" + style="$2" + + region_shortname="$(get_region_shortname "$region")" + + bounds_dir_md5="$(get_md5_dir "$bounds_dir")" + map_md5="$(get_md5 "$maps_dir/$region_shortname.osm.pbf")" + version="$(echo "$bounds_dir_md5|$map_md5" | md5sum | cut -d' ' -f1)" + + generated_map_dir="$out_dir/$region_shortname/" + version_path="$generated_map_dir/version.txt" + + build="true" + build_failed="false" + + if [[ -n "$style" ]]; then + style="$styles_dir/$style.TYP" + fi + + mkdir -p "$generated_map_dir" + + if [[ -f "$version_path" ]]; then + if [[ $(cat "$version_path") == "$version" ]]; then + build="false" + fi + fi + + if [[ $build == "true" ]]; then + cd "$build_dir" + + exec_mkgmap \ + --add-pois-to-areas \ + --bounds="$bounds_dir" \ + --gmapsupp \ + --index \ + --route \ + $tiles_dir/$region_shortname/6324*.osm.pbf $style + + [[ $? != 0 ]] && build_failed="true" + cd "$cwd" + + if [[ $build_failed == "false" ]]; then + rm -f $generated_map_dir/* + + mv "$build_dir/gmapsupp.img" "$generated_map_dir/" + mv "$build_dir/osmmap.img" "$generated_map_dir/" + mv "$build_dir/osmmap.tdb" "$generated_map_dir/" + echo "$version" > "$version_path" + + rm -f $build_dir/* + else + rm -f $build_dir/* + fi + fi +} + +function cleanup() { + rm -f "$build_dir/*" +} + +function exec_mkgmap() { + java -jar /opt/mkgmap/mkgmap.jar $@ +} + +function exec_splitter() { + java -jar /opt/splitter/splitter.jar $@ +} + +function update_bounds() { + function download_bounds() { + type="$1" + bounds_url_prefix="https://www.thkukuk.de/osm/data" + + curl -L --fail "$bounds_url_prefix/$type-latest.zip" -o "$bounds_dir/$type.zip" + unzip -o "$bounds_dir/$type.zip" -d "$bounds_dir" + rm -f "$bounds_dir/$type.zip" + } + + download_bounds "bounds" + download_bounds "sea" +} + +function update_osm_map() { + region="$1" + region_filename="$(get_region_shortname "$region").osm.pbf" + region_path="$maps_dir/$region_filename" + + curl -L "$(get_pbf_url "$region")" -o "$region_path" +} + +function update_tiles() { + region="$1" + region_tiles_dir="$tiles_dir/$(get_region_shortname "$region")" + + mkdir -p "$region_tiles_dir" + rm -rf "$region_tiles_dir/*" + cd "$region_tiles_dir" # HACK: --output-dir doesn't work + + exec_splitter "$maps_dir/$(get_region_shortname "$region").osm.pbf" + + cd "$cwd" +} + +mkdir -p "$bounds_dir" +mkdir -p "$build_dir" +mkdir -p "$maps_dir" +mkdir -p "$out_dir" +mkdir -p "$tiles_dir" + +if [[ -z $region ]]; then + die "No region specified" +fi + +if [[ "$(check_osm_region_exists "$region")" == "true" ]]; then + echo "๐Ÿ‘€ Checking for OSM updates..." + if [[ "$(check_osm_update "$region")" == "true" ]]; then + [[ $? != 0 ]] && die "Region \"$region\" does not exist" + + echo "๐ŸŒ Updating OSM map: $region" + update_osm_map "$region" + + echo "๐Ÿ“’ Updating bounds and sea..." + update_bounds + + echo "โœ‚๏ธ Building tiles: $region" + update_tiles "$region" + fi + + echo "๐Ÿ”จ Building Garmin map: $region" + build_gmap "$region" "$style" +else + [[ $? != 0 ]] && die "Region \"$region\" does not exist" +fi + +echo "๐Ÿงน Cleaning up..." +cleanup \ No newline at end of file diff --git a/osm-garmin/app/daemon.sh b/osm-garmin/app/daemon.sh new file mode 100644 index 0000000..01e8bdc --- /dev/null +++ b/osm-garmin/app/daemon.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +. /app/utils.sh + +sleep_build_fail="10" # 10 seconds +sleep_build_success="43200" # 12 hours +sleep_no_update="3600" # 1 hour +sleep_no_region="10" # 10 seconds + +function wait() { + time="$1" + action="$2" + + message="๐Ÿ’ค Sleeping $time seconds" + [[ -n "$action" ]] && message="$message ($action)" + message="$message..." + + echo "$message" + sleep $time +} + +while : +do + regions="europe/andorra" + style="USERBEAM" + + [[ -n "$OSMGARMIN_REGIONS" ]] && regions="$OSMGARMIN_REGIONS" + [[ -n "$OSMGARMIN_STYLE" ]] && style="$OSMGARMIN_STYLE" + + regions_array="$(echo $regions | tr ";" "\n")" + + for region in $regions_array; do + if [[ "$(check_osm_update "$region")" == "true" ]]; then + /app/build-gmap.sh "$region" "$style" + + if [[ $? == 0 ]]; then + wait $sleep_build_success "build success" + else + wait $sleep_build_fail "build fail" + fi + else + wait $sleep_no_update "no update" + fi + done +done \ No newline at end of file diff --git a/osm-garmin/app/install-mkgmap.sh b/osm-garmin/app/install-mkgmap.sh new file mode 100644 index 0000000..296306b --- /dev/null +++ b/osm-garmin/app/install-mkgmap.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +cwd="$(pwd)" + +function install() { + product="$1" + version="$(curl -Ls "https://www.mkgmap.org.uk/download/$product.html" | grep "text: current().version" | sed -e "s/^[^=]*//g" | sed -e "s/<\/span>//g")" + + cd "/tmp" + + curl -Ls "https://www.mkgmap.org.uk/download/$product-r$version.zip" -o "$product.zip" + + if [[ $? == 0 ]] && [[ -f "$product.zip" ]]; then + unzip "$product.zip" + + if [[ -d "$product-r$version" ]]; then + mv "$product-r$version" "/opt/$product" + rm -f "$product.zip" + else + exit 255 + fi + else + exit 255 + fi + + cd "$cwd" +} + +install "mkgmap" +install "splitter" \ No newline at end of file diff --git a/osm-garmin/app/utils.sh b/osm-garmin/app/utils.sh new file mode 100644 index 0000000..b0a7b44 --- /dev/null +++ b/osm-garmin/app/utils.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +bounds_dir="/data/build/bounds" +build_dir="/data/build/tmp" +maps_dir="/data/build/osm" +out_dir="/data/maps" +styles_dir="/data/styles" +tiles_dir="/data/build/tiles" + +function die() { + echo "Error: $1" + exit 255 +} + +function check_osm_region_exists() { + region="$1" + exists="true" + + curl -ILs --fail "$(get_pbf_url "$region")" 2> /dev/null + [[ $? != 0 ]] && exists="false" + + echo $exists +} + +function check_osm_update() { + region="$1" + region_path="$maps_dir/$(get_region_shortname "$region").osm.pbf" + update="true" + + md5="$(curl -Ls --fail "$(get_pbf_url "$region").md5" | cut -d' ' -f1)" + + if [[ $? == 0 ]]; then + if [[ -f "$region_path" ]]; then + if [[ "$(get_md5 "$region_path")" == "$md5" ]]; then + update="false" + fi + fi + fi + + echo $update +} + +function get_md5() { + file="$1" + echo "$(md5sum "$file" | cut -d' ' -f1)" +} + +function get_md5_dir() { + dir="$1" + echo "$(find "$dir" -type f -name "*.*" -exec md5sum {} + | awk '{print $1}' | sort | md5sum)" +} + +function get_pbf_url() { + region="$1" + echo "https://download.geofabrik.de/$region-latest.osm.pbf" +} + +function get_region_shortname() { + region="$1" + echo "$(echo "$region" | sed "s/\//./g")" +} \ No newline at end of file