前言:最近在做kmz图层导入到平台的功能。
思路:
①上传KMZ文件。
②而后进一步将其KMZ文件转换成KML文件。
③加载kml文件至地图显示。
备忘:此处只记录将其KMZ文件转换成KML文件的工作点。
准备工作:需要先准备一份kmz文件,kmz文件可以从以此路径(KMZ转kml示例文件-Java文档类资源-CSDN下载)地址进行下载。
代码部分
1.依赖
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
2.KMZ转KML
public Document unzipKmzToKml() throws Exception, Exception {
String strkmz="D:/atlas/测试数据_LayerToKMZ.kmz";
System.out.println("********************** 【KMZ转kml开始】kmz路径: **********************\n"+ strkmz);
File file = new File(strkmz);
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
InputStream inputStream = null;
ZipEntry entry = null;
Document doc = null;
while ((entry = zipInputStream.getNextEntry()) != null) {
String zipEntryName = entry.getName();
//获取所需文件的节点
if (zipEntryName.equals("doc.kml")) {
inputStream = zipFile.getInputStream(entry);
SAXReader reader = new SAXReader();
doc = reader.read(inputStream);
inputStream.close();
}
}
zipFile.close();
zipInputStream.close();
return doc;
}
Document unzipKmzToKml = this.unzipKmzToKml();
// 将dom4j 的document对象转换成String
// String asXML = unzipKmzToKml.asXML();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = new Date();
String fileName = sdf.format(date);
String strkml = "D:/atlas/shengchengKML/测试数据_LayerToKMZ_1.kml";
// 创建kml到本地
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(strkml),format);
xmlWriter.write(unzipKmzToKml);
xmlWriter.close();
System.out.println("\n********************** 【KMZ转kml成功】kml路径: **********************\n"+ strkml);
这样就在"D:/atlas/shengchengKML/"中生成了一个kml文件,然后就可以进行下一步操作了!!!