Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Displaying Hikes with gpx.studio

Published on

Updated on

3 minute reading time

As the weather gets warmer, I am starting to go on more hikes. Several people on their websites share a Strava embed which highlights a path taken during their workout. I believe as a community this has great potential for sharing our favorite hiking paths. I don’t, however, want to rely on Strava to host my GPS data. Instead, we will showcase how to accomplish the same effect but with open technologies.

At the end of this post, we will achieve in creating an embed like the following:

To get there, we will talk about:

Recording the Walk

My first requirement is that the route is recorded in an open source format. For this I chose GPX otherwise known as the GPS Exchange format. There are three types of messages that a GPX file can contain:

  • Waypoint: A point of interested denoted by a latitude/longitude/altitude (LLA) and a time with a small note describing it.
  • Route: An ordered list of LLA and time points used to denote a navigation from a start to end location.
  • Track: An ordered list of frequent LLA and time points denoting a GPS feed.

Popular applications such as AllTrails and Strava allow you to export your activities as GPX files. These will generally contain the track information. A cool open source alternative application called OSMTracker records track information as well as enabling an easy way to create waypoints.

Grabbing an embed

To create the embed we will use a project called gpx.studio. It’s a great open source project created by vcoppe on GitHub and built on top of Mapbox and OpenStreetMap. If you end up using this application, consider providing a donation to their developer to cover for some of the hosting and API costs. Our next step is to upload the GPX file we recorded from our exercise onto our website and configure our webserver so that gpx.studio can access the file. I store my hikes in data/tracks and created the following Nginx rule:

# Allow any application access
location /data/tracks {
  add_header Access-Control-Allow-Origin "*";
}

Next, go to the gpx.studio about page to enter in the URL in the embed section. It will then output the following HTML code to add to your website

<iframe 
  src="https://gpx.studio/?state=%7B%22urls%22:%5B%22https%3A%2F%2Fbrandonrozek.com%2Fdata%2Ftracks%2F2022-05-18-Burden-Pond-Preserve.gpx%22%5D%7D&embed&distance"
  width="100%"
  height="500"
  frameborder="0"
  allowfullscreen>
   	<p><a href="https://gpx.studio/?state=%7B%22urls%22:%5B%22https%3A%2F%2Fbrandonrozek.com%2Fdata%2Ftracks%2F2022-05-18-Burden-Pond-Preserve.gpx%22%5D%7D"></a></p>
</iframe>

Shortcodes for website

Notice that the URL structure follows a prescribed format. This means we can create a Hugo shortcode to generate these iFrames for us! The embed shown earlier on this page was created using the following shortcode command:

{{< displayGPX “2022-05-18-Burden-Pond-Preserve.gpx” >}}

So what’s the structure of the URLs? If we perform an URL decode on the IFrame URL we get the following:

https://gpx.studio/?
	state={"urls":["https://brandonrozek.com/data/tracks/2022-05-18-Burden-Pond-Preserve.gpx"]}
	&embed&distance

We can then replace the URL in the array with the one that you’ll use for your website. Since I’m sticking all my GPX files under https://brandonrozek.com/data/tracks, I only have to pass the filename into the shortcode which is stored under layouts/shortcodes/displayGPX.html.

<!-- Grab the filename and append it to the base url -->
{{ $url := printf "%sdata/tracks/%s" .Site.BaseURL (.Get 0) }}
<!-- Make the state={...} URL friendly -->
{{ $url_state := querify "state" (printf "{\"urls\":[\"%s\"]}" $url) }}
<!-- Create the Iframe and A tag URLs -->
{{ $iframe_url := printf "https://gpx.studio/?%s&embed&distance" $url_state }}
{{ $a_url := printf "https://gpx.studio/?%s" $url_state }}
<!-- Output the IFrame -->
<iframe {{ printf "src=%q" $iframe_url | safeHTMLAttr }}
        width="100%"
        height="500"
        frameborder="0"
        allowfullscreen>
            <p><a {{ printf "href=%q" $a_url | safeHTMLAttr }}></a></p>
</iframe>

Conclusion

With this we created a cool visual representation of GPX data on our own website built on top of open technologies. Since our routes are stored in GPX files, if gpx.studio was to go down, then we can likely find an alternative to plot the file for us. Gpx.studio is also built on top of OpenStreetMap, a great long-standing community driven mapping project.

Reply via Email

Have you published a response to this? :