❤️JAVA写入与读取GPX文件工具类❤️

一、创建TrackPoint、TrackSegment、Track三个实体类

根据实际情况自定义这个三个实体类的名称和属性

1、TrackPoint类

import java.util.Date;/*** @Author: LEAVES* @Version 1.0* @Date: 2021年10月08日  10时18分56秒* @Description: 每一个点的信息属性【其对应接收GPX中标签属性值】*/
public class TrackPoint {private double latitude;private double longitude;private Double elevation;private Date time;public TrackPoint() {}public TrackPoint(double latitude, double longitude, Double elevation, Date time) {this.latitude = latitude;this.longitude = longitude;this.elevation = elevation;this.time = time;}public double getLatitude() {return this.latitude;}public void setLatitude(double latitude) {this.latitude = latitude;}public double getLongitude() {return this.longitude;}public void setLongitude(double longitude) {this.longitude = longitude;}public Double getElevation() {return this.elevation;}public void setElevation(Double elevation) {this.elevation = elevation;}public Date getTime() {return this.time;}public void setTime(Date time) {this.time = time;}public double distanceTo(TrackPoint point) {boolean r = true;double latDistance = Math.toRadians(point.latitude - this.latitude);double lonDistance = Math.toRadians(point.longitude - this.longitude);double a = Math.sin(latDistance / 2.0D) * Math.sin(latDistance / 2.0D) + Math.cos(Math.toRadians(this.latitude)) * Math.cos(Math.toRadians(point.latitude)) * Math.sin(lonDistance / 2.0D) * Math.sin(lonDistance / 2.0D);double c = 2.0D * Math.atan2(Math.sqrt(a), Math.sqrt(1.0D - a));double distance = 6371.0D * c * 1000.0D;if (point.elevation != null && this.elevation != null) {double height = point.elevation - this.elevation;distance = Math.pow(distance, 2.0D) + Math.pow(height, 2.0D);}return Math.sqrt(distance);}
}

2、TrackSegment类

import java.util.ArrayList;
import java.util.List;/*** @Author: LEAVES* @Version 1.0* @Date: 2021年10月08日  10时55分37秒* @Description: 每个点的集合【一段轨迹】*/
public class TrackSegment {private List<TrackPoint> points;public TrackSegment() {this.points = new ArrayList();}public TrackSegment(List<TrackPoint> points) {this.points = points;}public void addTrackPoint(TrackPoint point) {this.points.add(point);}public void removeTrackPoint(int index) {this.points.remove(index);}public void clearTrackPoints() {this.points.clear();}public List<TrackPoint> getPoints() {return this.points;}public void setPoints(List<TrackPoint> points) {this.points = points;}
}

3、Track类

import java.util.ArrayList;
import java.util.List;/*** @Author: LEAVES* @Version 1.0* @Date: 2021年10月08日  11时18分47秒* @Description: 整个轨迹信息*/
public class Track {private List<TrackSegment> segments;public Track() {this.segments = new ArrayList();}public Track(List<TrackSegment> segments) {this.segments = segments;}public void addTrackSegment(TrackSegment segment) {this.segments.add(segment);}public void removeTrackSegment(int index) {this.segments.remove(index);}public void clearTrackSegments() {this.segments.clear();}public List<TrackSegment> getSegments() {return this.segments;}public void setSegments(List<TrackSegment> segments) {this.segments = segments;}
}

