Kmz for google earth images with java

Does anyone know which compression to use in Java to create KMZ files in which images are stored? I tried using standard Java compression (and various modes, BEST_COMPRESSION, DEFAULT_COMPRESSION, etc.), but my compressed file and kmz file always look a bit different, they don’t load on Google Earth. It looks like my png images in particular (the actual kml file seems to be compressed in the same way).

Has anyone successfully created a kmz archive that references local images (and is stored in a file directory) from outside of google earth?

thanks

Jeff

+4
source share
6 answers

The key to understanding this is the answer from @fraser, which is supported by this snippet from support for KML developers:

The supported compression method is ZIP (compatible with PKZIP), so neither gzip nor bzip will work. KMZ files compressed by this method are fully supported by the API.

KMZ in the Google Earth API and Unix KML Compression

Apache Commons has an archive processing library that is convenient for this: http://commons.apache.org/proper/commons-vfs/filesystems.html

+1
source

KMZ is just a zip file with a KML file and assets. For example, the london_eye.kmz kmz file contains:

  $ unzip -l london_eye.kmz Archive: london_eye.kmz Length Date Time Name -------- ---- ---- ---- 451823 09-27-07 08:47 doc.kml 0 09-26-07 07:39 files/ 1796 12-31-79 00:00 files/Blue_Tile.JPG 186227 12-31-79 00:00 files/Legs.dae 3960 12-31-79 00:00 files/Olive.JPG 1662074 12-31-79 00:00 files/Wheel.dae 65993 12-31-79 00:00 files/Wooden_Fence.jpg 7598 12-31-79 00:00 files/a0.gif 7596 12-31-79 00:00 files/a1.gif 7556 12-31-79 00:00 files/a10.gif 7569 12-31-79 00:00 files/a11.gif 7615 12-31-79 00:00 files/a12.gif 7587 12-31-79 00:00 files/a13.gif 7565 12-31-79 00:00 files/a14.gif 7603 12-31-79 00:00 files/a15.gif 7599 12-31-79 00:00 files/a16.gif 7581 12-31-79 00:00 files/a17.gif 7606 12-31-79 00:00 files/a18.gif 7613 12-31-79 00:00 files/a19.gif 7607 12-31-79 00:00 files/a2.gif 7592 12-31-79 00:00 files/a3.gif 7615 12-31-79 00:00 files/a4.gif 7618 12-31-79 00:00 files/a5.gif 7618 12-31-79 00:00 files/a6.gif 7578 12-31-79 00:00 files/a7.gif 7609 12-31-79 00:00 files/a8.gif 7603 12-31-79 00:00 files/a9.gif 57185 12-31-79 00:00 files/capsule.dae 310590 12-31-79 00:00 files/groundoverlay.jpg 224927 12-31-79 00:00 files/mechanism.dae 160728 12-31-79 00:00 files/shadowoverlay.jpg 33044 12-31-79 00:00 files/shed.dae -------- ------- 3310275 32 files 

You can build this with java.util.zip or even with jar if you want.

As for images, they should not be compressed, as they already contain compressed data. You do not get significant savings.

+1
source

By default, the ZipOutputStream class in Java will create a compatible KMZ file that Google Earth can read.

In ZipEntry, you can specify either the STORED compression or the DEFLATED method, both of which are compatible with Google Earth.

  • Pay attention to any ZIP library or API that you use, you should definitely specify ZIP 2.0 or "obsolete" compression methods (ie STORED and DEFLATE methods) if they are not standard methods. The DEFLATE method is called SuperFast and STORED is called None or "No Compression" in the WinZip Documentation .
  • Google Earth also supports the maximum or advanced deflation method, which is often displayed with the short name " Def: X ".
  • More advanced compression methods (e.g. bzip2, LZMA, etc.) are NOT compatible with Google Earth, and such KMZ files will be silently ignored when opened.

Here is a simple piece of code to create a KMZ file in Java.

  FileOutputStream fos = new FileOutputStream("example.kmz"); ZipOutputStream zoS = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry("doc.kml"); zoS.putNextEntry(ze); PrintStream ps = new PrintStream(zoS); ps.println("<?xml version='1.0' encoding='UTF-8'?>"); ps.println("<kml xmlns='http://www.opengis.net/kml/2.2'>"); // write out contents of KML file ... ps.println("<Document>"); ps.println("<Placemark>"); // ... ps.println("</Placemark>"); ps.println("</Document>"); ps.println("</kml>"); ps.flush(); zoS.closeEntry(); // close KML entry // include and write other files (Eg icons, overlays, other KML files, etc.) zoS.close(); 
+1
source

Of course, I have Kmz batch files with images in C #. AFAIK is the only compression method supported by ZIP (PKZIP compatible). Which library do you use Java in?

0
source

There is a library for working with KML in Java called JAK (Java API for KML).

Unfortunately, it seems to have an error: Problem 1: saving the KMZ file does not work - so it looks like you are not the first one to have problems creating the KMZ file ...

0
source

As simsong said, KMZ just buttoned KML. One thing I noticed is that doc.kml must be the first entry in the zip file in order for it to work reliably. I don’t remember to do something special with images (except putting everything except doc.kml in a subdirectory). My KMZ files are created using java.util.zip.

0
source

All Articles