Skip to content

Instantly share code, notes, and snippets.

@YohannesTz
Created March 2, 2024 09:56
Show Gist options
  • Save YohannesTz/17d87add1898fad111c2987f0c2d2956 to your computer and use it in GitHub Desktop.
Save YohannesTz/17d87add1898fad111c2987f0c2d2956 to your computer and use it in GitHub Desktop.
a simple script to generate GPX file from an array of LatLon points and save it to track.gpx file
#!/bin/bash
coordinates=("$@")
# Start GPX file
echo '<?xml version="1.0" encoding="UTF-8"?>' > track.gpx
echo '<gpx version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">' >> track.gpx
echo '<trk>' >> track.gpx
echo '<trkseg>' >> track.gpx
# Add coordinates
for ((i=0; i<$((${#coordinates[@]}/2)); i++)); do
lat=${coordinates[i*2]}
lon=${coordinates[i*2+1]}
echo "<trkpt lat=\"$lat\" lon=\"$lon\"></trkpt>" >> track.gpx
done
# End GPX file
echo '</trkseg>' >> track.gpx
echo '</trk>' >> track.gpx
echo '</gpx>' >> track.gpx
echo "GPX file generated: track.gpx"
./genrate_gpx.sh 9.04105476, 38.8427777, 9.04105476, 38.8427777, 9.04105476, 38.8427777, 9.04105476, 38.8427777, 9.04105476, 38.8427777, 9.04105476, 38.8427777, 9.04105476, 38.8427777, 9.04105476, 38.8427777, 9.03694864, 38.82990833, 9.0356817, 38.82844764
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment