【问题标题】:showing image at point in kmz在 kmz 点显示图像
【发布时间】:2020-06-09 05:08:54
【问题描述】:

我使用以下程序创建了一个 KMZ 文件,在我的 maven 项目中,我在项目文件夹下创建了一个名为 files 的文件夹,我在文件夹中添加了一个名为 grn-pushpin.png 的图像。

在我的程序中创建 KMZ 时,我传递了我的图像,如下所示

FileInputStream  is = new FileInputStream("files/grn-pushpin.png");
ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png"); 

在 KML 中显示点图像时,我给出了 ps.println("<Icon><href>files/grn-pushpin.png</href></Icon>"); 现在它正在显示图像,但它似乎只从本地文件夹显示。

如何确保图片来自 KMZ 文件?

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();
    }   
}   

我删除了以下代码行,但我仍然可以看到图像,这意味着它仅从文件夹加载,而不是从 KMZ 文件中读取

 is = new FileInputStream("files/grn-pushpin.png");
 ZipEntry zEnt = new ZipEntry("files/grn-pushpin.png");

【问题讨论】:

  • GE Pro 将首先在 KMZ 中查找文件并使用这些文件(如果可用),否则将使用相同路径(例如 files/grn-pushpin.png)在相对于 KMZ 文件的本地文件系统中查找这些文件)。将 KMZ 文件移动到不同的文件夹,以验证 KMZ 中的图像是否正在被使用。
  • @JasonM1 感谢您的回复,它仅从 KMZ 获取图像...

标签: java file kml java-io kmz


【解决方案1】:

Google 地球专业版 (GEP) 首先在 KMZ 文件中查找引用为相对 URI 的文件(例如 files/grn-pushpin.png),并在可用时使用它。

如果在 KMZ 文件中未找到图像引用作为条目,则 GEP 将使用相同路径在相对于 KMZ 文件的本地文件系统中查找这些文件,因此如果 kmz 文件 out.kmz 位于 C:/path/data/然后它将在C:/path/data/files/grn-pushpin.png 中查找与该文件夹相关的图像。

要验证 KMZ 中的引用文件是否正在加载,请将 out.kmz 文件移动到其他文件夹(例如桌面)并在 Google 地球中打开它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多