二、写入GPX文件工具类

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Locale;/*** @Author: LEAVES* @Version 1.0* @Date: 2021年10月08日  14时10分32秒* @Description: 写入GPX文件工具类*/
public class GpxWriter {private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");private final File gpxFile;public GpxWriter(String path) {this.gpxFile = new File(path);if (this.gpxFile.isDirectory()) {throw new RuntimeException("The given file is a directory.");}}/*** 写入GPX数据* <p>* 标准的gpx就是下面这些标签名称,如有特殊请自行添加或修改** @param track*/public void writeData(Track track) {DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();String coordsFormat = "%.7f";String var5 = "%.1f";try {DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();Document document = docBuilder.newDocument();Element rootElement = document.createElement("gpx");document.appendChild(rootElement);Element trk = document.createElement("trk");rootElement.appendChild(trk);track.getSegments().forEach((s) -> {Element trkseg = document.createElement("trkseg");trk.appendChild(trkseg);s.getPoints().forEach((p) -> {Element trkpt = document.createElement("trkpt");trkpt.setAttribute("lat", String.format(Locale.ROOT, "%.7f", p.getLatitude()));trkpt.setAttribute("lon", String.format(Locale.ROOT, "%.7f", p.getLongitude()));Element time;if (p.getElevation() != null) {time = document.createElement("ele");time.setTextContent(String.format(Locale.ROOT, "%.1f", p.getElevation()));trkpt.appendChild(time);}if (p.getTime() != null) {time = document.createElement("time");time.setTextContent(sdf1.format(p.getTime()));trkpt.appendChild(time);}trkseg.appendChild(trkpt);});});Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty("indent", "yes");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");DOMSource source = new DOMSource(document);StreamResult console = new StreamResult(System.out);StreamResult file = new StreamResult(this.gpxFile);transformer.transform(source, console);transformer.transform(source, file);} catch (TransformerException | ParserConfigurationException var13) {var13.printStackTrace();}}
}

三、读取GPX文件工具类

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;/*** @Author: LEAVES* @Version 1.0* @Date: 2021年10月08日  15时26分57秒* @Description: 读取GPX文件工具类* <p>* 参照JAVA读取XML文档的方法读取GPX文件*/
public class GpxReader {//这里时间格式根据GPX文件中的时间格式来选择确定private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");private static final SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");private static final SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");private final File gpxFile;public GpxReader(String path) {this.gpxFile = new File(path);if (!this.gpxFile.exists()) {throw new RuntimeException("File " + path + "does not exist.");} else if (this.gpxFile.isDirectory()) {throw new RuntimeException("The given file is a directory.");}}/*** 读取轨迹数据* <p>* 标准的gpx就是下面这些标签名称,如有特殊请自行添加或修改** @return*/public Track readData() {Track track = new Track();try {//1、得到 DOM 解析器的工厂实例DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();//2、然后从 DOM 工厂获得 DOM 解析器DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();//3、把要解析的 GPX 文档转化为输入流,以便 DOM 解析器解析它Document document = dBuilder.parse(this.gpxFile);//4、找出每个字节点(也可以理解其为根节点)//获取指定节点//需要的数据都在trk标签中NodeList trk = document.getElementsByTagName("trk");for (int i = 0; i < trk.getLength(); ++i) {Node trkItem = trk.item(i);if (trkItem.getNodeName().equals("trk")) {NodeList trkSegments = trkItem.getChildNodes();for (int j = 0; j < trkSegments.getLength(); ++j) {Node trkSegment = trkSegments.item(j);if (trkSegment.getNodeName().equals("trkseg")) {TrackSegment segment = new TrackSegment();track.addTrackSegment(segment);//获取其对应字节点NodeList trkPts = trkSegment.getChildNodes();for (int k = 0; k < trkPts.getLength(); ++k) {Node trkPt = trkPts.item(k);String nodename = trkPt.getNodeName();if (trkPt.getNodeName().equals("trkpt")) {Element element = (Element) trkPt;double lat = Double.parseDouble(element.getAttribute("lat"));double lon = Double.parseDouble(element.getAttribute("lon"));Double ele = null;String time = null;//获取其对应字节点List<Node> nodes = toNodeList(element.getChildNodes());Optional<Node> elev = nodes.stream().filter((e) -> {return e.getNodeName().equals("ele");}).findFirst();if (elev.isPresent()) {ele = Double.parseDouble(((Node) elev.get()).getTextContent());}Optional<Node> timeNode = nodes.stream().filter((e) -> {return e.getNodeName().equals("time");}).findFirst();if (timeNode.isPresent()) {time = ((Node) timeNode.get()).getTextContent();}segment.addTrackPoint(new TrackPoint(lat, lon, ele, this.parseDate(time)));}}}}}}} catch (IOException | ParserConfigurationException | SAXException var26) {var26.printStackTrace();}return track;}private static List<Node> toNodeList(NodeList nodeList) {List<Node> nodes = new ArrayList();for (int i = 0; i < nodeList.getLength(); ++i) {nodes.add(nodeList.item(i));}return nodes;}private Date parseDate(String value) {Date date = null;try {date = sdf1.parse(value);} catch (ParseException var5) {}return date;}
}

四、读取GPX测试

1、GPX文件例子:

