java生成航线kml文件-压缩zip
直接看代码
/**
* 生成航线kml文件
*
* @param airlineManage 航线信息
* @return String 临时存储文件名
*/
public AirlineManage setTravelsKml(AirlineManage airlineManage) throws IOException {
Element root = DocumentHelper.createElement("kml");
Document document = DocumentHelper.createDocument(root);
Namespace namespace = Namespace.get("http://www.opengis.net/kml/2.2");
root.addAttribute("xmlns", "http://www.opengis.net/kml/2.2");
root.add(namespace);
Element documentElement = root.addElement("Document");
documentElement.addElement("name").addText(airlineManage.getAirlineName()); //名称
Element placeMarkElement = documentElement.addElement("Placemark");//在文件夹中添加一个地标
placeMarkElement.addElement("name").addText("Wayline");
placeMarkElement.addElement("visibility").addText("1");
Element styleElement = placeMarkElement.addElement("Style");
Element lineStyleElement = styleElement.addElement("LineStyle");
lineStyleElement.addElement("color").addText("cc00ffff");
lineStyleElement.addElement("width").addText("5");
Element extendedData = placeMarkElement.addElement("ExtendedData");
Element data = extendedData.addElement("Data");
data.addAttribute("name", "LinePattern");
data.addElement("value").addText("65535");
// 航向间距 计算速度
CameraInfo cameraInfo = cameraInfoMapper.selectCameraInfoByCameraId(airlineManage.getCameraId());
double courseInterval = DrawWayPointUtil.calcNonOverlapDistance(
airlineManage.getRelativeHeight(),
cameraInfo.getSensorHigh(),
cameraInfo.getCameraFocal(),
airlineManage.getCourseOverlaps() / 100
);
// 默认2s
Double photoInterval = airlineManage.getPhotoInterval();
if (StringUtils.isNull(photoInterval) || photoInterval == 0) {
photoInterval = 2d;
}
double flightSpeed = DrawWayPointUtil.calcFlightSpeed(courseInterval, photoInterval);
extendedData.addElement("autoFlightSpeed").addText(String.valueOf(flightSpeed));
Element lineString = placeMarkElement.addElement("LineString");
lineString.addElement("altitudeMode").addText("clampToGround");
Element coordinatesEle = lineString.addElement("coordinates");
StringBuilder coordinatesBuilder = new StringBuilder();
airlineManage.getWaypointList()
.stream()
.forEach(latLng -> coordinatesBuilder.append(latLng.getLon() + "," + latLng.getLat() + "," + airlineManage.getRelativeHeight() + " \n"));
coordinatesEle.addText(coordinatesBuilder.toString());
//创建kml到本地
String kmlSuffix = ".kml";
String kmlTempPath = PvConstants.PV_TEMP_FILE_PATH + airlineManage.getAirlineName() + kmlSuffix;
File tempFile = new File(PvConstants.PV_TEMP_FILE_PATH);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(kmlTempPath), format);
xmlWriter.write(document);
xmlWriter.close();
// 压缩
String kmlZipSuffix = ".zip";
String kmlZipTempPath = PvConstants.PV_TEMP_FILE_PATH + airlineManage.getAirlineName() + kmlZipSuffix;
writeKml(Arrays.asList(kmlTempPath), kmlZipTempPath);
String objectUrl = minioUtil.uploadObjectAndObjectUrl(airlineManage.getAirlineName() + kmlZipSuffix, kmlZipTempPath);
airlineManage.setAirlineUrl(objectUrl);
FileUtils.deleteFile(kmlTempPath);
FileUtils.deleteFile(kmlZipTempPath);
return airlineManage;
}
public static void writeKml(List<String> filePathList, String kmlName) throws IOException {
OutputStream os = new BufferedOutputStream(new FileOutputStream(kmlName));
ZipOutputStream zos = new ZipOutputStream(os);
byte[] buf = new byte[8192];
int len;
for (String filePath : filePathList) {
File file = new File(filePath);
if (!file.isFile()) {
continue;
}
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
while ((len = bis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
bis.close();
}
zos.closeEntry();
zos.close();
os.close();
}
欢迎一起来学习和指导,谢谢关注!
本文来自博客园,作者:xiexie0812,转载请注明原文链接:https://www.cnblogs.com/mask-xiexie/p/16587715.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了