I have created a KMZ file by using the below program, In my maven project I have created a folder called files
under project folder, I have added an image called grn-pushpin.png
in the files folder.
In my program while creating the KMZ I have passed my image as below
FileInputStream is = new FileInputStream("files/grn-pushpin.png");
ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png");
While showing the point image in KML, I have given like ps.println("<Icon><href>files/grn-pushpin.png</href></Icon>");
Now it is showing the image but it seems it is showing from the local folder only.
How can I make sure that the image is coming from KMZ file?
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import java.io.*;
public class TestKmz {
public static void main(String[] args) throws IOException {
createKMZ();
System.out.println("file out.kmz created");
}
public static void createKMZ() throws IOException {
FileOutputStream fos = new FileOutputStream("out.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("<Placemark>");
// add reference to image via inline style
ps.println(" <Style><IconStyle>");
ps.println(" <Icon><href>files/grn-pushpin.png</href></Icon>");
ps.println(" </IconStyle></Style>");
ps.println(" <Point><coordinates>72.877460,19.144808</coordinates></Point> ");
ps.println("</Placemark>");
ps.println("</kml>");
ps.flush();
zoS.closeEntry(); // close KML entry
// now add image file entry to KMZ
FileInputStream is = null;
try {
is = new FileInputStream("files/grn-pushpin.png");
ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png");
zoS.putNextEntry(zEnt);
// copy image input to KMZ output
// write contents to entry within compressed KMZ file
IOUtils.copy(is, zoS);
} finally {
IOUtils.closeQuietly(is);
}
zoS.closeEntry();
zoS.close();
}
}
I have remove the following lines of code, still I am able to see the image it means it is loading from the folder only, it is not reading from KMZ file
is = new FileInputStream("files/grn-pushpin.png");
ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png");
Google Earth Pro (GEP) first looks for files referenced as relative URIs (e.g. files/grn-pushpin.png) inside KMZ file and uses it if available.
If image reference is not found as an entry within the KMZ file then GEP will look for those files in local file system relative to the KMZ file using the same path so if the kmz file out.kmz is located in C:/path/data/
then it would look for the image relative to that folder in C:/path/data/files/grn-pushpin.png
.
To verify the referenced files inside the KMZ are being loaded, move the out.kmz file to a different folder (e.g. Desktop) and open it in Google Earth.