Online Maps moving forward, from KML to GeoJSON

Once upon a time the way for me to access local maps was to ask relatives to give me their old phone books that also while being some 600 pages, included the local area maps. I would then tape these pages together in order to form a wall size map of the area. The last time I did this was in Turku, Finland, around 1999.

Since then the online maps have first slowly, then rapidly evolved in to what they are today. In the middle of the previous decade, when the word beta started to appear along with Web 2.0, the maps had evolved to a point where the location information could have been stored in a XML file, namely KML.

Since that time, XML has proven to be good, but there have risen better options, such as JSON, which is usually smaller in file size with the same textual data as XML.

This story is about transforming those KML files to GeoJSON. The latter can then be used with map loader such as Leaflet or put in a GitHub repository where the given GeoJSON will be rendered as a map.

The example commands will assume that Node.js and its Node Packaged Module manager (npm) have been installed and are available in the path.

Begin by installing the conversion tool called toGeoJSON.

sudo npm install -g togeojson

Once installed the conversion can be done with a command similar to:

togeojson Japan.April.2010.Places.kml > Japan.April.2010.Places.json

In the case of compressed KML files, which usually have the suffix kmz, they should be uncompressed first.

As for the size comparison, below is a table containing some of the compressed KML files that were tested with the conversion. All of the given KML files are available in one of the Japan adventure maps.

Comparison with the maximum compression level was added to reflect the final size difference.

File KMZ KML GeoJSON gzip –best
Japan.April.2010.Places 8.85 KB 44.4 KB 28.2 KB 4.9 KB
Jogging2010-01-14_20-05 18.6 KB 73.2 KB 357 KB 21.2 KB
RKHSK.Japan.Dojo 2.9 KB 9.8 KB 7.3 KB 1.9 KB
53-stations-of-the-tokaido 459 KB 1490 KB 171 KB
Castles-in-Japan 24.5 KB 25.5 KB 7.9 KB
Japan2011 18.1 KB 15.5 KB 4.2 KB
Japan-Accommodations-2011 9.1 KB 6.1 KB 1.4 KB
Museums 9.5 KB 5.9 KB 1.9 KB
Naginata 6.2 KB 2.9 KB 1.0 KB
Prefectual-Budoukan 15.8 KB 13.1 KB 2.1 KB
RKHSK-Japan-Dojo-2011 11.8 KB 7.4 KB 1.5 KB
Temples-and-Shrines 18.2 KB 16.4 KB 4.8 KB

As it can be seen, in some cases the resulting GeoJSON is somewhat smaller than the KML, but in some cases, it is quite the opposite, with significant increase of the file size.

As GeoJSON is becoming slowly more popular, also tooling for it is getting better. Recently published is a handy tool for creating and editing JSON files with this format. It was created by Tom MacWright and its source is available at GitHub.