datasette/importers/gpx-tracks
2022-08-10 12:51:24 +02:00

45 lines
1.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euxo pipefail
usage () {
echo "Recursively imports all gpx tracks in a directory into an SQLite database."
echo ""
echo "Usage: $(basename $0) directory target-database"
}
gpx_directory="$1"
target_database="$2"
# basic argument verification
case "$@" in -h|--help) usage && exit 0;; esac
[ -z "$gpx_directory" ] && usage && exit 1
[ -z "$target_database" ] && usage && exit 1
# we write to a temporary location to make the replace option atomic (if on the same device)
tmp_dir="$(mktemp -d diogenes.XXXXX -p /tmp)"
tmp="$tmp_dir/$(basename $target_database)"
# recursive scan and import
while IFS= read -d '' -r file; do
spatialite_xml_load -d "$tmp" -x "$file"
done < <(find "$gpx_directory" -name '*.gpx' -print0)
indices=(
"gpx_metadata_name"
"gpx_metadata_time"
"gpx_trk_trkseg_trkpt_ele"
"gpx_trk_trkseg_trkpt_extensions"
"gpx_trk_trkseg_trkpt_extensions_heading"
"gpx_trk_trkseg_trkpt_extensions_speed"
"gpx_trk_trkseg_trkpt_hdop"
"gpx_trk_trkseg_trkpt_time"
)
for i in "${indices[@]}"
do
sqlite3 "$tmp" "CREATE INDEX ${i}_ ON $i ( node_value )"
done
mv -f "$tmp" "$target_database"
rm -rf "$tmp_dir"
# TODO: Create joined view