springboot项目java生成kml文件

一、简介

1.1.kml是什么

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

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

2.1引入依赖

 		<dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </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;

}




  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Java中可以使用JAK库或libkml-java生成KML文件。 1. 使用JAK库 JAK库是一个用于生成和解析KML文件Java库。它提供了很多方法来构建KML文件,并且支持所有KML标签和元素。 以下是一个简单的示例代码,演示如何使用JAK库生成一个KML文件: ```java import de.micromata.opengis.kml.v_2_2_0.*; public class KMLGenerator { public static void main(String[] args) { // 创建KML文档 Kml kml = new Kml(); Document document = kml.createAndSetDocument().withName("My KML Document"); // 创建Placemark Placemark placemark = document.createAndAddPlacemark(); placemark.withName("My Placemark").withDescription("This is my placemark"); // 添加点坐标 Coordinate coord = new Coordinate(10.0, 20.0); placemark.createAndSetPoint().addToCoordinates(coord); // 保存KML文件 kml.marshal(new File("path/to/my.kml")); } } ``` 2. 使用libkml-java libkml-java是Google开发的一个用于处理KML和KMZ文件的C++库的Java绑定。它提供了Java类来生成和解析KML文件,并且支持所有KML标签和元素。 以下是一个简单的示例代码,演示如何使用libkml-java生成一个KML文件: ```java import com.google.common.io.Files; import com.google.kml.Kml; import com.google.kml.Placemark; import com.google.kml.Point; import com.google.kml.coordinates.Coordinate; public class KMLGenerator { public static void main(String[] args) { // 创建KML文档 Kml kml = new Kml(); Placemark placemark = new Placemark().withName("My Placemark").withDescription("This is my placemark"); // 添加点坐标 Coordinate coord = new Coordinate(10.0, 20.0); Point point = new Point().addToCoordinates(coord); placemark.setGeometry(point); // 添加Placemark到KML文档 kml.setFeature(placemark); // 保存KML文件 try { Files.write(kml.toString().getBytes(), new File("path/to/my.kml")); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上两种方法都可以用来生成KML文件,具体选择哪种方法取决于你的需求和个人偏好。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕木兮人可

感谢支持,勿忘初心

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值