Bulk Merging GeoJSONs into a Single File

I’ve spent a lot of time with Mapbox’s geojson.io and the GeoJSON format lately. One of the things I realized that I wanted to do was to take a pile of small GeoJSON files and combine them into a single file outside of the context of a database.

Scouring the Web

Of course, someone else had already thought of doing this and so I stumbled onto a series of Gists from 2012. There was a problem of course: these Gists used command line arguments for specifying the input and output files. I’m not a fan of this method because of the strain it puts on people who are uncomfortable with the command line. Rather I prefer to use text prompts to capture this same information.

Thus I ended up rewriting most of what was in th9ese Gists with the exception of the JSON (and GeoJSON) formatting logic which was perfectly fine.  I broke it down into discrete functions as opposed to a monolithic main function and spun it out into a repo with the test GeoJSON files that I was using to ensure its functionality.

The other twist here is that instead of requiring the user to supply the path names for the specific GeoJSONs that they want to combine I opt to have the script take the path to a folder and then have the OS return a list of file paths that were of the type GeoJSON. This change saves a lot of time when you’re setting up a batch processing operation and it eliminates the frustration of incorrectly entering a path name.

You can find the repo for this project dubbed BatchMergeGeoJSONs on Github.

Published by

Thomas Ryan

Thomas August Ryan is a map maker, freelance technology writer, and photographer. Hailing from Seattle you can now find him living in Austin.