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