Java API for KML (JAK) 在 kmz 文件中嵌入图像
有没有办法使用 Java API for KML (JAK) 将图像文件添加到 kmz 文件中?我可以毫无问题地创建一个 kml 文件,但我试图嵌入一个资源(例如带有一些图像文件的图像文件夹),但 marshalAsKmz 方法仅将 Kml 对象作为附加文件,所以我无法理解了解如何仅包含额外的图像。
Is there a way to just add an image file into a kmz file using Java API for KML (JAK)? I can create a kml file with no problem, but I'm trying to just embed a resources (such as an images folder with some image files), but the marshalAsKmz method takes only Kml objects as additional files, so I can't figure out how to just include extra images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在一个项目中使用 JAK 已有一年多了。我使用它来创建 KML,然后将其编组为普通 KML(而不是 KMZ)。我创建了一个单独的实用程序类,它使用 Java SE“Zip”类来手动创建 KMZ。它工作得很好。 KMZ 只不过是一个 .zip 存档,其中仅包含一个 .kml 文件和 0 个或多个资源文件(例如图像等)。唯一的区别是输出时将文件命名为 .kmz 而不是 .zip。在 KML 文档
定义中,使用相对于 KML 文档本身的路径引用资源文件。 KML 文件被认为位于 KMZ 存档的“根”处。如果您的资源文件也位于 KMZ (.zip) 的根目录中,那么您不需要路径,只需要文件名。
编辑: 我实际上忘记了在压缩 JAK
Kml
对象之前删除了将其编组到文件的中间步骤。下面的实用程序方法会将Kml
对象直接编组到ZipOutputStream
。这是我创建的一个实用程序类,用于执行我所描述的操作。我将其发布在这里是希望其他人发布仅使用 JAK 的替代解决方案,以便我将来可以废弃此代码。现在,这将为您完成这项工作。
注意:如果您不使用 slf4j、Apache Commons Lang 或 Commons I/O,则只需对代码进行一些调整即可删除/用您自己的代码替换这些位。显然这段代码需要JAK库。
I've been using JAK for over a year on a project. I use it to create the KML, then I marshal it as just plain KML (not KMZ). I created a separate utility class that uses the Java SE 'Zip' classes to manually create the KMZ. It works just fine. A KMZ is nothing more than a .zip archive that contains exactly one .kml file and 0 or more resource files (such as images, etc). The only difference is that you name the file as .kmz instead of .zip when you output it. In the KML document
<Style>
definitions, refer to your resources files with paths relative to the KML document itself. The KML file is considered to be at the 'root' of the KMZ archive. If your resource files are also located in the root of the KMZ (.zip) then you don't need a path, just the filename.EDIT: I actually forgot that I removed the intermediate step of marshalling the JAK
Kml
object to a file before I zip it. My utility method below will marshal theKml
object directly to theZipOutputStream
.Here is a utility class that I created to do just what I have described. I'm posting it here in the hope that someone else posts an alternate solution that uses only JAK so I can retire this code in the future. For now, this will do the job for you.
NOTE: If you don't use slf4j, Apache Commons Lang or Commons I/O then just make a few adjustments to the code to remove/replace those bits with your own code. Obviously this code requires the JAK library.
Java 附带了一个默认的 zip 实用程序,您所需要做的就是传递需要压缩的 kml 文件以及图像文件列表。这两个函数应该可以解决问题。
Java comes with a default zip utility, all you need to do is to pass the kml file you need to zip along with the list of image files. These two functions should do the trick.