springboot项目java生成kml文件

一、简介

1.1.kml是什么

KML 是一种文件格式,用于在地球浏览器(例如 Google 地球、Google 地图和 Google 地图移动版)中显示地理数据。KML 使用包含嵌套的元素和属性的结构(基于标记),并符合 XML 标准。所有标记都区分大小写,并且必须与 KML 参考中列出的完全一样。该参考指出了哪些标记是可选的。在给定元素内,标记必须按照参考中列出的顺序显示。
说白了,kml文件存储的一般是经纬度信息,用于在实景地图中显示轨迹。例如导航航迹、无人机飞行航迹等。

二、在spingboot项目中的简单应用

2.1引入依赖

 		<dependency>
            <groupId>dom4jgroupId>
            <artifactId>dom4jartifactId>
            <version>1.6.1version>
        dependency>

2.2kml封装工具类

import cn.hutool.core.util.Idutilimport com.common.dao.entity.PhotoFileInfoimport com.common.dto.request.TravelRecordRequest; 
import org.dom4j. Document;
import org. dom4j. DocumentHelper; 
import org.dom4j. Element;
import org.dom4j.io.OutputFormat; 
import org.dom4j.io.XMLwriter;
import org.springframework.beans.factory.annotation.Value;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 

/**

@Author: kiki Date: 2022/2/19

*/

public class Kmlutil {

public static PhotoFileInfo setTravelsKml(List<TravelRecordRequest> travelRecords,String fileName,String sysPath)throws Exception{

	//根节点添加属性	
	Element root = DocumentHelper.createElement( name: "kml");
	Document document = DocumentHelper.createDocument(root); 
	root.addAttribute("xmlns", "http://www.opengis.net/kml/2.2")
		        .addAttribute("xmlns:gx", "http://www.google.com/kml/ext/2.2")
		        .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
		        .addAttribute("xsi:schemaLocation", 
		                "http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd");  
	Element documentElement = root.addElement("Document"); 
	documentElement.addElement( "name").addText(fileName); 
	Element folderElement = documentElement.addElement("Folder"); 
	folderElement. addElement("name").addText("轨迹点位"); 
	folderElement.addElement("open").addText("e");
	Element styleElement = folderElement.addElement("Style"); 
	styleElement.addAttribute("id", s1: "EB-01-008");
	Element lineStyleElement = styleElement. addElement("LineStyle"); 
	lineStyleElement. addElement("color").addText("FFeeffff");
	lineStyleElement. addElement ("width").addText("4");
	Element polyStyleElement = styleElement.addElement("PolyStyle"); 
	polyStyleElement. addElement("color").addText("40eeffff");
	polyStyleElement.addElement("outline").addText("e");
	Element iconStyleElement = styleElement.addElement("IconStyle"); 
	iconStyleElement.addElement ("Icon").addText("");
	Element style2Element = folderElement. addElement("Style");
	style2Element.addAttribute("id","EB-01-008(shadow)");
	Element lineStyle2Element = style2Element.addElement("LineStyle");
	lineStyle2Element.addElement ("color"). addText("FF007f7f");
	lineStyle2Element. addElement("width").addText("4");
	Element polyStyle2Element = style2Element.addElement(  "PolyStyle"); 
	polyStyle2Element.addElement( "color").addText("40007f7f");
	polystyle2Element.addElement(  "outline").addText("e");
	Element iconStyle2Element = style2Element.addElement (  "IconStyle"); 
	iconStyle2Element.addElement("Icon").addText("");
	//生成轨迹途路径数据
	Element lineElement = documentElement.addElement ( s: "Placemark"); 
	lineElement.addElement( s: "name").addText("working area");
	lineElement.addElement( s: "styleUrl").addText("#EB-01-008"); 
	Element pointElement = lineElement.addElement( s: "LineString");
	pointElement.addElement( s: "extrude"). addText("e");
	pointElement.addElement( s: "tessellate").addText("1");
	pointElement.addElement(s: "altitudeMode").addText("absolute"); 
	String linedata = "";
	//每个坐标以及高度用换行符或空格分开
	for (TravelRecordRequest travelRecord : travelRecords) { 
	linedata = linedata+
	"\n" +travelRecord.getLng()+", "+travelRecord.getLat()+", "+travelRecord.getAlt(); 
	}
	pointElement. addElement( s: "coordinates ").addText(linedata);
	//创建km1到本地
	OutputFormat format = OutputFormat.createPrettyPrint(); 
	format.setEncoding("utf-8");
	String dateDir = new SimpleDateFormat( pattern: "yyyy/MM/dd").format(new Date()); 
	String localDirPath = sysPath+"/resource/"+ dateDir+"/thumb";
	He Ean yiee Nangate Code Arahge Betactor Buld Run Iools VCS Mindow Help
	File fileDir = new File(localDirPath); 
	if(!fileDir.exists()){
		fileDir.mkdirs();
	}
	String endName= fileName+"-"+ IdUtil.fastSimpLeUUID()+".km1"; 
	String relativePath ="/resource/"+dateDir+"/"+endName;
	String filePath=sysPath+relativePath; 
	XMLWriter xmlwriter = new XMLWriter(new FileOutputStream(filePath));
	xmlWriter.write(document); 
	xmlwriter.close(); 
	PhotoFileInfo photoFileInfo = new PhotoFileInfo(); 
	photoFileInfo.setFileRelativePath(relativePath); 
	photoFileInfo.setFilePath(filePath); 
	photoFileInfo.setUploadTime(new Date()); 
	photoFileInfo.setNewName(endName); 
	photoFileInfo.setoriginalName(fileName); 
	return photoFileInfo;

}




你可能感兴趣的:(java,spring,boot,开发语言,kml,spring)