Making KML files smaller by reducing precision

March 15, 2016

Our KML map of 3D areas has been steadily growing in size as new areas are added. We have considered converting it to KMZ format, which is the compressed version of KML files. KMZ uses standard ZIP compression and in many cases provides significant compression ratios. In our case it would shrink our KML file by over 60%. We have held back because this would mean changing the link to the file and as most users access it via a network linked KML we would also have to notify them to re-download the original KML with a new link. There are workarounds to this problem, so it might be worth doing. The other downside is that it adds an extra step to the process of updating the file as we would have to convert it to a KMZ file before uploading it.

Because of the above complications we have been giving some thought to other methods of making the file smaller. One thing we have noticed is that Google Earth saves KML files with an unnecessary amount of precision for latitudes and longitudes. It typically saves latitudes and longitudes using 16 significant digits. If you configure Google Earth to use decimal degrees then edit a placemark in Google Earth, it only actually shows 5 decimal places.

So how many decimal places are actually needed? We calculated that rounding to five decimal places results in a maximum error of just under a metre. Rounding to six decimal places gives an error of less than 10 centimetres. 14 decimal places gives an accuracy of less than a nanometre. Given that most Google Earth data such as altitude data or even image alignment can be off by many metres, having nanometre resolution in your KMLs is just wasting space. For our KML file five decimal places should be more than enough.

As you can see below, the results were quite impressive. Simply discarding unnecessary decimal places reduces the file size by almost half. We also show the effects of using the KMZ format.

We thought that other people with large KML files might find the idea useful. So, below is a JavaScript tool that will take a KML file and reduce the precision of the latitudes and longitudes.

Notes:

The current version only reduces precision in placemarks, polygons and lines, it does not affect some other KML elements that include latitudes and longitudes.

To achieve the reduction of precision the code parses the KML and recreates it using our in-development KML library, so we can’t guarantee that it will properly handle unusual KML elements. If you use it, double-check that the resulting file still has everything in it that you require, and let us know in the comments if you encounter any bugs.

Unnecessary white-space is also removed which makes the file smaller but less human-readable.

Precision: (decimal places)

About Timothy Whitehead

Timothy has been using Google Earth since 2004 when it was still called Keyhole before it was renamed Google Earth in 2005 and has been a huge fan ever since. He is a programmer working for Red Wing Aerobatx and lives in Cape Town, South Africa.

Comments

i tried it on my very large (200MB) MyPlaces kml and it unsurprisingly failed (there’s so much junk in it!) but I tried some smaller files (short paths that I’ve collected that I intend to check out one day) and some work and some don’t (sticking on the ‘Parsing’ prompt). You’re welcome to one to test (about 1.85KB)

It can happen if there are elements in the KML that my KML library doesn’t know what to do with. It would be helpful if you press F12 and let me know about any error messages that show in the browsers console.

i’ve been using kml files for Qlik Sense. Unfortunately the developers have put a polygon cap for rendering of heatmaps. With your little workaround i managed to successfully reduce the kml file that the rendering of heatmaps is now working perfectly again. Thanks for your work and the description above with regards to the kml vs. kmz comparison. Highly appreciated!!

This blog and its author are not an official source of information from Google that produces and owns Google Earth
Google and Google Earth are trademarks of Google Inc..
All image screenshots from Google Earth are Copyright Google.
All other trademarks appearing here are the trademarks of their respective owners.