Making KML files smaller by reducing precision

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.






PLEASE NOTE: Google Earth Blog is no longer writing regular posts. As a result, we are not accepting new comments or questions about Google Earth. If you have a question, use the official Google Earth and Maps Forums or the Google Earth Community Forums.

Comments

  1. 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)

    • I’ve been taking little chunks out of one test file and retrying it – the bit that seemd to make it work (well, it was almost the last bit I removed) was this:

      so I went back to the original and removed that one line and your script was finally able to work.
      Hope this helps

      • ok, so your page is stripping out the markup – here goes again
        it was ‘left caret’ then Snippet maxLines=”0″ ‘right caret’, ‘left caret’ /Snippet ‘right caret’

    • Timothy Whitehead says:

      For very large files try both Chrome and Firefox as I have found in the past that they have different limits with regards to the maximum size of XML file they can handle.

  2. Hi I tried to reduce the file and reduced it successfully but now my application is not recognizing the columns . I am using Qlik Sense for this

  3. I browsed my file, set the precision and then click “Reduce Precision” but nothing happened. Have I done anything wrong? My KML file is only 25 MB. Thanks.

    • Timothy Whitehead says:

      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.

  4. Hi Thanks It made a big difference to the file size

  5. HI It makes the file size smaller but after doing this operation , the application doesn’t recognize the columns? What am I doing wrong ?

  6. gino2780 says:

    Hello Timothy,

    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!!

    • Andre Costa says:

      @gino2780 Can you post a image of the map you developed? I’m trying to use kml with QlikSense too but I get the “Hypercube results too large” error.

  7. Katia Beaty says:

    What would cause it to create a an empty KML file? The file I have is only 33kb and I need to make it smaller.

  8. Thanks a lot! used for Qlik Sense Russian Federation KML size reduce. Didn’t work in Safari, but worked fine in Chrome for mac.



PLEASE NOTE: Google Earth Blog is no longer writing regular posts. As a result, we are not accepting new comments or questions about Google Earth. If you have a question, use the official Google Earth and Maps Forums or the Google Earth Community Forums.