Bash Scripting KMZ – Merging Different KMZ Layers into a Single File

bashkmlkmz

I have different .kmz files, each one contains a .png and a .kml, let's say:
a.kmz, b.kmz, c.kmz.
I would like to use a bash script to obtain a unique .kmz file, let's say d.kmz, that contains all the previous layers.
I simply tried to compress the .kml files and call the result d.kmz, but it does not work.

Best Answer

As you know, a KMZ file is a zipped version of a KML file, where the KML is compressed (very helpful for large datasets), and media files such as icon images, overlay images, balloon images, sound files, 3d models and others can be included in the KMZ's zip archive.

When Earth opens a KMZ file, it looks in the KMZ for ONE KML file. Usually there is just one. The convention is to name it doc.kml, but the name isn't important. If it finds more than one, then it will open just one of them. This is likely the issue you're running into by simply combining all the original KML files into one KMZ.

You'll need to actually combine the KML files into one KML, and then zip that up with the PNGs into your KMZ file. This can be done with a variety of software packages, from geospatial processing tools to basic XML tools (since KML is based on XML). If you use an XML tool, you'll probably need to have it open up each KML file, extract the content, and then recombine the contents from each KML into a new KML with the correct KML tags as a wrapper. The "content" you extract can either be the top level container (Document or Folder), in which case you'll need to put them inside a single container in the new file... or you can extract the contents out of that top level container (eg: Placemarks, overlays, etc.), and write those into the new file inside a new top level container.

Alternatively, as mentioned in one of the comments, if you just need to do this once or a few times, you can easily let Google Earth Pro do it for you, by opening your existing KMZ files in Earth, Making a new Folder in your My Places list, dragging the contents of the KMZs into that folder, and then saving the folder out as a new KMZ file.

Good luck.

Related Question