- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
有没有办法使用 Java API for KML (JAK) 将图像文件添加到 kmz 文件中?我可以毫无问题地创建一个 kml 文件,但我只是想嵌入一个资源(例如带有一些图像文件的图像文件夹),但是 marshalAsKmz 方法只将 Kml 对象作为附加文件,所以我想不通了解如何只包含额外的图像。
最佳答案
我已经在一个项目中使用 JAK 一年多了。我用它来创建 KML,然后将其编码为普通 KML(而非 KMZ)。我创建了一个单独的实用程序类,它使用 Java SE“Zip”类来手动创建 KMZ。它工作得很好。一个 KMZ 只不过是一个 .zip 文件,它只包含一个 .kml 文件和 0 个或多个资源文件(例如图像等)。唯一的区别是在输出文件时将文件命名为 .kmz 而不是 .zip。在 KML 文档中 <Style>
定义,请使用相对于 KML 文档本身的路径引用您的资源文件。 KML 文件被认为位于 KMZ 存档的“根目录”。如果您的资源文件也位于 KMZ (.zip) 的根目录中,那么您不需要路径,只需要文件名。
编辑: 我实际上忘记了我删除了编码 JAK Kml
的中间步骤在我压缩文件之前反对文件。我下面的实用程序方法将编码 Kml
直接反对 ZipOutputStream
.
这是我创建的一个实用程序类,它的作用正是我所描述的。我将其发布在这里是希望其他人发布仅使用 JAK 的替代解决方案,以便我将来可以淘汰此代码。现在,这将为您完成这项工作。
注意:如果您不使用 slf4j、Apache Commons Lang 或 Commons I/O,则只需对代码进行一些调整,用您自己的代码删除/替换这些位。显然这段代码需要 JAK 库。
package com.jimtough;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.micromata.opengis.kml.v_2_2_0.Kml;
/**
* Uses the classes in java.util.zip to package a KML file and its
* supplementary files as a ZIP-compressed KMZ file.
*
* @author JTOUGH
*/
public final class KMZPackager {
private final static Logger logger =
LoggerFactory.getLogger(KMZPackager.class);
/**
* Container for data that represents a single entry that will be added
* to a compressed archive.
* @author JTOUGH
*/
public static abstract class DataSource {
protected String archivedFileName;
/**
* Write the contents of this data source to the supplied
* zip output stream.
*
* @param zipOutputStream
* @throws IOException
*/
public abstract void writeToStream(ZipOutputStream zipOutputStream)
throws IOException;
}
/**
* Container for data that represents a single file that will be added
* to a compressed archive.
* @author JTOUGH
*/
public static final class FileDataSource extends DataSource {
private File sourceFile;
/**
* Constructor
*
* @param sourceFile Actual file that will be added to the
* compressed archive.
* @param archivedFileName Name that will be assigned to the compressed
* file within the archive. Caller must ensure that this value is
* unique within the archive otherwise an exception will be thrown
* when a name clash occurs during creation of the archive.
* This string must be non-null and non-empty. Any forward-slash
* characters in the string will be treated as directory separators
* when the KMZ/ZIP archive is created.
* @throws IllegalArgumentException If either of these parameters
* is a null reference
*/
public FileDataSource(
File sourceFile,
String archivedFileName)
throws IllegalArgumentException {
Validate.notNull(sourceFile);
Validate.notEmpty(archivedFileName);
this.sourceFile = sourceFile;
this.archivedFileName = archivedFileName;
}
@Override
public void writeToStream(ZipOutputStream zipOutputStream)
throws IOException {
Validate.notNull(zipOutputStream);
// Check that the file exists, and throw an appropriate exception
// before reading it
if (!sourceFile.exists()) {
throw new IllegalArgumentException(
"File referenced in parameter [" +
sourceFile.getAbsolutePath() + "] does not exist");
}
FileInputStream fis = new FileInputStream(sourceFile);
if (logger.isDebugEnabled()) {
logger.debug("Adding file to KMZ archive" +
" | archive name: " + archivedFileName +
" | original name: " +
sourceFile.getCanonicalPath());
}
// Mark the start of this new file in the ZIP stream
ZipEntry entry = new ZipEntry(archivedFileName);
zipOutputStream.putNextEntry(entry);
// Use the Apache commons-io library to do a buffered
// stream-to-stream copy
try {
IOUtils.copy(fis, zipOutputStream);
} finally {
fis.close();
}
}
}
/**
* Container for a single JAK Kml object that will be marshalled
* directly to a compressed KMZ archive as it is created
* @author JTOUGH
*/
public static final class KMLDataSource extends DataSource {
private Kml kml;
/**
* Constructor
*
* @param kml JAK Kml object that will be marshalled directly to the
* compressed archive.
* @param archivedFileName Name that will be assigned to the compressed
* file within the archive. Caller must ensure that this value is
* unique within the archive otherwise an exception will be thrown
* when a name clash occurs during creation of the archive.
* This string must be non-null and non-empty. Any forward-slash
* characters in the string will be treated as directory separators
* when the KMZ/ZIP archive is created.
* @throws IllegalArgumentException If either of these parameters
* is a null reference
*/
public KMLDataSource(
Kml kml,
String archivedFileName)
throws IllegalArgumentException {
Validate.notNull(kml);
Validate.notEmpty(archivedFileName);
this.kml = kml;
this.archivedFileName = archivedFileName;
}
@Override
public void writeToStream(ZipOutputStream zipOutputStream) throws IOException {
Validate.notNull(zipOutputStream);
// Mark the start of this new file in the ZIP stream
ZipEntry entry = new ZipEntry(archivedFileName);
zipOutputStream.putNextEntry(entry);
// Marshal the Kml object directly to the ZipOutputStream
if (logger.isDebugEnabled()) {
logger.debug("Marshalling KML to KMZ archive" +
" | archive name: " + archivedFileName);
}
kml.marshal(zipOutputStream);
}
}
/**
* Container for a stream holding a Kml document. This will be written
* directly to a compressed KMZ archive as it is created.
*/
public static final class StreamDataSource extends DataSource {
private InputStream inputStream;
/**
* Constructor
*
* @param inputStream the inputStream holding the KML text
* @param archivedFileName Name that will be assigned to the compressed
* file within the archive. Caller must ensure that this value is
* unique within the archive otherwise an exception will be thrown
* when a name clash occurs during creation of the archive.
* This string must be non-null and non-empty. Any forward-slash
* characters in the string will be treated as directory separators
* when the KMZ/ZIP archive is created.
* @throws IllegalArgumentException If either of these parameters
* is a null reference
*/
public StreamDataSource(
InputStream inputStream,
String archivedFileName)
throws IllegalArgumentException {
Validate.notNull(inputStream);
Validate.notEmpty(archivedFileName);
this.inputStream = inputStream;
this.archivedFileName = archivedFileName;
}
@Override
public void writeToStream(ZipOutputStream zipOutputStream) throws IOException {
Validate.notNull(zipOutputStream);
// Mark the start of this new file in the ZIP stream
ZipEntry entry = new ZipEntry(archivedFileName);
zipOutputStream.putNextEntry(entry);
// Use the Apache commons-io library to do a buffered
// stream-to-stream copy
if (logger.isDebugEnabled()) {
logger.debug("Copying KML from stream to KMZ archive" +
" | archive name: " + archivedFileName);
}
try {
IOUtils.copy(inputStream, zipOutputStream);
} finally {
inputStream.close();
}
}
}
/**
* Use ZIP compression to package a KML file and its supplementary files
* as a KMZ archive. The compressed archive will be written to the
* supplied output stream.
*
* @param os OutputStream to which the compressed archive will be written.
* This parameter must be a non-null reference to an OutputStream that
* is open for write operations.
* @param kmlDataSource KML to be added to the compressed archive.
* This parameter must not be null. The archivedFileName attribute must
* end with the .kml extension. The file is added to the compressed
* archive. The KMZ specification states that a KMZ archive must only
* contain a single KML file.
* @param supplementaryFileList List of file locations for supplementary
* files that will be included in the KMZ archive. A common example
* would be icons or image overlays that are referenced in the KML file.
* This parameter can be null or an empty list if there are no
* supplementary files to include in the KMZ. Each source that is included
* in the list must refer to an existing file that does NOT have the
* file extension '.kml'.
* @throws RuntimeException Thrown if anything unexpected occurs
* that prevents execution from continuing or if any of the stated
* conditions for the input parameters are violated
*/
public void packageAsKMZ(
OutputStream os,
DataSource kmlDataSource,
List<FileDataSource> supplementaryFileList)
throws RuntimeException {
ZipOutputStream zipOutputStream = null;
boolean isExceptionThrown = false;
Exception caughtException = null;
List<FileDataSource> supplFileList = supplementaryFileList;
try {
Validate.notNull(os, "os parameter cannot be null");
Validate.notNull(kmlDataSource,
"kmlFileDataSource parameter cannot be null");
// Treat a null parameter just like an empty list (which is OK)
if (supplFileList == null) {
supplFileList = Collections.emptyList();
}
if (logger.isDebugEnabled()) {
logger.debug(
"Creating KMZ archive" +
" | supplementary files: " + supplFileList.size());
}
// Create a buffered output stream for the new KMZ file
zipOutputStream = new ZipOutputStream(new BufferedOutputStream(os));
Validate.isTrue(
kmlDataSource.archivedFileName.endsWith(".kml"),
"KML archived file name must end with .kml");
kmlDataSource.writeToStream(zipOutputStream);
// Now process the list of supplementary files
if (logger.isDebugEnabled()) {
logger.debug("Adding supplementary files to KMZ archive" +
" | archive name: ");
}
for (FileDataSource ds : supplFileList) {
Validate.isTrue(
!ds.archivedFileName.endsWith(".kml"),
"Not legal to include .kml files in supplementary list");
ds.writeToStream(zipOutputStream);
}
// Close the output stream to complete the ZIP creation
zipOutputStream.flush();
zipOutputStream.close();
logger.info("KMZ archive created successfully");
} catch (IOException e) {
isExceptionThrown = true;
caughtException = e;
logger.error("IOException while creating ZIP stream");
} catch (IllegalArgumentException e) {
isExceptionThrown = true;
caughtException = e;
} catch (RuntimeException e) {
isExceptionThrown = true;
caughtException = e;
} finally {
if (isExceptionThrown) {
try {
if (zipOutputStream != null) {
zipOutputStream.close();
}
} catch (IOException ioe) {
// Don't care
}
throw new RuntimeException(caughtException);
}
}
}
}
关于KML (JAK) 的 Java API 在 kmz 文件中嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7265808/
我已经设置了 Azure API 管理服务,并在自定义域上配置了它。在 Azure 门户中 API 管理服务的配置部分下,我设置了以下内容: 因为这是一个客户端系统,我必须屏蔽细节,但以下是基础知识:
我是一名习惯 React Native 的新程序员。我最近开始学习 Fetch API 及其工作原理。我的问题是,我找不到人们使用 API key 在他们的获取语句中访问信息的示例(我很难清楚地表达有
这里有很多关于 API 是什么的东西,但是我找不到我需要的关于插件 API 和类库 API 之间的区别。反正我不明白。 在 Documenting APIs 一书中,我读到:插件 API 和类库 AP
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我正在尝试找出设计以下场景的最佳方法。 假设我已经有了一个 REST API 实现,它将从不同的供应商那里获取书籍并将它们返回给我自己的客户端。 每个供应商都提供单独的 API 来向其消费者提供图书。
请有人向我解释如何使用 api key 以及它有什么用处。 我对此进行了很多搜索,但得到了不同且相互矛盾的答案。有人说 API key 是保密的,它从不作为通信的一部分发送,而其他人则将它发送给客户端
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
谁能告诉我为什么 WSo2 API 管理器不进行身份验证?我已经设置了两个 WSo2 API Manager 1.8.0 实例并创建了一个 api。它作为原型(prototype) api 工作正常。
我在学习 DSL 的过程中遇到了 Fluent API。 我在流利的 API 上搜索了很多……我可以得出的基本结论是,流利的 API 使用方法链来使代码流利。 但我无法理解——在面向对象的语言中,我们
基本上,我感兴趣的是在多个区域设置 WSO2 API 管理器;例如亚洲、美国和欧洲。一些 API 将部署在每个区域的数据中心内,而其他 API 将仅部署在特定区域内。 理想情况下,我想要的是一个单一的
我正在构建自己的 API,供以下用户使用: 1) 安卓应用 2) 桌面应用 我的网址之一是:http://api.chatapp.info/order_api/files/getbeers.php我的
我需要向所有用户显示我的站点的分析,但使用 OAuth 它显示为登录用户配置的站点的分析。如何使用嵌入 API 实现仪表板但仅显示我的网站分析? 我能想到的最好的可能性是使用 API key 而不是客
我正在研究大公司如何管理其公共(public) API。我想到的是拥有成熟 API 的公司,例如 Google、Facebook、Twitter 和 Amazon。 这些公司向公众公开了许多不同的 A
在定义客户可访问的 API 时,以下是首选的行业惯例: a) 定义一组显式 API 方法,每个方法都有非常狭窄和特定的目的,例如: SetUserName SetUserAge Se
这在本地 deserver 和部署时都会发生。我成功地能够通过留言簿教程使用 API 资源管理器,但现在我已经创建了自己的项目并尝试访问我编写的第一个 API,它从未出现过。搜索栏旁边的黄色“正在加载
我正在尝试使用 http://ip-api.com/ api通过我的ip地址获取经度和纬度。当我访问 http://ip-api.com/json从我的浏览器或使用 curl,它以 json 格式返回
这里的典型示例是 Twitter 的 API。我从概念上理解 REST API 的工作原理,本质上它只是针对您的特定请求向他们的服务器查询,然后您会在其中收到响应(JSON、XML 等),很棒。 但是
我能想到的最好的标题,但要澄清的是,情况是这样的: 我正在开发一种类似短 url 的服务,该服务允许用户使用他们的 Twitter 帐户“登录”并发布内容。现在这项服务可以包含在 Tweetdeck
我正在设计用于管理评论和讨论线程的 API 方案。我想有一个点 /discussions/:discussionId 当您GET 时,它会返回一组评论和一些元数据。评论也许可以单独访问 /discus
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭去年。 Improve this quest
我是一名优秀的程序员,十分优秀!