exupero's blog

Merging GeoJSON features

In the previous post I mentioned that I track hikes, bicycle rides, and car trips in GeoJSON files. Each file is named after a particular date and contains the path of that day's activity. To see a cumulative map of everywhere I've logged hikes or bike rides or drives, I have a Babashka script called merge-geojson-features which combines the GeoJSON data into a single structure:

(require '[cheshire.core :as json])
(let [files *command-line-args*]
      {:type "FeatureCollection"
       :features (sequence
                     (map slurp)
                     (map json/parse-string)
                     (mapcat #(get % "features")))
      {:pretty (json/create-pretty-printer
                 (assoc json/default-pretty-print-options
                        :indent-arrays? true))})))

Probably jq could do the same with a one-liner, but so far I haven't figured out how. If you know, please email me.

Writing 14 lines of code in a language I'm familiar with is usually faster than writing one line in a language I only know the rudiments of, which is why I usually reach for Babashka for tasks like this. There's also the temptation to think that the script will develop further. For example, I could store the source filename as a property on each feature then be able to see when I took a particular trip in the shape info popup on geojson.io. So far, however, I haven't had the need.

The script prints the GeoJSON to the terminal, so I typically pipe it to pbcopy then paste the data into geojson.io to see it on a map.