DEV Community

Cover image for How a 3-Day Hike on the Camino de Santiago Led Me to Build My Own Mapping Tool
David Raviv
David Raviv

Posted on

How a 3-Day Hike on the Camino de Santiago Led Me to Build My Own Mapping Tool

A few weeks ago, I was planning a special trip: a three-day hike along the central trail of the Portuguese Camino de Santiago with my spouse and son. We chose the central route for its beautiful mountain views, a welcome change from the flatter coastal path.

As I dove into planning, I found plenty of websites with maps and descriptions. I even found a fantastic resource that provided a single KML file for the entire Camino trail. Perfect!

Or so I thought.

The Problem: One Giant Map, Many Small Steps

The file I had was a single, continuous path spanning hundreds of kilometers. What I needed were separate files for our daily segments. More importantly, I wanted to customize those segments. Every guide suggests different daily distances, but I needed to plan the route according to our family's pace.

I figured this would be a simple task. I opened the KML file in Google Earth, hoping to find a "split" or "slice" tool. No luck. I tried other GUI apps like Komoot, but they were geared towards planning routes from scratch, not editing existing ones in this specific way. I couldn't find a simple, intuitive tool to just chop up a path based on a set of points.

The frustration was real. I had the path, I knew where our daily stops would be, but I had no easy way to connect the two.

The Developer's Itch: "I Can Build That!"

After a few hours of searching, the developer in me took over. "How hard can it be to write a script for this?"

I decided to build a simple, no-frills command-line tool that would do exactly one thing: take a KML file with a long path and a second KML file with named points (our daily stops), and slice the path at those points.

I started with KML because my initial visualization was all in Google Earth. My plan was to create the daily KML segments and then use an online converter to get GPX files, which I could easily upload to Komoot and my Garmin Fenix watch.

Building the KML Slicer

I chose Node.js for its simplicity and the amazing packages available on npm. The core of the project relies on two key libraries:

  • fast-xml-parser: To parse the KML files, which are essentially just XML. This let me easily extract the long string of coordinates from the trail and the specific coordinates for my daily stops.
  • @turf/turf: This is a powerful library for geospatial analysis. I used its @turf/nearest-point-on-line function to find the exact point on the main trail that was closest to each of my "stop" points. This was the magic that made the whole thing work accurately.

The logic was straightforward:

  1. Read and parse the route KML to get the main LineString.
  2. Read and parse the points KML to get the list of Placemark locations.
  3. For each point, use Turf.js to find the closest point on the main line.
  4. Slice the main line's coordinate array at the indices of these split points.
  5. Generate a new KML file for each segment, naming it after the ending point (e.g., Day 1 - Town A.kml).

The Result: A Tool That Just Works

In a few hours, I had a working script! It was incredibly satisfying to run it and see the output folder fill up with perfectly sliced KML files for each day of our hike.

Here’s what the command looks like:

node index.js <path/to/route.kml> <path/to/points.kml> <path/to/output_directory>
Enter fullscreen mode Exit fullscreen mode

The trip was a huge success, and having custom maps on our devices each day was a game-changer.

Try It Yourself and What's Next

I realized that if I had this problem, other hikers, runners, or cyclists probably do too. So, I cleaned up the code, added some tests, and published it on GitHub.

You can check out the project here: https://github.com/davidraviv/kml-slicer

It's completely free and open-source. If you've ever found yourself in a similar mapping predicament, I'd love for you to try it out.

Right now, the tool is focused on KML, but if I see there's enough interest, I'll add support for slicing GPX files directly.

If you find this tool useful for planning your own adventures, please consider starring the repo on GitHub or even sponsoring the project. Any support would be amazing and will motivate me to add more features.

Happy hiking!

Top comments (0)