2、读取实现

public static void main(String[] args) {String path = "D:\\test\\轨迹_20211007_134948.gpx";GpxReader gpxFileReader = new GpxReader(path);Track track = gpxFileReader.readData();List<TrackSegment> segments = track.getSegments();segments.forEach(x -> {List<TrackPoint> points = x.getPoints();points.forEach(y -> {System.out.println("======================" );double longitude = y.getLongitude();System.out.println("经度 = " + longitude);double latitude = y.getLatitude();System.out.println("纬度 = " + latitude);Double elevation = y.getElevation();System.out.println("海拔 = " + elevation);Date time = y.getTime();System.out.println("时间 = " + time);});});
}

3、读取结果

JAVA写入与读取GPX文件工具类相关推荐

  1. Java读取.txt文件工具类

    相关工具类代码 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; i ...

  2. 【Java】生成 .json格式文件工具类

    package com.glodon.kgb.utils.json;import java.io.File; import java.io.FileOutputStream; import java. ...

  3. java io文件流序列化_Java——Properties集合,Object序列化流与反序列化流,打印流,commons-IO文件工具类...

    一.properties集合 集合对象Properties类,继承Hashtable,实现Map接口,可以和IO对象结合使用,实现数据的持久存储. 特点: Hashtable的子类,map集合中的方法 ...

  4. Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException; import java.io.InputStreamReader; import j ...

  5. java file ip_java常用工具类 IP、File文件工具类

    本文实例为大家分享了java常用工具类的具体实现代码,供大家参考,具体内容如下 IP工具类 package com.jarvis.base.util; import java.io.IOExcepti ...

  6. java filehelper_Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException; import java.io.InputStreamReader; import j ...

  7. java读写excel文件poi_Java利用POI读写Excel文件工具类

    本文实例为大家分享了Java读写Excel文件工具类的具体代码,供大家参考,具体内容如下 package com.test.app.utils; import java.io.File; import ...

  8. java常用文件工具类

    java常用工具类(一) 主要是Java操作文件及及IO流的一些常用库,是Apache对java的IO包的封装,这里面文件类工具有两个非常核心的类FilenameUtils跟FileUtils,IO流 ...

  9. java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例

    本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io. ...

  10. 【Java工具类】(22)—服务器传文件工具类SCp和Sftp

    Java工具类(22)-服务器传文件工具类SCp和Sftp 1.Scp package com.awifi.capacity.docker.manager.utils;import ch.ethz.s ...

最新文章

  1. DBUtils开源JDBC类库,对JDBC简单封装(作用是:简化编码工作量,同时不会影响程序的性能)...
  2. 果园种树java_Java版淘金果园系统
  3. 【django】使用虚拟环境
  4. 正式进驻1410实验室!
  5. 计算机网络--接入互联网方式
  6. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 3丨行程和用户【难度困难】
  7. 95-910-165-源码-FlinkSQL-Flink SQL 中的时间属性
  8. NEYC 2017 游记
  9. python正态分布代码_Tests for normality正态分布检验(python代码实现)
  10. 奇迹s6ep3服务器修改技术,奇迹S6EP3服务端之让天鹰及装备不掉持久的
  11. 如何使用JavaScript重定向到其他网页?
  12. CRM 安装不规范,亲人两行泪
  13. SaaS的优势和劣势
  14. 6张图教你搞定侧方停车----fwqlzz love is for ever
  15. latex 插图 上下放_专辑插图中上下文中的文本
  16. Access violation reading location 0x000000XX
  17. 手机访问电脑本地html文件
  18. 【数据库与SQL】力扣刷题SQL篇(7)
  19. 时间抖动(jitter)--学习笔记
  20. MySQL常用命令合集及语法

热门文章

  1. python模拟火车票订票系统_除夕火车票明天开售,上海各大火车站售票时间都在这...
  2. 计算机操作系统第四版知识点总结(详细版一)
  3. 剑指offer题目大全
  4. Linux快速入门之文件操作(01)
  5. 管理员权限自动注册Dll文件
  6. 武汉CMMI3-CMMI5三年到期后复审指南
  7. pygame 鼠标事件
  8. 贪吃蛇程序设计报告python_C某贪吃蛇程序设计报告.doc
  9. stdafx.h 简介及作用
  10. oppo 系列手机刷机教程