使用tinyxml2将gps经纬度转换为kml和gpx格式文件
转换代码在文章下方:
1.gps数据存储在文本中,如下所示:
每一行一个数据,数据使用逗号隔开,第一个为时间,第二个为经度,第三个维度,第四个为高度
2.使用tinyxml2将文件转换为标准的gpx格式文件,转换之后的结果如下:
3.转换后的gpx文件可以使用QGIS程序导入查验
转换代码如下:
kml转换代码
int writeKMLFIle(const std::string fileName, const std::vector<GPS_DATA>&data) {using namespace tinyxml2;tinyxml2::XMLDocument doc;tinyxml2::XMLDeclaration* declaration = doc.NewDeclaration();doc.InsertFirstChild(declaration);XMLElement* kml = doc.NewElement("kml ");kml->SetAttribute("xmlns", "http://www.opengis.net/kml/2.2");XMLElement* Document = doc.NewElement("Document");Document->SetAttribute("id", "root_doc");doc.InsertEndChild(Document);XMLElement* Placemark = doc.NewElement("Placemark");XMLElement* name_p = doc.NewElement("name");Placemark->InsertEndChild(name_p);XMLElement* Style = doc.NewElement("Style");XMLElement* LineStyle = doc.NewElement("LineStyle");Style->InsertEndChild(LineStyle);Placemark->InsertEndChild(Style);XMLElement* LineString = doc.NewElement("LineString");XMLElement* coordinates = doc.NewElement("coordinates");std::string strCoor = "";for (int i = 0; i < data.size(); ++i) {auto dat = data[i];strCoor +=dat.strLong+","+ dat.strLati+","+dat.strAlti+"\n";}coordinates->SetText(strCoor.c_str());LineString->InsertEndChild(coordinates);Placemark->InsertEndChild(LineString);Document->InsertEndChild(Placemark);kml->InsertEndChild(Document);doc.InsertEndChild(kml);return doc.SaveFile(fileName.c_str());
}
#include "../tinyxml2.h"
#include <fstream>
#include <string>;
#include <vector>struct GPS_DATA {std::string strTime;std::string strLong;std::string strLati;std::string strAlti;};int readCSVFile(const std::string fileName, std::vector<GPS_DATA>&data);
int writeGPXFile(const std::string fileName,const std::vector<GPS_DATA>&data);
int main(int argc,char** argv)
{std::string Nname = "C:\\Users\\op\\Desktop\\gpsdata/gps-2021-12-9-9-22-1.csv";std::vector<GPS_DATA> gpsdata;gpsdata.reserve(3 * 3600 / 6);int nLIne = readCSVFile(Nname, gpsdata);printf("input data has %d\n", nLIne);int res = writeGPXFile("gps.gpx", gpsdata);if (res == 0) {printf("create gpx data successful!\n");}else {printf("create gpx data error = %d!\n", res);}system("pause");return 0;
}void splitString(const std::string& istr,const std::string &s_str,std::vector<std::string>& vstr)
{if (istr.empty())return;auto p = istr.find_first_of(s_str);if ( p!= istr.npos) {vstr.push_back(istr.substr(0,p));splitString(istr.substr(p+1), s_str, vstr);}else {vstr.push_back(istr);}
}int readCSVFile(const std::string fileName, std::vector<GPS_DATA>& data)
{std::ifstream ifs(fileName);if (!ifs.is_open()) {printf("can't open file %s!\n", fileName.c_str());return -1;}do {std::string str;std::getline(ifs, str);if (str.empty())break;std::vector < std::string > vstr;splitString(str,",", vstr);if (vstr.size() == 4) {GPS_DATA gd{ vstr[0],vstr [1],vstr [2],vstr [3]};data.push_back(gd);}} while (true);return data.size();
}int writeGPXFile(const std::string fileName, const std::vector<GPS_DATA>&data)
{using namespace tinyxml2;tinyxml2::XMLDocument doc;tinyxml2::XMLDeclaration* declaration = doc.NewDeclaration();doc.InsertFirstChild(declaration);XMLElement* root = doc.NewElement("gpx ");root->SetAttribute("version", 1.00f);XMLElement* trackName = doc.NewElement("name");trackName->SetText("track--1");root->InsertFirstChild(trackName);XMLElement* tracks = doc.NewElement("trk");XMLElement* tracks_sg = doc.NewElement("trkseg");for (int i = 0; i < data.size(); ++i) {XMLElement* trkpt = doc.NewElement("trkpt");trkpt->SetAttribute("lat", data[i].strLati.c_str());trkpt->SetAttribute("lon", data[i].strLong.c_str());XMLElement* ele = doc.NewElement("ele");ele->SetText(data[i].strAlti.c_str());XMLElement* time = doc.NewElement("time");time->SetText(data[i].strTime.c_str());trkpt->InsertEndChild(ele);trkpt->InsertEndChild(time);tracks_sg->InsertEndChild(trkpt);}tracks->InsertFirstChild(tracks_sg);root->InsertEndChild(tracks);doc.InsertEndChild(root);return doc.SaveFile(fileName.c_str());
}
使用tinyxml2将gps经纬度转换为kml和gpx格式文件相关推荐
- 将Excel2003的xls格式文件转换为kml及gpx文件(ExcelToKml)
WGS84系统为GPS全球定位系统使用的坐标系,谷歌地球.手持GPS.带导航功能的手机以及车载GPS所用的其实都是WGS84系统,本软件可以将WGS84坐标下的经纬度坐标转换为谷歌地球(Google ...
- Java导出Kml或Kmz格式文件
需求:根据前端传递的笛卡尔坐标转换为kml或者kmz格式的文件 前端给的笛卡尔坐标数据格式为: [{"x":-2385687.468728053,"y":538 ...
- 原始GPS经纬度转换为距离(c/c++)
目录
- 请基于matlab将pdf格式的图片批处理转换为600dpi的tif格式文件,并裁剪为5736*2607的尺寸保存为600dpi的tif文件...
以下是在MATLAB中进行批处理转换和裁剪的基本步骤: 设置文件路径和输出路径. 使用 dir 函数读取文件夹中的所有 PDF 文件. 使用 imread 函数读取 PDF 文件. 使用 imresi ...
- KMZ转换为KML,KML转换为shp文件格式方法
最近项目使用谷歌地球的kmz数据,请教了各位高手收了解了kmz转换为kml到shp格式的方法: 参考资料 KMZ和KML转换为shape格式: ArcGIS学习记录-KMZ KML与SHP文件互相转换 ...
- java gps经纬度坐标转高德坐标_RN中Geolocation的经纬度坐标转换为高德地图坐标
Geolocation经纬度转换为高德地图经纬度方法 直接调用如下GPS. gcj_encrypt(lat, lon)就行了 var GPS = { PI : 3.14159265358979324, ...
- Python使用proj将GPS经纬度数据转换为utm坐标系(真实的地理位置坐标系,以米为单位),然后可以进行轨迹显示
1. 代码如下 from pyproj import Transformer import pyproj import json from matplotlib.pyplot import *if _ ...
- UTM坐标与GPS经纬度(WGS84)的相互转换
一.UTM介绍 统一横轴墨卡托投影系统(Universal Transverse Mercator,UTM) 参考: https://www.youtube.com/watch?v=LcVlx4Gur ...
- 利用Excel VBA将坐标点直接转换为谷歌地球的kml格式文件(ExcelToKml)
WGS84系统为GPS全球定位系统使用的坐标系,谷歌地球使用的即为WGS84,我们在野外所用的手持GPS.带导航功能的手机以及车载GPS所用的其实都是WGS84系统,本文所讲述的是利用Excel将WG ...
- 如何将EXCEL文件转换为KML
如何将EXCEL文件转换为KML 本文将演示的是如何将EXCEL文件转换为KML格式,然后在GoogleEarth中打开 工具/原料 91卫图助手下载器 91卫图助手下载地址: http://www. ...
最新文章
- 层次聚类定义、层次聚类过程可视化、簇间距离度量、BIRCH、两步聚类、BIRCH算法优缺点
- RHCS集群理论暨 最佳实践
- 微信背单词类小程序,小鸡单词源码下载,打卡微信小程序
- 面试常考:Synchronized 有几种用法?
- ant java xml文件,ant 学习(2)-ant开发java程序,buile.xml文件分离
- jQuery清空div内容
- 使用rundll32.exe绕过应用程序白名单(多种方法)
- ubuntu测试硬盘的读写速度
- Matlab 绘制柱状图并标注对应数字值
- 【IDE】IAR for ARM官网下载链接(包含一些历史版本)
- 通过Redis实现数据的交集、并集、补集
- 苹果发布“合二为一”芯片,华为公布“芯片叠加”的专利
- 定制ERP和通用ERP如何选择?
- 操作系统——进程的状态与转换
- Q版京剧脸谱来喽——状元
- 第四周总结(css,bootstrap)
- 华为开源数据库openGauss
- .NET C# winform窗体假死
- 【收藏】最全计算机网络基础思维导图
- Google Capture The Flag 2019 (Quals) WP